From 74ac767db9f52d93540923f66aefa6f0f2b22193 Mon Sep 17 00:00:00 2001 From: arastasci Date: Wed, 11 Dec 2024 20:21:22 +0300 Subject: [PATCH 1/2] feat: bookmarking words and viewing them. --- backend/app/migrations/0002_wordbookmark.py | 27 +++++++++ .../0003_alter_wordbookmark_word.py | 18 ++++++ backend/app/models.py | 11 ++++ backend/app/urls.py | 6 +- backend/app/views_directory/wordviews.py | 56 ++++++++++++++++++- 5 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 backend/app/migrations/0002_wordbookmark.py create mode 100644 backend/app/migrations/0003_alter_wordbookmark_word.py diff --git a/backend/app/migrations/0002_wordbookmark.py b/backend/app/migrations/0002_wordbookmark.py new file mode 100644 index 00000000..fc54dcc6 --- /dev/null +++ b/backend/app/migrations/0002_wordbookmark.py @@ -0,0 +1,27 @@ +# Generated by Django 4.2.17 on 2024-12-11 17:03 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('app', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='WordBookmark', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='bookmarked_words', to=settings.AUTH_USER_MODEL)), + ('word', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='bookmarks', to='app.word')), + ], + options={ + 'unique_together': {('user', 'word')}, + }, + ), + ] diff --git a/backend/app/migrations/0003_alter_wordbookmark_word.py b/backend/app/migrations/0003_alter_wordbookmark_word.py new file mode 100644 index 00000000..2329712f --- /dev/null +++ b/backend/app/migrations/0003_alter_wordbookmark_word.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.17 on 2024-12-11 17:17 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('app', '0002_wordbookmark'), + ] + + operations = [ + migrations.AlterField( + model_name='wordbookmark', + name='word', + field=models.CharField(max_length=255), + ), + ] diff --git a/backend/app/models.py b/backend/app/models.py index adcdb3c4..d56a167b 100644 --- a/backend/app/models.py +++ b/backend/app/models.py @@ -147,7 +147,18 @@ class Word(models.Model): def __str__(self): return self.word + + +class WordBookmark(models.Model): + user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='bookmarked_words') + word = models.CharField(max_length=255) + class Meta: + unique_together = ('user', 'word') + + def __str__(self): + return f"{self.user.username} bookmarked {self.word.word}" + class Relationship(models.Model): word = models.ForeignKey(Word, on_delete=models.CASCADE, related_name="relationships") related_word = models.ForeignKey(Word, on_delete=models.CASCADE, related_name="related_to") diff --git a/backend/app/urls.py b/backend/app/urls.py index 1abb9cb1..967262a3 100644 --- a/backend/app/urls.py +++ b/backend/app/urls.py @@ -1,6 +1,6 @@ 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.wordviews import get_turkish_translation, get_lexvo_info, get_word_meanings, fetch_english_words, bookmark_word, unbookmark_word, get_bookmarked_words from app.views_directory.profileviews import view_profile, update_profile, view_other_profile, view_followers, view_following from app.views_directory.follow_unfollow import follow_user, unfollow_user from app.views_directory.authentication_endpoints import RegisterView, LoginView, LogoutView, RefreshTokenView @@ -37,6 +37,10 @@ path('quiz/recommend//', quiz_views.get_quiz_recommendations, name="recommend_quiz"), path('quiz/review_latest//', quiz_views.get_latest_quiz_review, name="review_latest_quiz"), + path('word/bookmark/', bookmark_word, name='bookmark_word'), + path('word/unbookmark/', unbookmark_word, name='unbookmark_word'), + path('word/bookmarks/', get_bookmarked_words, name='get_bookmarked_words'), + path('create-post/',create_post, name='create_post'), path('signup/', RegisterView.as_view(), name='auth_register'), diff --git a/backend/app/views_directory/wordviews.py b/backend/app/views_directory/wordviews.py index 22818536..2bbfd384 100644 --- a/backend/app/views_directory/wordviews.py +++ b/backend/app/views_directory/wordviews.py @@ -17,11 +17,65 @@ from ..serializers import * from django.shortcuts import render from ..models import Tags -from ..models import Quiz, Relationship, Word, Translation +from ..models import Quiz, Relationship, Word, Translation, WordBookmark from ..word_service import lexvo_manager import requests +@api_view(['POST']) +@permission_classes([IsAuthenticated]) +def bookmark_word(request): + """ + Bookmark a word for the user. + """ + try: + word = request.data.get('word') + user = request.user + bookmark, created = WordBookmark.objects.get_or_create(user=user, word=word) + if not created: + return Response({"error": "Word already bookmarked"}, status=status.HTTP_400_BAD_REQUEST) + + return Response({"message": "Word bookmarked successfully"}, status=status.HTTP_201_CREATED) + + except Exception as e: + return Response({"error": f"An error occurred: {str(e)}"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + + +@api_view(['POST']) +@permission_classes([IsAuthenticated]) +def unbookmark_word(request): + """ + Unbookmark a word for the user. + """ + try: + word = request.data.get('word') + user = request.user + bookmark = WordBookmark.objects.filter(user=user, word=word).first() + if not bookmark: + return Response({"error": "Word not bookmarked"}, status=status.HTTP_400_BAD_REQUEST) + + bookmark.delete() + return Response({"message": "Word unbookmarked successfully"}, status=status.HTTP_200_OK) + + except Exception as e: + return Response({"error": f"An error occurred: {str(e)}"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + +@api_view(['GET']) +@permission_classes([IsAuthenticated]) +def get_bookmarked_words(request): + """ + Get all words bookmarked by the user. + """ + try: + user = request.user + bookmarks = WordBookmark.objects.filter(user=user) + bookmarked_words = [bookmark.word for bookmark in bookmarks] + return Response({"bookmarked_words": bookmarked_words}, status=status.HTTP_200_OK) + + except Exception as e: + return Response({"error": f"An error occurred: {str(e)}"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + + @api_view(['GET']) def get_lexvo_info(request,word): try: From fd1e6a63025afce0b43918c87748eb0adc60a76d Mon Sep 17 00:00:00 2001 From: arastasci Date: Wed, 11 Dec 2024 20:23:21 +0300 Subject: [PATCH 2/2] improve: words as params. --- backend/app/urls.py | 4 ++-- backend/app/views_directory/wordviews.py | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/backend/app/urls.py b/backend/app/urls.py index 967262a3..ff5cf02b 100644 --- a/backend/app/urls.py +++ b/backend/app/urls.py @@ -37,8 +37,8 @@ path('quiz/recommend//', quiz_views.get_quiz_recommendations, name="recommend_quiz"), path('quiz/review_latest//', quiz_views.get_latest_quiz_review, name="review_latest_quiz"), - path('word/bookmark/', bookmark_word, name='bookmark_word'), - path('word/unbookmark/', unbookmark_word, name='unbookmark_word'), + path('word/bookmark//', bookmark_word, name='bookmark_word'), + path('word/unbookmark//', unbookmark_word, name='unbookmark_word'), path('word/bookmarks/', get_bookmarked_words, name='get_bookmarked_words'), diff --git a/backend/app/views_directory/wordviews.py b/backend/app/views_directory/wordviews.py index 2bbfd384..07d164ef 100644 --- a/backend/app/views_directory/wordviews.py +++ b/backend/app/views_directory/wordviews.py @@ -24,12 +24,11 @@ @api_view(['POST']) @permission_classes([IsAuthenticated]) -def bookmark_word(request): +def bookmark_word(request, word): """ Bookmark a word for the user. """ try: - word = request.data.get('word') user = request.user bookmark, created = WordBookmark.objects.get_or_create(user=user, word=word) if not created: @@ -43,12 +42,11 @@ def bookmark_word(request): @api_view(['POST']) @permission_classes([IsAuthenticated]) -def unbookmark_word(request): +def unbookmark_word(request, word): """ Unbookmark a word for the user. """ try: - word = request.data.get('word') user = request.user bookmark = WordBookmark.objects.filter(user=user, word=word).first() if not bookmark: