diff --git a/apps/bookmark/views/bookmark.py b/apps/bookmark/views/bookmark.py index 05f3210..beefd23 100644 --- a/apps/bookmark/views/bookmark.py +++ b/apps/bookmark/views/bookmark.py @@ -25,7 +25,7 @@ class AddBookmarkView(CreateAPIView): operation_description="Add a bookmark for a specific content in a service.\ If the bookmark already exists but is inactive, it will be reactivated.\ \n Services must be choose from : \n \ - 'Library'\n'Podcast'\n'Podcast Playlist'\n'Hadith'\n'Video'\n'Video Playlist'\n'Article'", + 'Library'\n'Podcast'\n'Podcast Playlist'\n'Hadith' \n 'Hadith Correction'\n'Video'\n'Video Playlist'\n'Article'", tags=["Dobodbi - Bookmarks"], request_body=BookmarkSerializer, responses={ diff --git a/apps/hadis/views/hadis.py b/apps/hadis/views/hadis.py index d450af0..e6b0748 100644 --- a/apps/hadis/views/hadis.py +++ b/apps/hadis/views/hadis.py @@ -264,16 +264,29 @@ class HadisListView(ListAPIView): if not HadisCategory.objects.filter(slug=category_slug).exists(): return Hadis.objects.none() - return Hadis.objects.filter( + queryset = Hadis.objects.filter( category__slug=category_slug, status=True - ).order_by('number').annotate( + ).annotate( # distinct=True is CRITICAL here. # Without it, if 3 narrators are from "Layer 1", it counts as 3. # With it, it counts as 1 (unique layer). layer_count=Count('transmitters__narrator_layer', distinct=True) ).select_related('category') + # Filter by bookmarks if provided + is_bookmark = self.request.query_params.get('is_bookmark', '').lower() + if is_bookmark == 'true' and self.request.user.is_authenticated: + from apps.bookmark.models.bookmark import Bookmark + bookmarked_ids = Bookmark.objects.filter( + user=self.request.user, + service=Bookmark.ServiceChoices.HADITH, + status=True + ).values_list('content_id', flat=True) + queryset = queryset.filter(id__in=bookmarked_ids) + + return queryset + def get_serializer_context(self): """Add user bookmarks to serializer context to avoid caching issues""" context = super().get_serializer_context() @@ -328,6 +341,17 @@ class HadisMainListView(ListAPIView): if category_filter: queryset = queryset.filter(category__title__icontains=category_filter) + # Filter by bookmarks if provided + is_bookmark = self.request.query_params.get('is_bookmark', '').lower() + if is_bookmark == 'true' and self.request.user.is_authenticated: + from apps.bookmark.models.bookmark import Bookmark + bookmarked_ids = Bookmark.objects.filter( + user=self.request.user, + service=Bookmark.ServiceChoices.HADITH, + status=True + ).values_list('content_id', flat=True) + queryset = queryset.filter(id__in=bookmarked_ids) + return queryset def list(self, request, *args, **kwargs): @@ -569,7 +593,20 @@ class HadisCorrectionsView(ListAPIView): hadis = Hadis.objects.get(slug=hadis_slug, status=True) if not HadisCorrection.objects.filter(hadis=hadis).exists(): return Hadis.objects.none() - return HadisCorrection.objects.filter(hadis=hadis) + queryset = HadisCorrection.objects.filter(hadis=hadis) + + # Filter by bookmarks if provided + is_bookmark = self.request.query_params.get('is_bookmark', '').lower() + if is_bookmark == 'true' and self.request.user.is_authenticated: + from apps.bookmark.models.bookmark import Bookmark + bookmarked_ids = Bookmark.objects.filter( + user=self.request.user, + service=Bookmark.ServiceChoices.HADITH_CORRECTION, + status=True + ).values_list('content_id', flat=True) + queryset = queryset.filter(id__in=bookmarked_ids) + + return queryset except Hadis.DoesNotExist: return HadisCorrection.objects.none() diff --git a/apps/podcast/views.py b/apps/podcast/views.py index 1f65da8..64d9f3b 100644 --- a/apps/podcast/views.py +++ b/apps/podcast/views.py @@ -284,11 +284,24 @@ class UserPlaylistListAPIView(generics.ListAPIView): status=True ).values_list('podcast_id', flat=True) - return Podcast.objects.filter( + queryset = Podcast.objects.filter( id__in=user_playlist_podcasts, status=True ).order_by('-created_at') + # Filter by bookmarks if provided + is_bookmark = self.request.query_params.get('is_bookmark', '').lower() + if is_bookmark == 'true' and self.request.user.is_authenticated: + from apps.bookmark.models.bookmark import Bookmark + bookmarked_ids = Bookmark.objects.filter( + user=self.request.user, + service=Bookmark.ServiceChoices.PODCAST, + status=True + ).values_list('content_id', flat=True) + queryset = queryset.filter(id__in=bookmarked_ids) + + return queryset + class UserPlaylistCreateAPIView(generics.CreateAPIView): """ diff --git a/apps/video/views.py b/apps/video/views.py index 8358323..54a2945 100644 --- a/apps/video/views.py +++ b/apps/video/views.py @@ -291,14 +291,14 @@ class VideoListAPIView(generics.ListAPIView): # Import Bookmark model here to avoid circular imports from apps.bookmark.models import Bookmark - # Get all bookmarked playlist IDs for the current user + # Get all bookmarked video IDs for the current user bookmarked_ids = Bookmark.objects.filter( user=self.request.user, - service=Bookmark.ServiceChoices.VIDEO_PLAYLIST, + service=Bookmark.ServiceChoices.VIDEO, status=True ).values_list('content_id', flat=True) - # Filter playlists by these IDs + # Filter videos by these IDs queryset = queryset.filter(id__in=bookmarked_ids) sort = self.request.query_params.get('sort', '-created_at') allowed_sorts = [