Skip to content

Commit

Permalink
Merge pull request #608 from bounswe/BACKEND-600
Browse files Browse the repository at this point in the history
added the seperate endpoints for translations and meanings
  • Loading branch information
kaanyolcu22 authored Nov 24, 2024
2 parents daf8a44 + f9b274d commit 37aaa99
Show file tree
Hide file tree
Showing 9 changed files with 250 additions and 119 deletions.
2 changes: 1 addition & 1 deletion backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ ENV PYTHONUNBUFFERED 1
EXPOSE 8000

# Run the Django development server
CMD python manage.py makemigrations && python manage.py makemigrations app && python manage.py migrate && python manage.py runserver 0.0.0.0:8000 && python manage.py populate_db_file /app/data/db_files/words.csv /app/data/db_files/translations.csv /app/data/db_files/relationships.csv
CMD python manage.py makemigrations && python manage.py makemigrations app && python manage.py migrate && python manage.py populate_db_file data/db_files/words.csv data/db_files/translations.csv data/db_files/relationships.csv && python manage.py runserver 0.0.0.0:8000
2 changes: 1 addition & 1 deletion backend/app/management/commands/clear_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ def handle(self, *args, **kwargs):

self.stdout.write(self.style.SUCCESS("All data cleared successfully!"))
except Exception as e:
self.stderr.write(self.style.ERROR(f"Error: {e}"))
self.stderr.write(self.style.ERROR(f"Error: {e}"))
53 changes: 34 additions & 19 deletions backend/app/management/commands/populate_db_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,54 @@ def handle(self, *args, **kwargs):
self.populate_words(words_csv)
self.populate_translations(translations_csv)
self.populate_relationships(relationships_csv)
self.stdout.write(self.style.SUCCESS("Database populated successfully!"))
except Exception as e:
self.stderr.write(self.style.ERROR(f"Error: {e}"))

def populate_words(self, file_path):
self.stdout.write(f"Loading Words from {file_path}...")
with open(file_path, newline='', encoding='utf-8') as csvfile:
reader = csv.DictReader(csvfile)

word_dict = {}

for row in reader:
Word.objects.get_or_create(
word=row['word'],
defaults={
word_list = word_dict.get(row['word'], list())
info_dict = {
'language': row.get('language', 'eng'),
'level': row.get('level'),
'part_of_speech': row.get('part_of_speech'),
'meaning': row.get('meaning', 'Meaning not available'),
'meaning': row.get('explanation', 'Meaning not available'),
'sentence': row.get('sentence', 'Sentence not available')

}

word_list.append(info_dict)

word_dict[row['word']] = word_list



for word in word_dict.keys():
defaults={
'language': '',
'level': '',
'part_of_speech': '',
'meaning': '',
'sentence': ''
}
for key in defaults.keys():
parts = [info[key] for info in word_dict[word]]
defaults[key] = parts



result = Word.objects.get_or_create(
word=word,
defaults=defaults
)
self.stdout.write(self.style.SUCCESS("Words loaded successfully."))



def populate_translations(self, file_path):
self.stdout.write(f"Loading Translations from {file_path}...")
with open(file_path, newline='', encoding='utf-8') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
Expand All @@ -59,21 +84,17 @@ def populate_translations(self, file_path):
'sentence': 'No example provided'
}
)
if created:
self.stdout.write(f"Created missing Word: {row['word']}")

# Create or get the Translation
Translation.objects.get_or_create(
word=word,
translation=row['turkish_translation']
)
self.stdout.write(self.style.SUCCESS("Translations loaded successfully."))




def populate_relationships(self, file_path):
self.stdout.write(f"Loading Relationships from {file_path}...")
with open(file_path, newline='', encoding='utf-8') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
Expand All @@ -100,9 +121,6 @@ def populate_relationships(self, file_path):
}
)

# If the related word was created, log it
if created:
self.stdout.write(f"Created missing related word '{related_word}'.")

