Browse Source

feat(hadis): add ancestors list to SimpleCategory for breadcrumb navigation

master
Mohsen Taba 2 weeks ago
parent
commit
fc13a13857
  1. 22
      apps/hadis/serializers/category.py
  2. 4
      apps/hadis/views/category.py

22
apps/hadis/serializers/category.py

@ -171,9 +171,29 @@ class ArabicLocalizedField(serializers.Field):
class SimpleCategory(serializers.ModelSerializer): class SimpleCategory(serializers.ModelSerializer):
title = LocalizedField() title = LocalizedField()
description = LocalizedField() description = LocalizedField()
sect_type = serializers.CharField(source='sect.sect_type', read_only=True)
ancestors = serializers.SerializerMethodField()
class Meta: class Meta:
model = HadisCategory 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): class HadisCategorySectListSerializer(serializers.ModelSerializer):
"""Serializer for HadisSect list with grouped response""" """Serializer for HadisSect list with grouped response"""

4
apps/hadis/views/category.py

@ -339,7 +339,7 @@ class HadisCategorySelectBySectView(ListAPIView):
# 3. Serialize this single category for the Hero section # 3. Serialize this single category for the Hero section
# You might need a simple serializer just for titles/descriptions # 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 # 4. Inject it into the response data
# Note: We access response.data because we are using DRF's Response object # 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 # 3. Serialize this single category for the Hero section
# You might need a simple serializer just for titles/descriptions # 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 # 4. Inject it into the response data
# Note: We access response.data because we are using DRF's Response object # Note: We access response.data because we are using DRF's Response object

Loading…
Cancel
Save