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.
86 lines
2.9 KiB
86 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_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 = 'id'
|
|
lookup_url_kwarg = 'bookreference_id'
|
|
|
|
@book_detail_swagger
|
|
def get(self, request, *args, **kwargs):
|
|
return super().get(request, *args, **kwargs)
|
|
|
|
def get_queryset(self):
|
|
return BookReference.objects.filter(id = self.kwargs['bookreference_id']).prefetch_related(
|
|
'authors__name',
|
|
'images__image',
|
|
)
|
|
|
|
|
|
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):
|
|
return BookReference.objects.prefetch_related(
|
|
'authors',
|
|
'hadis_references__hadis'
|
|
).order_by('id')
|
|
|
|
# def list(self, request, *args, **kwargs):
|
|
# queryset = self.get_queryset()
|
|
# serializer = self.get_serializer(queryset, many=True, context={'request': request})
|
|
|
|
# # Group book references by ID for easy lookup
|
|
# grouped_data = {}
|
|
# for book_data in serializer.data:
|
|
# book_id = str(book_data['id'])
|
|
# grouped_data[book_id] = book_data
|
|
|
|
# response_data = {
|
|
# 'count': queryset.count(),
|
|
# 'results': grouped_data
|
|
# }
|
|
|
|
# return Response(response_data)
|
|
|
|
|
|
|
|
class BookAttributeView(ListCreateAPIView):
|
|
"""
|
|
API view to list all book attributes and create new book attributes
|
|
"""
|
|
queryset = BookAttribute.objects.all()
|
|
serializer_class = BookAttributeSerializer
|
|
|