Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(backend): Bookmarking Words #763

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions backend/app/migrations/0002_wordbookmark.py
Original file line number Diff line number Diff line change
@@ -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')},
},
),
]
18 changes: 18 additions & 0 deletions backend/app/migrations/0003_alter_wordbookmark_word.py
Original file line number Diff line number Diff line change
@@ -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),
),
]
11 changes: 11 additions & 0 deletions backend/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
6 changes: 5 additions & 1 deletion backend/app/urls.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -37,6 +37,10 @@
path('quiz/recommend/<int:quiz_id>/', quiz_views.get_quiz_recommendations, name="recommend_quiz"),
path('quiz/review_latest/<int:quiz_id>/', quiz_views.get_latest_quiz_review, name="review_latest_quiz"),

path('word/bookmark/<str:word>/', bookmark_word, name='bookmark_word'),
path('word/unbookmark/<str:word>/', 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'),
Expand Down
54 changes: 53 additions & 1 deletion backend/app/views_directory/wordviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,63 @@
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, word):
"""
Bookmark a word for the user.
"""
try:
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, word):
"""
Unbookmark a word for the user.
"""
try:
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:
Expand Down