From 1757fbfbc332375b898277681b308ef342164489 Mon Sep 17 00:00:00 2001 From: mohsentaba Date: Sun, 1 Nov 2026 10:54:38 +0330 Subject: [PATCH] perf(hadis): optimize category hadis and corrections API endpoints and eliminate N+1 reference queries --- apps/hadis/serializers/category.py | 23 +- apps/hadis/serializers/hadis.py | 344 +++++++++++++++++------------ apps/hadis/views/hadis.py | 288 +++++++++++++++++------- 3 files changed, 429 insertions(+), 226 deletions(-) diff --git a/apps/hadis/serializers/category.py b/apps/hadis/serializers/category.py index 7eb5376..f0ff956 100644 --- a/apps/hadis/serializers/category.py +++ b/apps/hadis/serializers/category.py @@ -179,12 +179,29 @@ class SimpleCategory(serializers.ModelSerializer): fields = ['id', 'title', 'slug', 'is_topic', 'source_type', 'sect_type', 'description', 'ancestors'] def get_ancestors(self, obj): - if not obj: + if not obj or not getattr(obj, 'parent_id', None): return [] request = self.context.get('request') - ancestors_qs = obj.get_ancestors(include_self=False).select_related('sect') + ancestors_list = None + if hasattr(obj, '_state') and 'parent' in obj._state.fields_cache: + chain = [] + curr = obj.parent + complete = True + while curr is not None: + chain.append(curr) + if curr.parent_id is None: + break + if hasattr(curr, '_state') and 'parent' in curr._state.fields_cache: + curr = curr.parent + else: + complete = False + break + if complete: + ancestors_list = list(reversed(chain)) + if ancestors_list is None: + ancestors_list = obj.get_ancestors(include_self=False).select_related('sect') result = [] - for a in ancestors_qs: + for a in ancestors_list: result.append({ "id": a.id, "title": get_localized_text(a.title, request=request) or a.slug, diff --git a/apps/hadis/serializers/hadis.py b/apps/hadis/serializers/hadis.py index 0e18076..06359b8 100644 --- a/apps/hadis/serializers/hadis.py +++ b/apps/hadis/serializers/hadis.py @@ -455,8 +455,12 @@ class HadisListSerializer(serializers.ModelSerializer): def get_bookmark(self, obj): """Get bookmark information for this playlist.""" + if 'user_bookmarked_hadis_ids' in self.context: + return obj.id in self.context['user_bookmarked_hadis_ids'] request = self.context.get('request') - user = request.user if request else None + user = request.user if request else None + if not user or not getattr(user, 'is_authenticated', False): + return False book_mark = BookmarkStatusSerializer.get_bookmark_info( obj=obj, user=user, @@ -1424,66 +1428,11 @@ class HadisReferenceSerializer(serializers.ModelSerializer): 'address', 'url', 'open_book_url', 'citation_summary', 'images', 'images_count' ] - def get_edition_info(self, obj): - edition = obj.edition - if not edition and getattr(obj, "book_volume", None) and obj.book_volume.edition: - edition = obj.book_volume.edition - if not edition and getattr(obj, "book_reference", None) and hasattr(obj.book_reference, "editions"): - edition = obj.book_reference.editions.first() - if edition: - request = self.context.get("request") - pub_src = edition.publisher_item.title if (getattr(edition, "publisher_item_id", None) and edition.publisher_item and edition.publisher_item.title) else edition.publisher - pub = get_localized_text(pub_src, request) if isinstance(pub_src, list) else pub_src - ed_title = get_localized_text(edition.title, request) if isinstance(getattr(edition, "title", None), list) else getattr(edition, "title", None) - city_src = ( - edition.city_of_publication_city.title - if (getattr(edition, "city_of_publication_city_id", None) and edition.city_of_publication_city and edition.city_of_publication_city.title) - else getattr(edition, "city_of_publication", None) - ) - country_src = ( - edition.country_of_publication_country.title - if (getattr(edition, "country_of_publication_country_id", None) and edition.country_of_publication_country and edition.country_of_publication_country.title) - else getattr(edition, "country_of_publication", None) - ) - city = get_localized_text(city_src, request) if city_src else "" - country = get_localized_text(country_src, request) if country_src else "" - vol_count = edition.volumes.count() if hasattr(edition, "volumes") else 0 - num_vols = vol_count if vol_count > 0 else edition.number_of_volumes - - def _serialize_contributor(c): - name = get_localized_text(c.name, request) if isinstance(getattr(c, "name", None), list) else (getattr(c, "name", None) or "") - author_obj = getattr(c, "author", None) - author_data = None - if author_obj: - author_data = { - "id": author_obj.id, - "slug": author_obj.slug, - "name": get_localized_text(author_obj.name, request) if isinstance(author_obj.name, list) else (author_obj.name or ""), - } - return { - "id": c.id, - "name": name, - "slug": author_obj.slug if (author_obj and author_obj.slug) else getattr(c, "slug", None), - "author_id": getattr(c, "author_id", None), - "author": author_data, - } - - researchers = [_serialize_contributor(r) for r in edition.researchers.all()] if hasattr(edition, "researchers") else [] - editors = [_serialize_contributor(e) for e in edition.editors.all()] if hasattr(edition, "editors") else [] + def _get_resolved_edition(self, obj): + return _resolve_reference_edition_fast(obj) - return { - "id": edition.id, - "title": ed_title or "", - "edition_number": edition.edition_number, - "publisher": pub or "", - "publication_place": {"city": city or "", "country": country or ""}, - "year_of_publication": edition.year_of_publication or "", - "number_of_volumes": num_vols, - "source_url": getattr(edition, "source_url", None) or "", - "researchers": researchers, - "editors": editors, - } - return None + def get_edition_info(self, obj): + return _build_edition_info_dict(obj, self._get_resolved_edition(obj), self.context.get("request")) def get_volume_info(self, obj): if obj.book_volume: @@ -1544,11 +1493,7 @@ class HadisReferenceSerializer(serializers.ModelSerializer): return absolute_https_url(obj.book_volume.file.url, request) if request else absolute_https_url(obj.book_volume.file.url) if getattr(obj, 'url', None): return obj.url - edition = obj.edition - if not edition and getattr(obj, 'book_volume', None) and obj.book_volume.edition: - edition = obj.book_volume.edition - if not edition and getattr(obj, 'book_reference', None) and hasattr(obj.book_reference, 'editions'): - edition = obj.book_reference.editions.first() + edition = self._get_resolved_edition(obj) if edition and getattr(edition, 'source_url', None): return edition.source_url return None @@ -1556,11 +1501,7 @@ class HadisReferenceSerializer(serializers.ModelSerializer): def get_book(self, obj): if obj.book_reference: request = self.context.get('request') - first_ed = obj.edition - if not first_ed and getattr(obj, "book_volume", None) and obj.book_volume.edition: - first_ed = obj.book_volume.edition - if not first_ed and hasattr(obj.book_reference, "editions"): - first_ed = obj.book_reference.editions.first() + first_ed = self._get_resolved_edition(obj) pub = getattr(obj.book_reference, "publisher", None) if pub is None and first_ed: @@ -1746,8 +1687,12 @@ class HadisCorrectionSerializer(serializers.ModelSerializer): def get_bookmark(self, obj): """Get bookmark information for this correction.""" + if 'user_bookmarked_correction_ids' in self.context: + return obj.id in self.context['user_bookmarked_correction_ids'] request = self.context.get('request') user = request.user if request else None + if not user or not getattr(user, 'is_authenticated', False): + return False book_mark = BookmarkStatusSerializer.get_bookmark_info( obj=obj, user=user, @@ -2038,6 +1983,186 @@ class OriginalTextReferenceImageSerializer(serializers.ModelSerializer): # --- سریالایزرهای جامع رفرنس‌ها (شامل اطلاعات کتاب، ادیشن و ولوم) --- +_EDITION_METADATA_MEM = None +_EDITION_METADATA_LOADED_AT = 0.0 +_EDITION_SIGNALS_CONNECTED = False + + +def _invalidate_edition_metadata_cache(**kwargs): + global _EDITION_METADATA_MEM, _EDITION_METADATA_LOADED_AT + _EDITION_METADATA_MEM = None + _EDITION_METADATA_LOADED_AT = 0.0 + + +def _ensure_edition_cache_signals(): + global _EDITION_SIGNALS_CONNECTED + if _EDITION_SIGNALS_CONNECTED: + return + _EDITION_SIGNALS_CONNECTED = True + from django.db.models.signals import post_save, post_delete + from ..models import ( + BookEdition, BookVolume, BookResearcher, BookEditor, + BookAuthor, BookPublisher, City, Country + ) + for model_cls in ( + BookEdition, BookVolume, BookResearcher, BookEditor, + BookAuthor, BookPublisher, City, Country + ): + post_save.connect(_invalidate_edition_metadata_cache, sender=model_cls, dispatch_uid=f"inv_ed_save_{model_cls.__name__}") + post_delete.connect(_invalidate_edition_metadata_cache, sender=model_cls, dispatch_uid=f"inv_ed_del_{model_cls.__name__}") + + +def get_cached_edition_metadata(): + global _EDITION_METADATA_MEM, _EDITION_METADATA_LOADED_AT + _ensure_edition_cache_signals() + import time + now = time.monotonic() + if _EDITION_METADATA_MEM is not None and (now - _EDITION_METADATA_LOADED_AT) < 300.0: + return _EDITION_METADATA_MEM + + from django.db.models import Count, Prefetch + from ..models import BookEdition, BookVolume, BookResearcher, BookEditor + + editions = list( + BookEdition.objects.select_related( + 'publisher_item', + 'city_of_publication_city', + 'country_of_publication_country', + ).annotate( + _cached_vol_count=Count('volumes') + ).prefetch_related( + Prefetch('researchers', queryset=BookResearcher.objects.select_related('author')), + Prefetch('editors', queryset=BookEditor.objects.select_related('author')), + ) + ) + by_id = {} + first_by_book_id = {} + for ed in editions: + ed._cached_researchers = list(ed.researchers.all()) + ed._cached_editors = list(ed.editors.all()) + by_id[ed.id] = ed + if ed.book_reference_id and ed.book_reference_id not in first_by_book_id: + first_by_book_id[ed.book_reference_id] = ed + + book_volumes_count = dict( + BookVolume.objects.values('book_reference_id') + .annotate(cnt=Count('id')) + .values_list('book_reference_id', 'cnt') + ) + _EDITION_METADATA_MEM = { + 'by_id': by_id, + 'first_by_book_id': first_by_book_id, + 'book_volumes_count': book_volumes_count, + } + _EDITION_METADATA_LOADED_AT = now + return _EDITION_METADATA_MEM + + +def _resolve_reference_edition_fast(obj): + if hasattr(obj, '_resolved_edition_cache'): + return obj._resolved_edition_cache + meta = get_cached_edition_metadata() + by_id = meta['by_id'] + first_by_book_id = meta['first_by_book_id'] + + edition = None + ed_id = getattr(obj, 'edition_id', None) + if ed_id and ed_id in by_id: + edition = by_id[ed_id] + if not edition and getattr(obj, 'book_volume_id', None) and getattr(obj, 'book_volume', None): + bv_ed_id = getattr(obj.book_volume, 'edition_id', None) + if bv_ed_id and bv_ed_id in by_id: + edition = by_id[bv_ed_id] + if not edition and getattr(obj, 'book_reference_id', None): + edition = first_by_book_id.get(obj.book_reference_id) + + if not edition: + if getattr(obj, 'edition_id', None) and obj.edition: + edition = obj.edition + elif getattr(obj, 'book_volume', None) and obj.book_volume.edition: + edition = obj.book_volume.edition + elif getattr(obj, 'book_reference', None) and hasattr(obj.book_reference, 'editions'): + if hasattr(obj.book_reference, '_prefetched_objects_cache') and 'editions' in obj.book_reference._prefetched_objects_cache: + editions_list = list(obj.book_reference.editions.all()) + edition = editions_list[0] if editions_list else None + else: + edition = obj.book_reference.editions.first() + + obj._resolved_edition_cache = edition + return edition + + +def _build_edition_info_dict(obj, edition, request): + if hasattr(obj, '_edition_info_cache'): + return obj._edition_info_cache + if not edition: + obj._edition_info_cache = None + return None + + pub_src = edition.publisher_item.title if (getattr(edition, "publisher_item_id", None) and edition.publisher_item and edition.publisher_item.title) else edition.publisher + pub = get_localized_text(pub_src, request) if isinstance(pub_src, list) else pub_src + ed_title = get_localized_text(edition.title, request) if isinstance(getattr(edition, "title", None), list) else getattr(edition, "title", None) + city_src = ( + edition.city_of_publication_city.title + if (getattr(edition, "city_of_publication_city_id", None) and edition.city_of_publication_city and edition.city_of_publication_city.title) + else getattr(edition, "city_of_publication", None) + ) + country_src = ( + edition.country_of_publication_country.title + if (getattr(edition, "country_of_publication_country_id", None) and edition.country_of_publication_country and edition.country_of_publication_country.title) + else getattr(edition, "country_of_publication", None) + ) + city = get_localized_text(city_src, request) if city_src else "" + country = get_localized_text(country_src, request) if country_src else "" + if hasattr(edition, '_cached_vol_count'): + vol_count = edition._cached_vol_count + elif hasattr(edition, '_prefetched_objects_cache') and 'volumes' in edition._prefetched_objects_cache: + vol_count = len(edition.volumes.all()) + elif hasattr(edition, "volumes"): + vol_count = edition.volumes.count() + else: + vol_count = 0 + num_vols = vol_count if vol_count > 0 else edition.number_of_volumes + + def _serialize_contributor(c): + name = get_localized_text(c.name, request) if isinstance(getattr(c, "name", None), list) else (getattr(c, "name", None) or "") + author_obj = getattr(c, "author", None) + author_data = None + if author_obj: + author_data = { + "id": author_obj.id, + "slug": author_obj.slug, + "name": get_localized_text(author_obj.name, request) if isinstance(author_obj.name, list) else (author_obj.name or ""), + } + return { + "id": c.id, + "name": name, + "slug": author_obj.slug if (author_obj and author_obj.slug) else getattr(c, "slug", None), + "author_id": getattr(c, "author_id", None), + "author": author_data, + } + + raw_researchers = edition._cached_researchers if hasattr(edition, '_cached_researchers') else (edition.researchers.all() if hasattr(edition, "researchers") else []) + raw_editors = edition._cached_editors if hasattr(edition, '_cached_editors') else (edition.editors.all() if hasattr(edition, "editors") else []) + researchers = [_serialize_contributor(r) for r in raw_researchers] + editors = [_serialize_contributor(e) for e in raw_editors] + + result = { + "id": edition.id, + "title": ed_title or "", + "edition_number": edition.edition_number, + "publisher": pub or "", + "publication_place": {"city": city or "", "country": country or ""}, + "year_of_publication": edition.year_of_publication or "", + "number_of_volumes": num_vols, + "source_url": getattr(edition, "source_url", None) or "", + "researchers": researchers, + "editors": editors, + } + obj._edition_info_cache = result + return result + + class BaseDetailedReferenceSerializer(serializers.ModelSerializer): book_title = serializers.SerializerMethodField() book_authors = serializers.SerializerMethodField() @@ -2059,66 +2184,11 @@ class BaseDetailedReferenceSerializer(serializers.ModelSerializer): ] return [] - def get_edition_info(self, obj): - edition = obj.edition - if not edition and getattr(obj, "book_volume", None) and obj.book_volume.edition: - edition = obj.book_volume.edition - if not edition and getattr(obj, "book_reference", None) and hasattr(obj.book_reference, "editions"): - edition = obj.book_reference.editions.first() - if edition: - request = self.context.get("request") - pub_src = edition.publisher_item.title if (getattr(edition, "publisher_item_id", None) and edition.publisher_item and edition.publisher_item.title) else edition.publisher - pub = get_localized_text(pub_src, request) if isinstance(pub_src, list) else pub_src - ed_title = get_localized_text(edition.title, request) if isinstance(getattr(edition, "title", None), list) else getattr(edition, "title", None) - city_src = ( - edition.city_of_publication_city.title - if (getattr(edition, "city_of_publication_city_id", None) and edition.city_of_publication_city and edition.city_of_publication_city.title) - else getattr(edition, "city_of_publication", None) - ) - country_src = ( - edition.country_of_publication_country.title - if (getattr(edition, "country_of_publication_country_id", None) and edition.country_of_publication_country and edition.country_of_publication_country.title) - else getattr(edition, "country_of_publication", None) - ) - city = get_localized_text(city_src, request) if city_src else "" - country = get_localized_text(country_src, request) if country_src else "" - vol_count = edition.volumes.count() if hasattr(edition, "volumes") else 0 - num_vols = vol_count if vol_count > 0 else edition.number_of_volumes - - def _serialize_contributor(c): - name = get_localized_text(c.name, request) if isinstance(getattr(c, "name", None), list) else (getattr(c, "name", None) or "") - author_obj = getattr(c, "author", None) - author_data = None - if author_obj: - author_data = { - "id": author_obj.id, - "slug": author_obj.slug, - "name": get_localized_text(author_obj.name, request) if isinstance(author_obj.name, list) else (author_obj.name or ""), - } - return { - "id": c.id, - "name": name, - "slug": author_obj.slug if (author_obj and author_obj.slug) else getattr(c, "slug", None), - "author_id": getattr(c, "author_id", None), - "author": author_data, - } - - researchers = [_serialize_contributor(r) for r in edition.researchers.all()] if hasattr(edition, "researchers") else [] - editors = [_serialize_contributor(e) for e in edition.editors.all()] if hasattr(edition, "editors") else [] + def _get_resolved_edition(self, obj): + return _resolve_reference_edition_fast(obj) - return { - "id": edition.id, - "title": ed_title or "", - "edition_number": edition.edition_number, - "publisher": pub or "", - "publication_place": {"city": city or "", "country": country or ""}, - "year_of_publication": edition.year_of_publication or "", - "number_of_volumes": num_vols, - "source_url": getattr(edition, "source_url", None) or "", - "researchers": researchers, - "editors": editors, - } - return None + def get_edition_info(self, obj): + return _build_edition_info_dict(obj, self._get_resolved_edition(obj), self.context.get("request")) def get_volume_info(self, obj): if obj.book_volume: @@ -2162,11 +2232,7 @@ class DetailedCorrectionReferenceSerializer(BaseDetailedReferenceSerializer): def get_book(self, obj): if obj.book_reference: request = self.context.get('request') - first_ed = obj.edition - if not first_ed and getattr(obj, "book_volume", None) and obj.book_volume.edition: - first_ed = obj.book_volume.edition - if not first_ed and hasattr(obj.book_reference, "editions"): - first_ed = obj.book_reference.editions.first() + first_ed = self._get_resolved_edition(obj) pub = getattr(obj.book_reference, "publisher", None) if pub is None and first_ed: @@ -2181,8 +2247,8 @@ class DetailedCorrectionReferenceSerializer(BaseDetailedReferenceSerializer): num_vols = getattr(obj.book_reference, "number_of_volumes", None) if num_vols is None and first_ed: num_vols = first_ed.number_of_volumes - if num_vols is None and hasattr(obj.book_reference, "volumes"): - num_vols = obj.book_reference.volumes.count() or None + if num_vols is None and getattr(obj, 'book_reference_id', None): + num_vols = get_cached_edition_metadata()['book_volumes_count'].get(obj.book_reference_id) or None return { 'id': obj.book_reference.id, @@ -2214,11 +2280,7 @@ class DetailedCorrectionReferenceSerializer(BaseDetailedReferenceSerializer): return absolute_https_url(obj.book_volume.file.url, request) if request else absolute_https_url(obj.book_volume.file.url) if getattr(obj, 'url', None): return obj.url - edition = obj.edition - if not edition and getattr(obj, 'book_volume', None) and obj.book_volume.edition: - edition = obj.book_volume.edition - if not edition and getattr(obj, 'book_reference', None) and hasattr(obj.book_reference, 'editions'): - edition = obj.book_reference.editions.first() + edition = self._get_resolved_edition(obj) if edition and getattr(edition, 'source_url', None): return edition.source_url return None diff --git a/apps/hadis/views/hadis.py b/apps/hadis/views/hadis.py index 0e0d1d9..ab09d23 100644 --- a/apps/hadis/views/hadis.py +++ b/apps/hadis/views/hadis.py @@ -250,6 +250,55 @@ class HadisSyncView(ListAPIView): }) from ..serializers.category import CategoryQuranVerseSerializer +from django.http import Http404 +from django.db.models import Exists, OuterRef + + +_LOOKUP_STATUS_TAGS_MEM = None +_LOOKUP_STATUS_TAGS_LOADED_AT = 0.0 +_LOOKUP_SIGNALS_CONNECTED = False + + +def _invalidate_lookup_status_tags_cache(**kwargs): + global _LOOKUP_STATUS_TAGS_MEM, _LOOKUP_STATUS_TAGS_LOADED_AT + _LOOKUP_STATUS_TAGS_MEM = None + _LOOKUP_STATUS_TAGS_LOADED_AT = 0.0 + + +def get_cached_statuses_and_tags(): + global _LOOKUP_SIGNALS_CONNECTED, _LOOKUP_STATUS_TAGS_MEM, _LOOKUP_STATUS_TAGS_LOADED_AT + if not _LOOKUP_SIGNALS_CONNECTED: + _LOOKUP_SIGNALS_CONNECTED = True + from django.db.models.signals import post_save, post_delete + for model_cls in (HadisStatus, HadisTag): + post_save.connect(_invalidate_lookup_status_tags_cache, sender=model_cls, dispatch_uid=f"inv_st_{model_cls.__name__}") + post_delete.connect(_invalidate_lookup_status_tags_cache, sender=model_cls, dispatch_uid=f"inv_st_del_{model_cls.__name__}") + + import time + now = time.monotonic() + if _LOOKUP_STATUS_TAGS_MEM is not None and (now - _LOOKUP_STATUS_TAGS_LOADED_AT) < 300.0: + return _LOOKUP_STATUS_TAGS_MEM + + statuses = list(HadisStatus.objects.all().order_by('order')) + tags = list(HadisTag.objects.filter(status=True).order_by('id')) + _LOOKUP_STATUS_TAGS_MEM = (statuses, tags) + _LOOKUP_STATUS_TAGS_LOADED_AT = now + return _LOOKUP_STATUS_TAGS_MEM + + +def build_detailed_reference_prefetch(ref_model, image_model, relation_name='references'): + return Prefetch( + relation_name, + queryset=ref_model.objects.select_related( + 'book_reference', + 'book_reference__author', + 'book_volume', + ).prefetch_related( + Prefetch('images', queryset=image_model.objects.order_by('priority')), + ) + ) + + class HadisListView(ListAPIView): """ API view to list Hadis by category_id @@ -258,18 +307,33 @@ class HadisListView(ListAPIView): pagination_class = StandardResultsSetPagination authentication_classes = [TokenAuthentication] + def _get_category_obj(self): + if not hasattr(self, '_category_obj'): + category_slug = self.kwargs.get('category_slug') + self._category_obj = ( + HadisCategory.objects.select_related( + 'sect', + 'parent', + 'parent__sect', + 'parent__parent', + 'parent__parent__sect', + ).filter(slug=category_slug).first() + if category_slug else None + ) + return self._category_obj + @hadis_list_swagger def get(self, request, *args, **kwargs): return self.list(request, *args, **kwargs) def list(self, request, *args, **kwargs): + category_obj = self._get_category_obj() + if not category_obj: + raise Http404("No HadisCategory matches the given query.") + # 1. Run the standard list logic (get pagination, filter, results) response = super().list(request, *args, **kwargs) - # 2. Find the "Parent" Category based on the URL slug - category_slug = self.kwargs.get('category_slug') - category_obj = get_object_or_404(HadisCategory, slug=category_slug) - # 3. Serialize this single category for the Hero section category_data = SimpleCategory(category_obj, context={'request': request}).data @@ -294,6 +358,7 @@ class HadisListView(ListAPIView): # 👇 5. NEW LOGIC: Fetch all statuses for frontend filtering UI lang = getattr(request, "LANGUAGE_CODE", None) or "fa" + cached_statuses, cached_tags = get_cached_statuses_and_tags() statuses_list = [ { "id": status.id, @@ -303,7 +368,7 @@ class HadisListView(ListAPIView): "main_color_code": status.main_color_code, "description": get_localized_text(status.description, request=request, language_code=lang), } - for status in HadisStatus.objects.all().order_by('order') + for status in cached_statuses ] tags_list = [ @@ -311,7 +376,7 @@ class HadisListView(ListAPIView): "id": tag.id, "title": get_localized_text(tag.title, request=request, language_code=lang) or "", } - for tag in HadisTag.objects.filter(status=True).order_by('id') + for tag in cached_tags ] # 6. Inject it into the response data @@ -335,31 +400,29 @@ class HadisListView(ListAPIView): return response def get_queryset(self): - category_slug = self.kwargs.get('category_slug') - if not HadisCategory.objects.filter(slug=category_slug).exists(): + category_obj = self._get_category_obj() + if not category_obj: return Hadis.objects.none() + m2m_cat_subquery = Hadis.categories.through.objects.filter( + hadis_id=OuterRef('pk'), + hadiscategory_id=category_obj.id, + ) queryset = Hadis.objects.filter( - Q(category__slug=category_slug) | Q(categories__slug=category_slug), + Q(category_id=category_obj.id) | Exists(m2m_cat_subquery), status=True - ).distinct().annotate( - layer_count=Count('transmitters__narrator_layer', distinct=True) - ).select_related('category').prefetch_related( + ).select_related( + 'category', + 'category__sect', + 'hadis_status', + ).defer( + 'explanation', + 'normalized_text', + ).prefetch_related( 'tags', - Prefetch( - 'references', - queryset=HadisReference.objects.select_related( - 'book_reference', - 'book_reference__author', - 'edition', - 'book_volume', - 'book_volume__edition' - ).prefetch_related( - Prefetch('images', queryset=ReferenceImage.objects.order_by('priority')), - 'book_reference__editions' - ) - ) + build_detailed_reference_prefetch(HadisReference, ReferenceImage), ) + needs_distinct = False # 👇 1. Apply Search Filter search_query = self.request.query_params.get('search', None) @@ -440,8 +503,9 @@ class HadisListView(ListAPIView): ).values_list('content_id', flat=True) queryset = queryset.filter(id__in=bookmarked_ids) - # ❗️ distinct() برای جلوگیری از دابلیکیت شدن احادیث به خاطر ManyToMany ها الزامی است - return queryset.distinct() + if any([search_query, transmitter_filter, source_filter, author_filter, tag_filter]): + return queryset.distinct() + return queryset def get_serializer_context(self): """Add user bookmarks to serializer context to avoid caching issues""" @@ -480,24 +544,13 @@ class HadisMainListView(ListAPIView): def get_queryset(self): queryset = Hadis.objects.filter(status=True).select_related( - 'category__sect', 'hadis_status' - ).annotate( - layer_count=Count('transmitters__narrator_layer', distinct=True) + 'category', 'category__sect', 'hadis_status' + ).defer( + 'explanation', + 'normalized_text', ).prefetch_related( 'tags', - Prefetch( - 'references', - queryset=HadisReference.objects.select_related( - 'book_reference', - 'book_reference__author', - 'edition', - 'book_volume', - 'book_volume__edition' - ).prefetch_related( - Prefetch('images', queryset=ReferenceImage.objects.order_by('priority')), - 'book_reference__editions' - ) - ) + build_detailed_reference_prefetch(HadisReference, ReferenceImage), ) # Get search parameters @@ -864,20 +917,24 @@ class HadisCorrectionsView(ListAPIView): # }) def get_queryset(self): + from ..models import CorrectionReferenceImage hadis_slug = self.kwargs.get('hadis_slug') try: - hadis = Hadis.objects.filter(slug=hadis_slug, status=True).first() + hadis = Hadis.objects.filter(slug=hadis_slug, status=True).only('id').first() if not hadis and str(hadis_slug).isdigit(): - hadis = Hadis.objects.filter(id=int(hadis_slug), status=True).first() + hadis = Hadis.objects.filter(id=int(hadis_slug), status=True).only('id').first() if not hadis: return HadisCorrection.objects.none() - queryset = HadisCorrection.objects.filter(hadis=hadis).select_related( + queryset = HadisCorrection.objects.filter(hadis_id=hadis.id).select_related( 'hadis' + ).defer( + 'normalized_text', + 'hadis__text', + 'hadis__translation', + 'hadis__explanation', + 'hadis__normalized_text', ).prefetch_related( - 'references__book_reference__author', - 'references__edition', - 'references__book_volume', - 'references__images' + build_detailed_reference_prefetch(CorrectionReference, CorrectionReferenceImage), ).order_by('priority', '-created_at') # Filter by bookmarks if provided @@ -895,6 +952,20 @@ class HadisCorrectionsView(ListAPIView): except Hadis.DoesNotExist: return HadisCorrection.objects.none() + def get_serializer_context(self): + context = super().get_serializer_context() + user = self.request.user + if user and user.is_authenticated: + from apps.bookmark.models.bookmark import Bookmark + user_bookmarks = Bookmark.objects.filter( + user=user, + service=Bookmark.ServiceChoices.HADITH_CORRECTION, + status=True + ).values_list('content_id', flat=True) + context['user_bookmarked_correction_ids'] = set(user_bookmarks) + else: + context['user_bookmarked_correction_ids'] = set() + return context class CategoryHadisCorrectionsView(ListAPIView): @@ -906,6 +977,46 @@ class CategoryHadisCorrectionsView(ListAPIView): permission_classes = [AllowAny] pagination_class = StandardResultsSetPagination + def _get_category_and_descendant_ids(self): + if not hasattr(self, '_cached_category_info'): + category_slug = self.kwargs.get('category_slug') + category_obj = ( + HadisCategory.objects.select_related( + 'sect', + 'parent', + 'parent__sect', + 'parent__parent', + 'parent__parent__sect', + ).filter(slug=category_slug).first() + if category_slug else None + ) + if not category_obj: + descendant_ids = [] + elif category_obj.is_leaf_node(): + descendant_ids = [category_obj.id] + else: + descendant_ids = list(category_obj.get_descendants(include_self=True).values_list('id', flat=True)) + self._cached_category_info = (category_obj, descendant_ids) + return self._cached_category_info + + def _get_category_hadiths(self, cat_ids): + if not hasattr(self, '_cached_category_hadiths') or self._cached_category_hadiths_key != tuple(cat_ids): + self._cached_category_hadiths_key = tuple(cat_ids) + if not cat_ids: + self._cached_category_hadiths = [] + else: + m2m_sub = Hadis.categories.through.objects.filter( + hadis_id=OuterRef('pk'), + hadiscategory_id__in=cat_ids, + ) + self._cached_category_hadiths = list( + Hadis.objects.filter( + Q(category_id__in=cat_ids) | Exists(m2m_sub), + status=True, + ).only('id', 'number', 'slug', 'title').order_by('number', 'id') + ) + return self._cached_category_hadiths + @swagger_auto_schema( operation_summary="Get Category Hadis Evaluations", operation_description="Returns all evaluations across all hadiths belonging to a specific category, including hadis_info for each evaluation. Supports filtering by multiple hadiths and/or categories.", @@ -922,12 +1033,12 @@ class CategoryHadisCorrectionsView(ListAPIView): return self.list(request, *args, **kwargs) def get_queryset(self): + from ..models import CorrectionReferenceImage category_slug = self.kwargs.get('category_slug') try: - category_ids = [] - if category_slug: - category = HadisCategory.objects.get(slug=category_slug) - category_ids = list(category.get_descendants(include_self=True).values_list('id', flat=True)) + category_obj, category_ids = self._get_category_and_descendant_ids() + if category_slug and not category_obj: + return HadisCorrection.objects.none() # Optional filter by multiple categories (selected subcategories or category slugs/IDs) cat_param_keys = ['category_slugs', 'categories', 'category_slug'] @@ -966,21 +1077,23 @@ class CategoryHadisCorrectionsView(ListAPIView): return HadisCorrection.objects.none() if category_ids: - hadis_ids = Hadis.objects.filter( - Q(category_id__in=category_ids) | Q(categories__id__in=category_ids), - status=True - ).distinct().values_list('id', flat=True) + hadis_ids = [h.id for h in self._get_category_hadiths(category_ids)] + if not hadis_ids: + return HadisCorrection.objects.none() + queryset = HadisCorrection.objects.filter(hadis_id__in=hadis_ids) else: - hadis_ids = Hadis.objects.filter(status=True).values_list('id', flat=True) + queryset = HadisCorrection.objects.filter(hadis__status=True) - queryset = HadisCorrection.objects.filter( - hadis_id__in=hadis_ids - ).select_related('hadis').prefetch_related( - 'references', - 'references__book_reference__author', - 'references__edition', - 'references__book_volume', - 'references__images' + queryset = queryset.select_related( + 'hadis' + ).defer( + 'normalized_text', + 'hadis__text', + 'hadis__translation', + 'hadis__explanation', + 'hadis__normalized_text', + ).prefetch_related( + build_detailed_reference_prefetch(CorrectionReference, CorrectionReferenceImage), ).order_by('priority', '-created_at') # Optional search query filter @@ -1035,20 +1148,30 @@ class CategoryHadisCorrectionsView(ListAPIView): except HadisCategory.DoesNotExist: return HadisCorrection.objects.none() + def get_serializer_context(self): + context = super().get_serializer_context() + user = self.request.user + if user and user.is_authenticated: + from apps.bookmark.models.bookmark import Bookmark + user_bookmarks = Bookmark.objects.filter( + user=user, + service=Bookmark.ServiceChoices.HADITH_CORRECTION, + status=True + ).values_list('content_id', flat=True) + context['user_bookmarked_correction_ids'] = set(user_bookmarks) + else: + context['user_bookmarked_correction_ids'] = set() + return context + def list(self, request, *args, **kwargs): response = super().list(request, *args, **kwargs) - category_slug = self.kwargs.get('category_slug') - category_obj = HadisCategory.objects.filter(slug=category_slug).first() if category_slug else None + category_obj, cat_ids = self._get_category_and_descendant_ids() category_data = SimpleCategory(category_obj, context={'request': request}).data if category_obj else None available_hadiths = [] available_categories = [] if category_obj: - cat_ids = category_obj.get_descendants(include_self=True).values_list('id', flat=True) - hadiths_in_cat = Hadis.objects.filter( - category_id__in=cat_ids, - status=True - ).order_by('number', 'id') + hadiths_in_cat = self._get_category_hadiths(cat_ids) for h in hadiths_in_cat: h_title = get_localized_text(h.title, request) if hasattr(h, 'title') and h.title else f"Hadith {h.number}" @@ -1059,14 +1182,15 @@ class CategoryHadisCorrectionsView(ListAPIView): "title": h_title, }) - subcats = category_obj.get_children() if hasattr(category_obj, 'get_children') else category_obj.children.all() - for sc in subcats: - sc_title = get_localized_text(sc.title, request) if hasattr(sc, 'title') and sc.title else sc.slug - available_categories.append({ - "id": sc.id, - "slug": sc.slug, - "title": sc_title, - }) + if not category_obj.is_leaf_node(): + subcats_qs = category_obj.get_children() if hasattr(category_obj, 'get_children') else category_obj.children.all() + for sc in subcats_qs.only('id', 'slug', 'title'): + sc_title = get_localized_text(sc.title, request) if hasattr(sc, 'title') and sc.title else sc.slug + available_categories.append({ + "id": sc.id, + "slug": sc.slug, + "title": sc_title, + }) if isinstance(response.data, dict): ordered_data = {