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.
187 lines
6.6 KiB
187 lines
6.6 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(
|
|
'attributes',
|
|
'authors',
|
|
'images',
|
|
'hadis_references__hadis'
|
|
)
|
|
)
|
|
|
|
|
|
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
|
|
"""
|
|
qs = BookReference.objects.select_related().prefetch_related(
|
|
'authors', 'attributes', 'images', 'hadis_references__hadis'
|
|
).distinct().order_by('id')
|
|
|
|
category = self.request.query_params.get('category')
|
|
if category:
|
|
qs = qs.filter(subject_area__id=category)
|
|
|
|
author = self.request.query_params.get('author')
|
|
if author:
|
|
qs = qs.filter(authors__id=author)
|
|
|
|
sect = self.request.query_params.get('sect')
|
|
if sect:
|
|
qs = qs.filter(type__id=sect)
|
|
|
|
pdf_available = self.request.query_params.get('pdf_available')
|
|
if pdf_available == 'true':
|
|
qs = qs.filter(documents__isnull=False)
|
|
elif pdf_available == 'false':
|
|
qs = qs.filter(documents__isnull=True)
|
|
|
|
century = self.request.query_params.get('century')
|
|
if century:
|
|
if century == 'before_islam':
|
|
qs = qs.filter(authors__death_year_hijri__lt=0)
|
|
elif century.isdigit():
|
|
century_num = int(century)
|
|
start_year = (century_num - 1) * 100 + 1
|
|
end_year = century_num * 100
|
|
qs = qs.filter(authors__death_year_hijri__gte=start_year, authors__death_year_hijri__lte=end_year)
|
|
|
|
return qs
|
|
|
|
def list(self, request, *args, **kwargs):
|
|
queryset = self.filter_queryset(self.get_queryset())
|
|
|
|
# If pagination is active, paginate it (though it's NoPagination)
|
|
page = self.paginate_queryset(queryset)
|
|
if page is not None:
|
|
serializer = self.get_serializer(page, many=True)
|
|
else:
|
|
serializer = self.get_serializer(queryset, many=True)
|
|
|
|
from ..models import BookSubjectArea, BookType
|
|
from ..serializers.category import get_localized_text
|
|
|
|
categories_qs = BookSubjectArea.objects.all()
|
|
categories = [{"id": c.id, "title": get_localized_text(c.title, request)} for c in categories_qs]
|
|
|
|
sects_qs = BookType.objects.all()
|
|
sects = [{"id": s.id, "title": get_localized_text(s.title, request)} for s in sects_qs]
|
|
|
|
filters_metadata = {
|
|
"categories": categories,
|
|
"madhab": sects
|
|
}
|
|
|
|
if page is not None:
|
|
response = self.get_paginated_response(serializer.data)
|
|
response.data['filters'] = filters_metadata
|
|
return response
|
|
|
|
return Response({
|
|
"count": queryset.count(),
|
|
"filters": filters_metadata,
|
|
"results": serializer.data
|
|
})
|
|
|
|
|
|
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)
|
|
|
|
|