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.
414 lines
15 KiB
414 lines
15 KiB
from rest_framework.generics import ListAPIView, RetrieveAPIView
|
|
from django.shortcuts import get_object_or_404
|
|
from utils.pagination import NoPagination, StandardResultsSetPagination
|
|
from rest_framework.pagination import PageNumberPagination
|
|
from rest_framework.response import Response
|
|
from django.db.models import Count
|
|
from django.db.models import Prefetch
|
|
from ..serializers.category import get_localized_text
|
|
|
|
from ..models import HadisCategory, Hadis, HadisCollection,HadisTransmitter , HadisCorrection ,HadisReference, HadisStatus ,ReferenceImage
|
|
from ..serializers import HadisListSerializer, HadisBasicSerializer, HadisDetailSerializer, HadisCollectionListSerializer, HadisSyncSerializer,HadisCorrectionSerializer,HadisTransmitterListSerializer
|
|
from ..docs import arguments_filters_swagger ,hadis_list_swagger, hadis_detail_swagger, hadis_collections_swagger, hadis_sync_swagger, hadis_transmitters_swagger, hadis_corrections_swagger, hadis_basic_swagger, hadis_main_list_swagger
|
|
|
|
|
|
class HadisCollectionListView(ListAPIView):
|
|
"""
|
|
API view to list all hadis collections
|
|
"""
|
|
queryset = HadisCollection.objects.filter(status=True).order_by('order')
|
|
serializer_class = HadisCollectionListSerializer
|
|
pagination_class = NoPagination
|
|
|
|
@hadis_collections_swagger
|
|
def get(self, request, *args, **kwargs):
|
|
return self.list(request, *args, **kwargs)
|
|
|
|
|
|
class HadisSyncView(ListAPIView):
|
|
"""
|
|
API view to sync all hadis data for offline mode
|
|
"""
|
|
serializer_class = HadisSyncSerializer
|
|
pagination_class = NoPagination
|
|
|
|
def get_queryset(self):
|
|
return (
|
|
Hadis.objects
|
|
.filter(status=True)
|
|
.select_related('category', 'hadis_status')
|
|
.prefetch_related(
|
|
'tags',
|
|
# 1. OPTIMIZED TRANSMITTERS
|
|
Prefetch(
|
|
'transmitters',
|
|
queryset=HadisTransmitter.objects.select_related(
|
|
'transmitter',
|
|
'transmitter__reliability', # <--- Fixes N+1 for reliability text
|
|
'narrator_layer'
|
|
).order_by('order')
|
|
),
|
|
# 2. OPTIMIZED REFERENCES
|
|
Prefetch(
|
|
'references',
|
|
queryset=HadisReference.objects
|
|
.select_related('book_reference')
|
|
.prefetch_related(
|
|
'book_reference__authors',
|
|
# <--- Fixes N+1 for Image ordering
|
|
Prefetch(
|
|
'images',
|
|
queryset=ReferenceImage.objects.order_by('priority')
|
|
)
|
|
)
|
|
),
|
|
'hadiscorrection_set',
|
|
)
|
|
.order_by('id')
|
|
)
|
|
|
|
@hadis_sync_swagger
|
|
def get(self, request, *args, **kwargs):
|
|
return self.list(request, *args, **kwargs)
|
|
|
|
def list(self, request, *args, **kwargs):
|
|
queryset = self.get_queryset()
|
|
serializer = self.get_serializer(queryset, many=True)
|
|
|
|
response_data = {
|
|
'count': queryset.count(),
|
|
'results': serializer.data
|
|
}
|
|
|
|
return Response(response_data)
|
|
|
|
|
|
class HadisListView(ListAPIView):
|
|
"""
|
|
API view to list Hadis by category_id
|
|
"""
|
|
serializer_class = HadisListSerializer
|
|
pagination_class = StandardResultsSetPagination
|
|
|
|
@hadis_list_swagger
|
|
def get(self, request, *args, **kwargs):
|
|
return self.list(request, *args, **kwargs)
|
|
|
|
def get_queryset(self):
|
|
category_slug = self.kwargs.get('category_slug')
|
|
if not HadisCategory.objects.filter(slug=category_slug).exists():
|
|
return Hadis.objects.none()
|
|
|
|
return Hadis.objects.filter(
|
|
category__slug=category_slug,
|
|
status=True
|
|
).order_by('number').annotate(
|
|
# distinct=True is CRITICAL here.
|
|
# Without it, if 3 narrators are from "Layer 1", it counts as 3.
|
|
# With it, it counts as 1 (unique layer).
|
|
layer_count=Count('transmitters__narrator_layer', distinct=True)
|
|
).select_related('category')
|
|
|
|
|
|
class HadisMainListView(ListAPIView):
|
|
"""
|
|
API view to list Hadis by category_id
|
|
"""
|
|
serializer_class = HadisListSerializer
|
|
pagination_class = StandardResultsSetPagination
|
|
|
|
@hadis_main_list_swagger
|
|
def get(self, request, *args, **kwargs):
|
|
return self.list(request, *args, **kwargs)
|
|
|
|
def get_queryset(self):
|
|
# queryset = Hadis.objects.select_related('category', 'hadis_status')
|
|
queryset = Hadis.objects.select_related('category__sect', 'hadis_status')
|
|
|
|
# Get search parameters
|
|
search_query = self.request.query_params.get('search', None)
|
|
status_filter = self.request.query_params.get('status', None)
|
|
category_filter = self.request.query_params.get('category', None)
|
|
|
|
# Apply search filter
|
|
if search_query:
|
|
queryset = self.apply_search_filter(queryset, search_query)
|
|
|
|
# Apply status filter
|
|
if status_filter:
|
|
queryset = queryset.filter(hadis_status__title__icontains=status_filter)
|
|
|
|
# Apply category filter
|
|
if category_filter:
|
|
queryset = queryset.filter(category__title__icontains=category_filter)
|
|
|
|
return queryset
|
|
|
|
def list(self, request, *args, **kwargs):
|
|
queryset = self.get_queryset()
|
|
|
|
# Apply pagination
|
|
page = self.paginate_queryset(queryset)
|
|
if page is not None:
|
|
serializer = self.get_serializer(page, many=True)
|
|
paginated_response = self.get_paginated_response(serializer.data)
|
|
|
|
# Get category titles
|
|
category_titles = self.get_category_titles(request)
|
|
|
|
# Get status titles
|
|
status_titles = self.get_status_titles(request)
|
|
|
|
# Modify the paginated response to include our custom data
|
|
response_data = paginated_response.data
|
|
# response_data['category_titles'] = self.get_cached_category_titles(request)
|
|
# response_data['status_titles'] = self.get_cached_status_titles(request)
|
|
|
|
return Response(response_data)
|
|
|
|
# Fallback for when pagination is disabled
|
|
serializer = self.get_serializer(queryset, many=True)
|
|
return Response({
|
|
'count': queryset.count(),
|
|
'results': serializer.data
|
|
})
|
|
|
|
return Response(response_data)
|
|
|
|
def get_category_titles(self,request):
|
|
"""Get list of category titles based on language"""
|
|
from ..models import HadisCategory
|
|
|
|
categories = HadisCategory.objects.all()
|
|
category_titles = []
|
|
|
|
for category in categories:
|
|
title = get_localized_text(category.title,request)
|
|
category_titles.append(title)
|
|
|
|
return category_titles
|
|
|
|
def get_status_titles(self, request):
|
|
"""Get list of status titles based on language"""
|
|
from ..models import HadisStatus
|
|
|
|
statuses = HadisStatus.objects.all().order_by('order')
|
|
status_titles = []
|
|
|
|
for status in statuses:
|
|
title = get_localized_text(status.title,request)
|
|
status_titles.append(title)
|
|
return status_titles
|
|
|
|
def apply_search_filter(self, queryset, search_query):
|
|
"""
|
|
Apply search filter across multiple fields including JSONFields.
|
|
Searches in: title, title_narrator, text, translation
|
|
"""
|
|
from django.db.models import Q
|
|
|
|
# Basic search conditions
|
|
search_conditions = Q(text__icontains=search_query)
|
|
|
|
# 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)
|
|
search_conditions |= Q(title_narrator__icontains=search_query)
|
|
search_conditions |= Q(translation__icontains=search_query)
|
|
|
|
return queryset.filter(search_conditions)
|
|
|
|
#we add this later
|
|
|
|
# def get_cached_category_titles(self, request):
|
|
# """Fetches categories, cached for 1 hour to reduce DB load"""
|
|
# lang = getattr(request, "LANGUAGE_CODE", "en")
|
|
# cache_key = f"hadis_meta_categories_{lang}"
|
|
|
|
# data = cache.get(cache_key)
|
|
# if not data:
|
|
# # If not in cache, fetch from DB
|
|
# from ..models import HadisCategory
|
|
# categories = HadisCategory.objects.all().only('id', 'title') # Fetch only needed fields
|
|
|
|
# # Build list
|
|
# data = [get_localized_text(c.title, request) for c in categories]
|
|
|
|
# # Save to cache
|
|
# cache.set(cache_key, data, timeout=60 * 60) # 1 Hour
|
|
|
|
# return data
|
|
|
|
# def get_cached_status_titles(self, request):
|
|
# """Fetches statuses, cached for 1 hour"""
|
|
# lang = getattr(request, "LANGUAGE_CODE", "en")
|
|
# cache_key = f"hadis_meta_statuses_{lang}"
|
|
|
|
# data = cache.get(cache_key)
|
|
# if not data:
|
|
# from ..models import HadisStatus
|
|
# statuses = HadisStatus.objects.all().order_by('order')
|
|
# data = [get_localized_text(s.title, request) for s in statuses]
|
|
# cache.set(cache_key, data, timeout=60 * 60)
|
|
|
|
# return data
|
|
|
|
|
|
class HadisBasicView(RetrieveAPIView):
|
|
"""
|
|
API view to retrieve basic Hadis information by hadis_slug
|
|
"""
|
|
serializer_class = HadisBasicSerializer
|
|
lookup_field = 'slug'
|
|
lookup_url_kwarg = 'hadis_slug'
|
|
|
|
@hadis_basic_swagger
|
|
def get(self, request, *args, **kwargs):
|
|
return self.retrieve(request, *args, **kwargs)
|
|
|
|
def get_queryset(self):
|
|
return Hadis.objects.filter(status=True)
|
|
|
|
|
|
class HadisDetailView(RetrieveAPIView):
|
|
"""
|
|
API view to retrieve detailed Hadis information by hadis_slug (excluding transmitters and corrections)
|
|
"""
|
|
serializer_class = HadisDetailSerializer
|
|
lookup_field = 'slug'
|
|
lookup_url_kwarg = 'hadis_slug'
|
|
|
|
@hadis_detail_swagger
|
|
def get(self, request, *args, **kwargs):
|
|
return self.retrieve(request, *args, **kwargs)
|
|
|
|
def get_queryset(self):
|
|
return Hadis.objects.filter(status=True).select_related(
|
|
'category', 'hadis_status'
|
|
).prefetch_related(
|
|
'tags',
|
|
'references__book_reference__title',
|
|
'references__book_reference__images',
|
|
'references__book_reference__authors',
|
|
'references__book_reference__id',
|
|
'references__book_reference__description',
|
|
)
|
|
|
|
class HadisTransmittersView(RetrieveAPIView):
|
|
"""
|
|
Fetches a single Hadis but filters the nested Transmitters list
|
|
if a ?layer=slug param is provided.
|
|
"""
|
|
serializer_class = HadisTransmitterListSerializer
|
|
lookup_field = 'slug'
|
|
lookup_url_kwarg = 'hadis_slug'
|
|
|
|
@hadis_transmitters_swagger
|
|
def get(self, request, *args, **kwargs):
|
|
return self.retrieve(request, *args, **kwargs)
|
|
|
|
def get_queryset(self):
|
|
# 1. Get the filter param
|
|
layer_slug = self.request.query_params.get('layer')
|
|
|
|
# 2. Build the query for the "Child" (Transmitters)
|
|
# We start with the base optimization (select_related)
|
|
transmitter_qs = HadisTransmitter.objects.select_related(
|
|
'transmitter',
|
|
'narrator_layer'
|
|
).order_by('order')
|
|
|
|
# 3. Apply the filter to the Child Query (if param exists)
|
|
if layer_slug:
|
|
# Assumes 'NarratorLayer' has a 'slug' field.
|
|
# If not, use 'narrator_layer__name' or 'narrator_layer__id'.
|
|
transmitter_qs = transmitter_qs.filter(narrator_layer__slug=layer_slug)
|
|
|
|
# 4. Use the Prefetch object to inject this filtered list into the Parent
|
|
return Hadis.objects.filter(status=True).prefetch_related(
|
|
Prefetch('transmitters', queryset=transmitter_qs)
|
|
)
|
|
|
|
class HadisCorrectionsView(ListAPIView):
|
|
"""
|
|
API view to retrieve corrections for a specific hadis
|
|
"""
|
|
serializer_class = HadisCorrectionSerializer
|
|
pagination_class = StandardResultsSetPagination
|
|
lookup_field = 'slug'
|
|
lookup_url_kwarg = 'hadis_slug'
|
|
|
|
@hadis_corrections_swagger
|
|
def get(self, request, *args, **kwargs):
|
|
return self.list(request, *args, **kwargs)
|
|
# hadis = self.get_object()
|
|
# corrections_data = []
|
|
|
|
# for correction in hadis.hadiscorrection_set.all():
|
|
# correction_info = {
|
|
# 'id': correction.id,
|
|
# 'title': correction.title,
|
|
# 'description': correction.description,
|
|
# 'translation': correction.translation
|
|
# }
|
|
# corrections_data.append(correction_info)
|
|
|
|
# return Response({
|
|
# 'hadis_id': hadis.id,
|
|
# 'corrections_count': len(corrections_data),
|
|
# 'corrections': corrections_data
|
|
# })
|
|
|
|
def get_queryset(self):
|
|
hadis_slug = self.kwargs.get('hadis_slug')
|
|
try:
|
|
hadis = Hadis.objects.get(slug=hadis_slug, status=True)
|
|
if not HadisCorrection.objects.filter(hadis=hadis).exists():
|
|
return Hadis.objects.none()
|
|
return HadisCorrection.objects.filter(hadis=hadis)
|
|
except Hadis.DoesNotExist:
|
|
return HadisCorrection.objects.none()
|
|
|
|
|
|
class HadisFiltersView(ListAPIView):
|
|
"""
|
|
API view to return filter data for hadis
|
|
Returns statuses and categories for filtering
|
|
"""
|
|
pagination_class = NoPagination
|
|
|
|
@arguments_filters_swagger
|
|
def get(self, request, *args, **kwargs):
|
|
return self.list(request, *args, **kwargs)
|
|
|
|
def get_queryset(self):
|
|
# This view doesn't need a queryset, it returns computed data
|
|
return Hadis.objects.none()
|
|
|
|
def list(self, request, *args, **kwargs):
|
|
# Get statuses from HadisStatus model
|
|
statuses = []
|
|
for status in HadisStatus.objects.all().order_by('order'):
|
|
title_text = get_localized_text(status.title, request)
|
|
if title_text and status.slug:
|
|
statuses.append({
|
|
'text': title_text,
|
|
'slug': status.slug
|
|
})
|
|
|
|
# Get categories from HadisCategory model
|
|
categories = []
|
|
for category in HadisCategory.objects.all().order_by('order'):
|
|
title_text = get_localized_text(category.title, request)
|
|
if title_text and category.slug:
|
|
categories.append({
|
|
'text': title_text,
|
|
'slug': category.slug
|
|
})
|
|
|
|
response_data = {
|
|
'statuses': statuses,
|
|
'categories': categories
|
|
}
|
|
|
|
return Response(response_data)
|