From 97239bd1e230492e8ac01fdbb780cf88d9383493 Mon Sep 17 00:00:00 2001 From: mortezaei Date: Sat, 8 Nov 2025 14:37:47 +0330 Subject: [PATCH] feat(bookmark): streamline bookmark creation and add downloaded book filtering - Simplified the bookmark creation process by using request data directly in the serializer. - Introduced a new `perform_create` method to save the user context during bookmark creation. - Enhanced the book listing functionality to filter books by download status for the current user, using a new query parameter `is_downloaded`. --- apps/bookmark/views/bookmark.py | 10 ++++------ apps/library/doc.py | 11 ++++++++++- apps/library/views.py | 12 ++++++++++++ 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/apps/bookmark/views/bookmark.py b/apps/bookmark/views/bookmark.py index 1a0da3e..59904ca 100644 --- a/apps/bookmark/views/bookmark.py +++ b/apps/bookmark/views/bookmark.py @@ -57,16 +57,14 @@ class AddBookmarkView(CreateAPIView): return Response(serializer.data, status=status.HTTP_200_OK) # Create a new bookmark - serializer = self.get_serializer(data={ - 'user': request.user.id, - 'service': service, - 'content_id': content_id, - 'status': True - }) + serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) self.perform_create(serializer) headers = self.get_success_headers(serializer.data) return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers) + + def perform_create(self, serializer): + serializer.save(user=self.request.user) class RemoveBookmarkView(DestroyAPIView): diff --git a/apps/library/doc.py b/apps/library/doc.py index 890a9a1..3f4b106 100644 --- a/apps/library/doc.py +++ b/apps/library/doc.py @@ -41,6 +41,14 @@ is_bookmark_param = openapi.Parameter( required=False ) +is_downloaded_param = openapi.Parameter( + 'is_downloaded', + openapi.IN_QUERY, + description="Filter books that are downloaded by the current user (set to 'true' to enable)", + type=openapi.TYPE_BOOLEAN, + required=False +) + search_param = openapi.Parameter( 'search', openapi.IN_QUERY, @@ -181,12 +189,13 @@ book_list_swagger = swagger_auto_schema( - Middle section collection using the query parameter 'middle' - Bottom section collection using the query parameter 'bottom' - Bookmarked books using the query parameter 'is_bookmark=true' + - Downloaded books using the query parameter 'is_downloaded=true' You can also search for books by title, summary, or author using the query parameter 'search'. """, operation_summary="List Books", tags=["Library"], - manual_parameters=[collection_id_param, middle_param, bottom_param, is_bookmark_param, search_param], + manual_parameters=[collection_id_param, middle_param, bottom_param, is_bookmark_param, is_downloaded_param, search_param], responses={ 200: books_response, 401: "Authentication credentials were not provided or are invalid.", diff --git a/apps/library/views.py b/apps/library/views.py index c13f010..8cb62a8 100644 --- a/apps/library/views.py +++ b/apps/library/views.py @@ -137,6 +137,18 @@ class BookListView(ListAPIView): # Filter books by these IDs queryset = queryset.filter(id__in=bookmarked_ids) + # Filter by downloaded books if requested + is_downloaded = self.request.query_params.get('is_downloaded', '').lower() + if is_downloaded == 'true': + # Get all downloaded book IDs for the current user + downloaded_ids = BookDownload.objects.filter( + user=self.request.user, + status=True + ).values_list('book_id', flat=True) + + # Filter books by these IDs + queryset = queryset.filter(id__in=downloaded_ids) + return queryset.order_by('-pin', '-created_at')