Browse Source

feat(library): add endpoint for listing downloaded books and remove filtering from book list

- Introduced a new `DownloadedBooksListView` to retrieve books downloaded by the current user.
- Updated `urls.py` to include the new endpoint `/books/downloaded/`.
- Removed the `is_downloaded` parameter from the `BookListView` to streamline the book listing process.
master
mortezaei 6 months ago
parent
commit
0cdc001b50
  1. 13
      apps/library/doc.py
  2. 2
      apps/library/urls.py
  3. 65
      apps/library/views.py

13
apps/library/doc.py

@ -41,14 +41,6 @@ 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,
@ -189,13 +181,14 @@ 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'.
Note: To get downloaded books, use the separate endpoint /books/downloaded/
""", """,
operation_summary="List Books", operation_summary="List Books",
tags=["Library"], tags=["Library"],
manual_parameters=[collection_id_param, middle_param, bottom_param, is_bookmark_param, is_downloaded_param, search_param],
manual_parameters=[collection_id_param, middle_param, bottom_param, is_bookmark_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.",

2
apps/library/urls.py

@ -6,6 +6,7 @@ from apps.library.views import (
MiddleBookCollectionListView, MiddleBookCollectionListView,
BookListView, BookListView,
BookDetailView, BookDetailView,
DownloadedBooksListView,
BookDownloadCreateAPIView, BookDownloadCreateAPIView,
) )
@ -15,5 +16,6 @@ urlpatterns = [
path('collections/', MiddleBookCollectionListView.as_view(), name='collection-list'), path('collections/', MiddleBookCollectionListView.as_view(), name='collection-list'),
path('books/', BookListView.as_view(), name='book-list'), path('books/', BookListView.as_view(), name='book-list'),
path('books/<int:pk>/', BookDetailView.as_view(), name='book-detail'), path('books/<int:pk>/', BookDetailView.as_view(), name='book-detail'),
path('books/downloaded/', DownloadedBooksListView.as_view(), name='downloaded-books-list'),
path('books/download/', BookDownloadCreateAPIView.as_view(), name='book-download'), path('books/download/', BookDownloadCreateAPIView.as_view(), name='book-download'),
] ]

65
apps/library/views.py

@ -137,18 +137,6 @@ 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')
@ -191,6 +179,59 @@ class MiddleBookCollectionListView(ListAPIView):
).order_by('order') ).order_by('order')
class DownloadedBooksListView(ListAPIView):
"""
API view to list books that have been downloaded by the current user
"""
serializer_class = BookSerializer
permission_classes = (IsAuthenticated,)
filter_backends = [SearchFilter]
search_fields = ['title', 'summary', 'publisher', 'isbn']
@swagger_auto_schema(
operation_id="list_downloaded_books",
operation_description="""
Retrieve a list of books that have been downloaded by the current user.
This endpoint returns a paginated list of books that the authenticated user has downloaded.
The results are not cached to ensure real-time accuracy of the download list.
You can search for downloaded books by title, summary, publisher, or ISBN using the 'search' query parameter.
""",
operation_summary="List Downloaded Books",
tags=["Library"],
manual_parameters=[
openapi.Parameter(
'search',
openapi.IN_QUERY,
description="Search downloaded books by title, summary, publisher, or ISBN",
type=openapi.TYPE_STRING,
required=False
)
],
responses={
200: "List of downloaded books with pagination",
401: "Authentication credentials were not provided or are invalid",
500: "Internal server error occurred"
}
)
def get(self, request, *args, **kwargs):
return super().get(request, *args, **kwargs)
def get_queryset(self):
# 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)
# Return books that match these IDs
return Book.objects.filter(
id__in=downloaded_ids,
status=True
).order_by('-created_at')
class BookDownloadCreateAPIView(CreateAPIView): class BookDownloadCreateAPIView(CreateAPIView):
""" """
API view to create a book download record and increment the book's download count API view to create a book download record and increment the book's download count

Loading…
Cancel
Save