From ce5638389f728b450d1053d014eeabe000faba46 Mon Sep 17 00:00:00 2001 From: mohsentaba Date: Sat, 27 Dec 2025 14:18:10 +0330 Subject: [PATCH] sync reference hadises fixed. --- apps/hadis/docs.py | 10 ++++----- apps/hadis/serializers/hadis.py | 14 +++++++++++++ apps/hadis/serializers/reference.py | 32 ++++++----------------------- 3 files changed, 25 insertions(+), 31 deletions(-) diff --git a/apps/hadis/docs.py b/apps/hadis/docs.py index fbeffde..5c0198b 100644 --- a/apps/hadis/docs.py +++ b/apps/hadis/docs.py @@ -57,13 +57,13 @@ hadis_category_tree_swagger = swagger_auto_schema( operation_description=( "Get complete hierarchical tree of Hadis categories grouped by sect type (shia/sunni). " "Categories are not grouped by source_type in the response (mobile filters source_type client-side). " - "This sync endpoint returns only category metadata (no hadis payload) for fast offline building of navigation trees." + "This sync endpoint returns only category metadata (no hadis payload) for fast offline building of navigation trees. Includes sect information and full category tree with hadis counts and children counts." ), operation_summary="Get Complete Hadis Category Tree", tags=['Hadis'], responses={ status.HTTP_200_OK: openapi.Response( - description="Complete hierarchical tree grouped by sect with only category metadata (no hadis payloads)", + description="Complete hierarchical tree grouped by sect with category metadata including hadis counts and children counts", examples={ "application/json": { "count": 6, @@ -87,7 +87,7 @@ hadis_category_tree_swagger = swagger_auto_schema( "categories": [ { "id": 10, - "name": "Tafsir", + "title": "Tafsir", "description": "Quran commentary", "slug": "-330", "source_type": "quran", @@ -101,7 +101,7 @@ hadis_category_tree_swagger = swagger_auto_schema( "children": [ { "id": 11, - "name": "Surah Al-Fatiha", + "title": "Surah Al-Fatiha", "description": "Opening chapter", "slug": "-330", "source_type": "quran", @@ -136,7 +136,7 @@ hadis_category_tree_swagger = swagger_auto_schema( "categories": [ { "id": 20, - "name": "Book of Faith", + "title": "Book of Faith", "description": "Iman topics", "slug": "-330", "source_type": "hadith", diff --git a/apps/hadis/serializers/hadis.py b/apps/hadis/serializers/hadis.py index 97d1210..082da88 100644 --- a/apps/hadis/serializers/hadis.py +++ b/apps/hadis/serializers/hadis.py @@ -600,6 +600,20 @@ class HadisBasicSerializer(serializers.ModelSerializer): } return None +class HadisShortSerializer(serializers.ModelSerializer): + """Basic serializer for Hadis with minimal information""" + translation = LocalizedField() + + title = LocalizedField() + title_narrator = LocalizedField() + + class Meta: + model = Hadis + fields = [ + 'id', 'slug', 'title', 'title_narrator', 'text', + 'translation', 'share_link'] + + class HadisDetailSerializer(serializers.ModelSerializer): """Detailed serializer for Hadis with core information (transmitters and corrections moved to separate endpoints)""" diff --git a/apps/hadis/serializers/reference.py b/apps/hadis/serializers/reference.py index 13a8dcb..75e12b1 100644 --- a/apps/hadis/serializers/reference.py +++ b/apps/hadis/serializers/reference.py @@ -2,8 +2,8 @@ from rest_framework import serializers from django.utils.translation import get_language from .category import get_localized_text -from ..serializers import HadisListSerializer,HadisBasicSerializer,LocalizedField -from ..models import BookReference , BookAuthor , BookReferenceImage, HadisReference , BookAttribute +from ..serializers import HadisBasicSerializer,LocalizedField, HadisShortSerializer +from ..models import BookReference , BookAuthor , BookReferenceImage , BookAttribute class BookAuthorSerializer(serializers.ModelSerializer): @@ -163,27 +163,7 @@ class BookReferenceSyncSerializer(serializers.ModelSerializer): 'rating': obj.rate } - def get_hadises(self, obj): - """Get all hadises related to this book reference (already prefetched)""" - request = self.context.get('request') - - hadis_list = [] - # obj.hadis_references.all() is already prefetched! - for hadis_ref in obj.hadis_references.all(): - hadis = hadis_ref.hadis # Already prefetched, no query! - - # Handle get_translation - if it's a method, call it; if field, access it - translation = hadis.get_translation if callable(hadis.get_translation) else hadis.translation - if callable(translation): - translation = translation() - - hadis_list.append({ - 'id': hadis.id, - 'title': get_localized_text(hadis.title, request), - 'title_narrator': get_localized_text(hadis.title_narrator, request), - 'text': get_localized_text(hadis.text, request), - 'translation': get_localized_text(translation, request), - 'share_link': hadis.share_link - }) - - return hadis_list \ No newline at end of file + def get_hadises(self,obj): + references = obj.hadis_references.all() + hadis_list = [ref.hadis for ref in references if ref.hadis] + return HadisShortSerializer(hadis_list,many=True).data \ No newline at end of file