You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
90 lines
2.9 KiB
90 lines
2.9 KiB
from rest_framework.generics import ListAPIView, RetrieveAPIView,ListCreateAPIView
|
|
from rest_framework.response import Response
|
|
from ..models import BookReference , BookAuthor , BookReferenceImage, BookAttribute
|
|
from ..serializers.reference import BookAuthorSerializer, BookDetailSerializer , BookReferenceSerializer, BookReferenceSyncSerializer, BookAttributeSerializer
|
|
from ..docs import book_attributes_create_swagger, book_attributes_list_swagger, book_references_list_swagger, book_authors_list_swagger, book_detail_swagger, reference_sync_swagger
|
|
from utils.pagination import NoPagination
|
|
|
|
|
|
|
|
class BookReferencesView(ListAPIView):
|
|
queryset = BookReference.objects.all()
|
|
serializer_class = BookReferenceSerializer
|
|
|
|
@book_references_list_swagger
|
|
def get(self, request, *args, **kwargs):
|
|
return super().get(request, *args, **kwargs)
|
|
|
|
|
|
class BookAuthorView(ListAPIView):
|
|
queryset = BookAuthor.objects.all()
|
|
serializer_class = BookAuthorSerializer
|
|
|
|
@book_authors_list_swagger
|
|
def get(self, request, *args, **kwargs):
|
|
return super().get(request, *args, **kwargs)
|
|
|
|
class BookDetailView(RetrieveAPIView):
|
|
serializer_class = BookDetailSerializer
|
|
lookup_field = 'slug'
|
|
lookup_url_kwarg = 'reference_slug'
|
|
|
|
@book_detail_swagger
|
|
def get(self, request, *args, **kwargs):
|
|
return super().get(request, *args, **kwargs)
|
|
|
|
def get_queryset(self):
|
|
return BookReference.objects.filter(slug = self.kwargs.get('reference_slug'))
|
|
# .prefetch_related(
|
|
# 'authors__name',
|
|
# 'images__image',
|
|
# )
|
|
|
|
|
|
from django.db.models import Prefetch
|
|
|
|
class BookReferenceSyncView(ListAPIView):
|
|
"""
|
|
API view to sync all book reference data for offline mode
|
|
Returns all book references with basic info, detailed information, and related hadises
|
|
"""
|
|
serializer_class = BookReferenceSyncSerializer
|
|
pagination_class = NoPagination
|
|
|
|
@reference_sync_swagger
|
|
def get(self, request, *args, **kwargs):
|
|
return super().get(request, *args, **kwargs)
|
|
|
|
def get_queryset(self):
|
|
"""
|
|
Prefetch ALL related data to avoid N+1 queries
|
|
"""
|
|
return (
|
|
BookReference.objects
|
|
.select_related() # If any ForeignKey fields
|
|
.prefetch_related(
|
|
'authors',
|
|
'attributes', # ← ADDED
|
|
'images', # ← ADDED
|
|
'hadis_references__hadis'
|
|
)
|
|
.order_by('id')
|
|
)
|
|
|
|
|
|
class BookAttributeView(ListCreateAPIView):
|
|
"""
|
|
API view to list all book attributes and create new book attributes
|
|
"""
|
|
queryset = BookAttribute.objects.all()
|
|
serializer_class = BookAttributeSerializer
|
|
|
|
@book_attributes_list_swagger
|
|
def get(self, request, *args, **kwargs):
|
|
return self.list(request, *args, **kwargs)
|
|
|
|
@book_attributes_create_swagger
|
|
def post(self, request, *args, **kwargs):
|
|
return self.create(request, *args, **kwargs)
|
|
|
|
|