|
|
|
@ -4,6 +4,39 @@ from django.utils.translation import gettext_lazy as _ |
|
|
|
# from django.db.models import Count |
|
|
|
|
|
|
|
from ..models import HadisSect, HadisCategory, Hadis , HadisCategory |
|
|
|
from django.utils.translation import get_language |
|
|
|
|
|
|
|
def get_localized_text(json_list, request=None, fallback_lang="en"): |
|
|
|
""" |
|
|
|
Extract localized text from a JSON list based on request's language. |
|
|
|
|
|
|
|
Expects: [{"language_code": "en", "text": "..."}, ...] |
|
|
|
Returns: Single text string or None |
|
|
|
""" |
|
|
|
if not json_list or not isinstance(json_list, list): |
|
|
|
return None |
|
|
|
|
|
|
|
# Get target language |
|
|
|
language_code = getattr(request, "LANGUAGE_CODE", None) if request else None |
|
|
|
if not language_code: |
|
|
|
language_code = get_language() or fallback_lang |
|
|
|
|
|
|
|
# 1) Exact match |
|
|
|
for item in json_list: |
|
|
|
if isinstance(item, dict) and item.get("language_code") == language_code: |
|
|
|
return item.get("text") |
|
|
|
|
|
|
|
# 2) Fallback to English |
|
|
|
for item in json_list: |
|
|
|
if isinstance(item, dict) and item.get("language_code") == "en": |
|
|
|
return item.get("text") |
|
|
|
|
|
|
|
# 3) First available |
|
|
|
if json_list and isinstance(json_list[0], dict): |
|
|
|
return json_list[0].get("text") |
|
|
|
|
|
|
|
return None |
|
|
|
|
|
|
|
|
|
|
|
class LocalizedField(serializers.Field): |
|
|
|
""" |
|
|
|
@ -136,23 +169,28 @@ class HadisCategoryTreeSerializer(serializers.ModelSerializer): |
|
|
|
).order_by('number').values_list('number', flat=True) |
|
|
|
) |
|
|
|
|
|
|
|
def to_dict(self, c): |
|
|
|
"""Convert category to dictionary""" |
|
|
|
children = c.get_children().filter(sect=c.sect).order_by('order') |
|
|
|
|
|
|
|
def to_dict(self, category, request=None): |
|
|
|
"""Convert category to dict, applying localization""" |
|
|
|
if request is None: |
|
|
|
request = self.context.get('request') |
|
|
|
|
|
|
|
return { |
|
|
|
'id': c.id, |
|
|
|
'name': self.get_name(c), |
|
|
|
'description': c.description, |
|
|
|
'source_type': c.source_type, |
|
|
|
'hadis_count': self.get_hadis_count(c), |
|
|
|
'has_hadis': self.get_has_hadis(c), |
|
|
|
'order': c.order, |
|
|
|
'thumbnail': self.get_thumbnail(c), |
|
|
|
'xmind_file': self.get_xmind_file(c), |
|
|
|
'has_xmind_file': self.get_has_xmind_file(c), |
|
|
|
'children_count': 0 if not children else len([self.to_dict(i) for i in children]), |
|
|
|
'children': [] if not children else [self.to_dict(i) for i in children], |
|
|
|
'id': category.id, |
|
|
|
'title': get_localized_text(category.title, request), # <-- use helper |
|
|
|
'description': get_localized_text(category.description, request), # <-- use helper |
|
|
|
'slug': category.slug, |
|
|
|
'source_type': category.source_type, |
|
|
|
'hadis_count': self.get_hadis_count(category), |
|
|
|
'has_hadis': self.get_has_hadis(category), |
|
|
|
'order': category.order, |
|
|
|
'thumbnail': self.get_thumbnail(category), |
|
|
|
'xmind_file': self.get_xmind_file(category), |
|
|
|
'has_xmind_file': self.get_has_xmind_file(category), |
|
|
|
'children': [ |
|
|
|
self.to_dict(child, request) # recursively apply |
|
|
|
for child in category.children.all() |
|
|
|
] |
|
|
|
} |
|
|
|
|
|
|
|
class HadisCategorySelectSerializer(serializers.ModelSerializer): |
|
|
|
|