Browse Source

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`.
master
mortezaei 6 months ago
parent
commit
97239bd1e2
  1. 10
      apps/bookmark/views/bookmark.py
  2. 11
      apps/library/doc.py
  3. 12
      apps/library/views.py

10
apps/bookmark/views/bookmark.py

@ -57,17 +57,15 @@ class AddBookmarkView(CreateAPIView):
return Response(serializer.data, status=status.HTTP_200_OK) return Response(serializer.data, status=status.HTTP_200_OK)
# Create a new bookmark # 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) serializer.is_valid(raise_exception=True)
self.perform_create(serializer) self.perform_create(serializer)
headers = self.get_success_headers(serializer.data) headers = self.get_success_headers(serializer.data)
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers) 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): class RemoveBookmarkView(DestroyAPIView):
""" """

11
apps/library/doc.py

@ -41,6 +41,14 @@ is_bookmark_param = openapi.Parameter(
required=False 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_param = openapi.Parameter(
'search', 'search',
openapi.IN_QUERY, openapi.IN_QUERY,
@ -181,12 +189,13 @@ book_list_swagger = swagger_auto_schema(
- Middle section collection using the query parameter 'middle' - Middle section collection using the query parameter 'middle'
- Bottom section collection using the query parameter 'bottom' - Bottom section collection using the query parameter 'bottom'
- Bookmarked books using the query parameter 'is_bookmark=true' - 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'. You can also search for books by title, summary, or author using the query parameter 'search'.
""", """,
operation_summary="List Books", operation_summary="List Books",
tags=["Library"], 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={ responses={
200: books_response, 200: books_response,
401: "Authentication credentials were not provided or are invalid.", 401: "Authentication credentials were not provided or are invalid.",

12
apps/library/views.py

@ -137,6 +137,18 @@ class BookListView(ListAPIView):
# Filter books by these IDs # Filter books by these IDs
queryset = queryset.filter(id__in=bookmarked_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') return queryset.order_by('-pin', '-created_at')

Loading…
Cancel
Save