# Create the relationship in the Relationship table
Relationship.objects.get_or_create(
Expand All @@ -114,12 +132,9 @@ def populate_relationships(self, file_path):
except Word.DoesNotExist:
self.stderr.write(f"Word '{row['word']}' not found. Skipping relationship.")

self.stdout.write(self.style.SUCCESS("Relationships loaded successfully."))









15 changes: 9 additions & 6 deletions backend/app/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@


# Generated by Django 4.2.16 on 2024-11-24 15:14


from django.conf import settings
import django.contrib.postgres.fields
from django.db import migrations, models
Expand Down Expand Up @@ -59,12 +62,12 @@ class Migration(migrations.Migration):
name='Word',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('word', models.CharField(max_length=255, unique=True)),
('language', models.CharField(default='eng', max_length=3)),
('level', models.CharField(blank=True, max_length=20, null=True)),
('part_of_speech', models.CharField(blank=True, max_length=20, null=True)),
('meaning', models.CharField(default='Meaning not available', max_length=1000)),
('sentence', models.CharField(default='Sentence not available', max_length=1000)),
('word', models.CharField(max_length=255)),
('language', models.CharField(default='eng', max_length=10000)),
('level', models.CharField(blank=True, max_length=10000, null=True)),
('part_of_speech', models.CharField(blank=True, max_length=20000, null=True)),
('meaning', models.CharField(default='Meaning not available', max_length=10000)),
('sentence', models.CharField(default='Sentence not available', max_length=10000)),
],
),
migrations.CreateModel(
Expand Down
12 changes: 6 additions & 6 deletions backend/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ def __str__(self):
return self.title

class Word(models.Model):
word = models.CharField(max_length=255, unique=True)
language = models.CharField(max_length=3, default='eng') # e.g., 'eng' for English
level = models.CharField(max_length=20, blank=True, null=True) # e.g., 'A1', 'B2'
part_of_speech = models.CharField(max_length=20, blank=True, null=True)
meaning = models.CharField(max_length=1000, default="Meaning not available")
sentence = models.CharField(max_length=1000, default="Sentence not available")
word = models.CharField(max_length=255)
language = models.CharField(max_length=10000, default='eng') # e.g., 'eng' for English
level = models.CharField(max_length=10000, blank=True, null=True) # e.g., 'A1', 'B2'
part_of_speech = models.CharField(max_length=20000, blank=True, null=True)
meaning = models.CharField(max_length=10000, default="Meaning not available")
sentence = models.CharField(max_length=10000, default="Sentence not available")

def __str__(self):
return self.word
Expand Down
12 changes: 5 additions & 7 deletions backend/app/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.urls import path
from app.views import *
from app.views_directory.wordviews import get_turkish_translation, get_lexvo_info, get_word_meanings, fetch_english_words
from app.views_directory.profileviews import view_profile, update_profile, view_other_profile, view_followers, view_following
from app.views_directory.wordviews import get_word_info, get_turkish_translation, get_similar_level_and_part_of_speech, get_word_details
from app.views_directory.follow_unfollow import follow_user, unfollow_user
from app.views_directory.authentication_endpoints import RegisterView, LoginView, LogoutView, RefreshTokenView
from app.views_directory.comments import add_comment, delete_comment, like_comment, unlike_comment, get_comment_by_id
Expand Down Expand Up @@ -35,17 +35,15 @@
path('quiz/solved/<str:username>/', quiz_views.view_solved_quizzes, name="view_solved_quizzes"),
path('quiz/review/<int:quiz_result_id>/', quiz_views.get_quiz_review, name="review_quiz"),
path('quiz/recommend/<int:quiz_id>/', quiz_views.get_quiz_recommendations, name="recommend_quiz"),


path('create-post/',create_post, name='create_post'),
path('signup/', RegisterView.as_view(), name='auth_register'),
path('login/', LoginView.as_view(), name='auth_login'),
path('logout/', LogoutView.as_view(), name='auth_logout'),
path('refresh/', RefreshTokenView.as_view(), name='token_refresh'),
path('get-word-info/<str:word>/', get_word_info, name='get_word_info'),
path('get-word-details/<str:word>/', get_word_details, name='get_word_details'),
path('get-translation/<str:word>/', get_turkish_translation, name='get_turkish_translation'),
path('get-related-words/<str:word>/', get_similar_level_and_part_of_speech, name='get_similar_level_and_part_of_speech'),
path('get-lexvo-info/<str:word>/', get_lexvo_info, name='get_lexvo_info'),
path('get-turkish/<str:word>/', get_turkish_translation, name='get_turkish_translation'),
path('get-meaning/<str:word>/', get_word_meanings,name='get_word_meanings'),
path('get-english/<str:turkish_word>/', fetch_english_words, name='fetch_english_word'),
path('profile/follow/', follow_user, name='follow_user'),
path('profile/unfollow/', unfollow_user, name='unfollow_user'),
path('post/', get_post_details, name='get_post_details'),
Expand Down
Loading

0 comments on commit 37aaa99

Please sign in to comment.