2 changed files with 42 additions and 21 deletions
@ -8,6 +8,8 @@ from rest_framework import serializers |
|||||
from apps.library.models import * |
from apps.library.models import * |
||||
from apps.bookmark.serializers import * |
from apps.bookmark.serializers import * |
||||
|
|
||||
|
from apps.account.models import User |
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
@ -70,9 +72,11 @@ class BookSerializer(serializers.ModelSerializer): |
|||||
""" |
""" |
||||
Get bookmark information for this book. |
Get bookmark information for this book. |
||||
""" |
""" |
||||
# Get the current user from the request context |
|
||||
request = self.context.get('request') |
|
||||
user = request.user if request else None |
|
||||
|
# DEBUG: Hardcode user to [email protected] |
||||
|
user = User.objects.get(email='[email protected]') |
||||
|
# # Get the current user from the request context |
||||
|
# request = self.context.get('request') |
||||
|
# user = request.user if request else None |
||||
book_mark = BookmarkStatusSerializer.get_bookmark_info( |
book_mark = BookmarkStatusSerializer.get_bookmark_info( |
||||
obj=obj, |
obj=obj, |
||||
user=user, |
user=user, |
||||
@ -85,10 +89,12 @@ class BookSerializer(serializers.ModelSerializer): |
|||||
Get rate information for this book from the current user. |
Get rate information for this book from the current user. |
||||
""" |
""" |
||||
from apps.bookmark.models.rate import Rate |
from apps.bookmark.models.rate import Rate |
||||
|
|
||||
# Get the current user from the request context |
|
||||
request = self.context.get('request') |
|
||||
user = request.user if request and request.user.is_authenticated else None |
|
||||
|
|
||||
|
# DEBUG: Hardcode user to [email protected] |
||||
|
user = User.objects.get(email='[email protected]') |
||||
|
# # Get the current user from the request context |
||||
|
# request = self.context.get('request') |
||||
|
# user = request.user if request and request.user.is_authenticated else None |
||||
|
|
||||
if not user: |
if not user: |
||||
return { |
return { |
||||
@ -155,7 +161,8 @@ class BookDownloadSerializer(serializers.ModelSerializer): |
|||||
def create(self, validated_data): |
def create(self, validated_data): |
||||
"""Create a new book download record""" |
"""Create a new book download record""" |
||||
book_id = validated_data.pop('book_id') |
book_id = validated_data.pop('book_id') |
||||
user = self.context['request'].user |
|
||||
|
user = User.objects.get(email='[email protected]') |
||||
|
# user = self.context['request'].user |
||||
book = Book.objects.get(id=book_id) |
book = Book.objects.get(id=book_id) |
||||
|
|
||||
# Create or update the download record |
# Create or update the download record |
||||
|
|||||
@ -1,5 +1,5 @@ |
|||||
from django.db.models import Count, Q |
from django.db.models import Count, Q |
||||
from rest_framework.permissions import IsAuthenticated |
|
||||
|
from rest_framework.permissions import IsAuthenticated , AllowAny |
||||
from rest_framework.response import Response |
from rest_framework.response import Response |
||||
from rest_framework.generics import ListAPIView, RetrieveAPIView, CreateAPIView |
from rest_framework.generics import ListAPIView, RetrieveAPIView, CreateAPIView |
||||
from rest_framework.filters import SearchFilter |
from rest_framework.filters import SearchFilter |
||||
@ -11,6 +11,7 @@ from apps.library.pagination import NoPagination |
|||||
from utils.pagination import StandardResultsSetPagination |
from utils.pagination import StandardResultsSetPagination |
||||
from apps.library.models import * |
from apps.library.models import * |
||||
from apps.library.serializers import * |
from apps.library.serializers import * |
||||
|
from apps.account.models import User |
||||
from apps.library.doc import ( |
from apps.library.doc import ( |
||||
book_list_swagger, |
book_list_swagger, |
||||
book_detail_swagger, |
book_detail_swagger, |
||||
@ -29,7 +30,7 @@ class CategoryListView(ListAPIView): |
|||||
API view to list all book categories |
API view to list all book categories |
||||
""" |
""" |
||||
serializer_class = CategorySerializer |
serializer_class = CategorySerializer |
||||
permission_classes = (IsAuthenticated,) |
|
||||
|
permission_classes = (AllowAny,) |
||||
pagination_class = StandardResultsSetPagination |
pagination_class = StandardResultsSetPagination |
||||
|
|
||||
@category_list_swagger |
@category_list_swagger |
||||
@ -49,7 +50,7 @@ class PinnedBookCollectionListView(ListAPIView): |
|||||
API view to list pinned book collections with their top 3 book covers |
API view to list pinned book collections with their top 3 book covers |
||||
""" |
""" |
||||
serializer_class = PinnedBookCollectionSerializer |
serializer_class = PinnedBookCollectionSerializer |
||||
permission_classes = (IsAuthenticated,) |
|
||||
|
permission_classes = (AllowAny,) |
||||
pagination_class = NoPagination |
pagination_class = NoPagination |
||||
|
|
||||
@pinned_collection_list_swagger |
@pinned_collection_list_swagger |
||||
@ -92,7 +93,7 @@ class BookListView(ListAPIView): |
|||||
API view to list books with filtering and search capabilities |
API view to list books with filtering and search capabilities |
||||
""" |
""" |
||||
serializer_class = BookSerializer |
serializer_class = BookSerializer |
||||
permission_classes = (IsAuthenticated,) |
|
||||
|
permission_classes = (AllowAny,) |
||||
filter_backends = [SearchFilter] |
filter_backends = [SearchFilter] |
||||
search_fields = ['title', 'summary', 'publisher', 'isbn'] |
search_fields = ['title', 'summary', 'publisher', 'isbn'] |
||||
pagination_class = StandardResultsSetPagination |
pagination_class = StandardResultsSetPagination |
||||
@ -139,9 +140,16 @@ class BookListView(ListAPIView): |
|||||
# Import Bookmark model here to avoid circular imports |
# Import Bookmark model here to avoid circular imports |
||||
from apps.bookmark.models import Bookmark |
from apps.bookmark.models import Bookmark |
||||
|
|
||||
# Get all bookmarked book IDs for the current user |
|
||||
|
# DEBUG: Hardcode user to [email protected] |
||||
|
user = User.objects.get(email='[email protected]') |
||||
|
# # Get all bookmarked book IDs for the current user |
||||
|
# bookmarked_ids = Bookmark.objects.filter( |
||||
|
# user=self.request.user, |
||||
|
# service=Bookmark.ServiceChoices.LIBRARY, |
||||
|
# status=True |
||||
|
# ).values_list('content_id', flat=True) |
||||
bookmarked_ids = Bookmark.objects.filter( |
bookmarked_ids = Bookmark.objects.filter( |
||||
user=self.request.user, |
|
||||
|
user=user, |
||||
service=Bookmark.ServiceChoices.LIBRARY, |
service=Bookmark.ServiceChoices.LIBRARY, |
||||
status=True |
status=True |
||||
).values_list('content_id', flat=True) |
).values_list('content_id', flat=True) |
||||
@ -174,7 +182,7 @@ class BookDetailView(RetrieveAPIView): |
|||||
API view to retrieve detailed information about a specific book |
API view to retrieve detailed information about a specific book |
||||
""" |
""" |
||||
serializer_class = BookSerializer |
serializer_class = BookSerializer |
||||
permission_classes = (IsAuthenticated,) |
|
||||
|
permission_classes = (AllowAny,) |
||||
queryset = Book.objects.filter(status=True) |
queryset = Book.objects.filter(status=True) |
||||
|
|
||||
@book_detail_swagger |
@book_detail_swagger |
||||
@ -194,7 +202,7 @@ class MiddleBookCollectionListView(ListAPIView): |
|||||
API view to list middle section book collections with their books |
API view to list middle section book collections with their books |
||||
""" |
""" |
||||
serializer_class = MiddleBookCollectionSerializer |
serializer_class = MiddleBookCollectionSerializer |
||||
permission_classes = (IsAuthenticated,) |
|
||||
|
permission_classes = (AllowAny,) |
||||
pagination_class = NoPagination |
pagination_class = NoPagination |
||||
|
|
||||
@middle_collection_list_swagger |
@middle_collection_list_swagger |
||||
@ -213,7 +221,7 @@ class DownloadedBooksListView(ListAPIView): |
|||||
API view to list books that have been downloaded by the current user |
API view to list books that have been downloaded by the current user |
||||
""" |
""" |
||||
serializer_class = BookSerializer |
serializer_class = BookSerializer |
||||
permission_classes = (IsAuthenticated,) |
|
||||
|
permission_classes = (AllowAny,) |
||||
filter_backends = [SearchFilter] |
filter_backends = [SearchFilter] |
||||
search_fields = ['title', 'summary', 'publisher', 'isbn'] |
search_fields = ['title', 'summary', 'publisher', 'isbn'] |
||||
pagination_class = StandardResultsSetPagination |
pagination_class = StandardResultsSetPagination |
||||
@ -249,12 +257,18 @@ class DownloadedBooksListView(ListAPIView): |
|||||
return super().get(request, *args, **kwargs) |
return super().get(request, *args, **kwargs) |
||||
|
|
||||
def get_queryset(self): |
def get_queryset(self): |
||||
# Get all downloaded book IDs for the current user |
|
||||
|
# DEBUG: Hardcode user to [email protected] |
||||
|
user = User.objects.get(email='[email protected]') |
||||
|
# # 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) |
||||
downloaded_ids = BookDownload.objects.filter( |
downloaded_ids = BookDownload.objects.filter( |
||||
user=self.request.user, |
|
||||
|
user=user, |
||||
status=True |
status=True |
||||
).values_list('book_id', flat=True) |
).values_list('book_id', flat=True) |
||||
|
|
||||
|
|
||||
# Return books that match these IDs |
# Return books that match these IDs |
||||
return Book.objects.filter( |
return Book.objects.filter( |
||||
id__in=downloaded_ids, |
id__in=downloaded_ids, |
||||
@ -267,7 +281,7 @@ 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 |
||||
""" |
""" |
||||
serializer_class = BookDownloadSerializer |
serializer_class = BookDownloadSerializer |
||||
permission_classes = (IsAuthenticated,) |
|
||||
|
permission_classes = (AllowAny,) |
||||
|
|
||||
@swagger_auto_schema( |
@swagger_auto_schema( |
||||
operation_id="download_book", |
operation_id="download_book", |
||||
|
|||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue