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.
125 lines
4.2 KiB
125 lines
4.2 KiB
from rest_framework.authentication import TokenAuthentication
|
|
from rest_framework.permissions import IsAuthenticated
|
|
from rest_framework.generics import ListAPIView, RetrieveAPIView,ListCreateAPIView
|
|
from rest_framework.response import Response
|
|
from django.db.models import Q
|
|
from ..models import BookReference , BookAuthor , BookReferenceImage, BookAttribute
|
|
from ..serializers.reference import BookAuthorSerializer, BookDetailSerializer , BookReferenceSerializer, BookReferenceSyncSerializer, BookAttributeSerializer
|
|
from ..serializers.category import get_localized_text
|
|
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
|
|
from utils.pagination import StandardResultsSetPagination
|
|
|
|
|
|
|
|
class BookReferencesView(ListAPIView):
|
|
queryset = BookReference.objects.all()
|
|
serializer_class = BookReferenceSerializer
|
|
pagination_class = StandardResultsSetPagination
|
|
@book_references_list_swagger
|
|
def get(self, request, *args, **kwargs):
|
|
return super().get(request, *args, **kwargs)
|
|
|
|
def get_queryset(self):
|
|
queryset = BookReference.objects.all()
|
|
|
|
# Get search parameter
|
|
search_query = self.request.query_params.get('search', None)
|
|
|
|
# Apply search filter
|
|
if search_query:
|
|
queryset = self.apply_search_filter(queryset, search_query)
|
|
|
|
return queryset
|
|
|
|
def apply_search_filter(self, queryset, search_query):
|
|
"""
|
|
Apply search filter across book titles (JSONField).
|
|
Searches in: title
|
|
"""
|
|
# Search conditions
|
|
search_conditions = Q()
|
|
|
|
# For JSONFields, search in the JSON string representation
|
|
# This will find matches in the "text" values within the JSON arrays
|
|
search_conditions |= Q(title__icontains=search_query)
|
|
|
|
return queryset.filter(search_conditions)
|
|
|
|
|
|
class BookAuthorView(ListAPIView):
|
|
queryset = BookAuthor.objects.all()
|
|
serializer_class = BookAuthorSerializer
|
|
pagination_class = StandardResultsSetPagination
|
|
|
|
|
|
@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'
|
|
permission_classes = (IsAuthenticated,)
|
|
authentication_classes = [TokenAuthentication]
|
|
|
|
@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)
|
|
|
|
|