From fc13a138570fc3ea666f7803e493afbb3eed673a Mon Sep 17 00:00:00 2001 From: mohsentaba Date: Tue, 13 Oct 2026 14:57:02 +0330 Subject: [PATCH] feat(hadis): add ancestors list to SimpleCategory for breadcrumb navigation --- apps/hadis/serializers/category.py | 22 +++++++++++++++++++++- apps/hadis/views/category.py | 4 ++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/apps/hadis/serializers/category.py b/apps/hadis/serializers/category.py index 25171d5..527c459 100644 --- a/apps/hadis/serializers/category.py +++ b/apps/hadis/serializers/category.py @@ -171,9 +171,29 @@ class ArabicLocalizedField(serializers.Field): class SimpleCategory(serializers.ModelSerializer): title = LocalizedField() description = LocalizedField() + sect_type = serializers.CharField(source='sect.sect_type', read_only=True) + ancestors = serializers.SerializerMethodField() + class Meta: model = HadisCategory - fields = ['id', 'title','description'] + fields = ['id', 'title', 'slug', 'source_type', 'sect_type', 'description', 'ancestors'] + + def get_ancestors(self, obj): + if not obj: + return [] + request = self.context.get('request') + ancestors_qs = obj.get_ancestors(include_self=False).select_related('sect') + result = [] + for a in ancestors_qs: + result.append({ + "id": a.id, + "title": get_localized_text(a.title, request=request) or a.slug, + "slug": a.slug, + "source_type": a.source_type, + "sect_type": a.sect.sect_type if a.sect else None, + "has_hadis": False, + }) + return result class HadisCategorySectListSerializer(serializers.ModelSerializer): """Serializer for HadisSect list with grouped response""" diff --git a/apps/hadis/views/category.py b/apps/hadis/views/category.py index 149d79c..54425e3 100644 --- a/apps/hadis/views/category.py +++ b/apps/hadis/views/category.py @@ -339,7 +339,7 @@ class HadisCategorySelectBySectView(ListAPIView): # 3. Serialize this single category for the Hero section # You might need a simple serializer just for titles/descriptions - category_data = SimpleCategory(category_obj).data + category_data = SimpleCategory(category_obj, context={'request': request}).data # 4. Inject it into the response data # Note: We access response.data because we are using DRF's Response object @@ -407,7 +407,7 @@ class HadisCategorySelectBySectSourceView(ListAPIView): # 3. Serialize this single category for the Hero section # You might need a simple serializer just for titles/descriptions - category_data = SimpleCategory(category_obj).data + category_data = SimpleCategory(category_obj, context={'request': request}).data # 4. Inject it into the response data # Note: We access response.data because we are using DRF's Response object