|
|
@ -127,20 +127,71 @@ class AboutUsDobodiAPIView(GenericAPIView): |
|
|
return [IsAuthenticated(), IsSuperAdmin()] |
|
|
return [IsAuthenticated(), IsSuperAdmin()] |
|
|
return [] |
|
|
return [] |
|
|
|
|
|
|
|
|
|
|
|
def _build_response_payload(self, request, raw_data): |
|
|
|
|
|
from apps.hadis.serializers.category import get_localized_text |
|
|
|
|
|
from apps.library.models import Book |
|
|
|
|
|
from apps.article.models import Article |
|
|
|
|
|
from apps.video.models import Video |
|
|
|
|
|
from apps.podcast.models import Podcast |
|
|
|
|
|
from apps.hadis.models import Transmitters |
|
|
|
|
|
|
|
|
|
|
|
data = dict(raw_data) if isinstance(raw_data, dict) else {} |
|
|
|
|
|
translations = data.get('translations') |
|
|
|
|
|
if not isinstance(translations, list): |
|
|
|
|
|
translations = [] |
|
|
|
|
|
|
|
|
|
|
|
title_translations = data.get('title_translations') |
|
|
|
|
|
if not isinstance(title_translations, list): |
|
|
|
|
|
title_translations = [] |
|
|
|
|
|
|
|
|
|
|
|
# Backward compatibility: if translations is empty but legacy content exists, seed it |
|
|
|
|
|
legacy_content = data.get('content') or data.get('description') or '' |
|
|
|
|
|
if not translations and legacy_content: |
|
|
|
|
|
translations = [{'language_code': 'en', 'text': legacy_content, 'title': legacy_content}] |
|
|
|
|
|
|
|
|
|
|
|
localized_content = get_localized_text(translations, request=request) or legacy_content or '' |
|
|
|
|
|
localized_title = get_localized_text(title_translations, request=request) or data.get('title') or '' |
|
|
|
|
|
|
|
|
|
|
|
payload = { |
|
|
|
|
|
**data, |
|
|
|
|
|
'title': localized_title, |
|
|
|
|
|
'content': localized_content, |
|
|
|
|
|
'description': localized_content, |
|
|
|
|
|
'translations': translations, |
|
|
|
|
|
'title_translations': title_translations, |
|
|
|
|
|
'books_count': Book.objects.filter(status=True).count(), |
|
|
|
|
|
'articles_count': Article.objects.filter(status=True).count(), |
|
|
|
|
|
'videos_count': Video.objects.filter(status=True).count(), |
|
|
|
|
|
'podcasts_count': Podcast.objects.filter(status=True).count(), |
|
|
|
|
|
'scholars_count': Transmitters.objects.count(), |
|
|
|
|
|
} |
|
|
|
|
|
return payload |
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs): |
|
|
def get(self, request, *args, **kwargs): |
|
|
preferences = global_preferences_registry.manager() |
|
|
preferences = global_preferences_registry.manager() |
|
|
about_us_dobodi = preferences.get('about_us_dobodi__about_us_dobodi', {}) |
|
|
about_us_dobodi = preferences.get('about_us_dobodi__about_us_dobodi', {}) |
|
|
if not isinstance(about_us_dobodi, dict): |
|
|
|
|
|
about_us_dobodi = {} |
|
|
|
|
|
serializer = self.get_serializer(about_us_dobodi) |
|
|
|
|
|
|
|
|
payload = self._build_response_payload(request, about_us_dobodi) |
|
|
|
|
|
serializer = self.get_serializer(payload) |
|
|
return Response(serializer.data) |
|
|
return Response(serializer.data) |
|
|
|
|
|
|
|
|
def put(self, request, *args, **kwargs): |
|
|
def put(self, request, *args, **kwargs): |
|
|
serializer = self.get_serializer(data=request.data) |
|
|
serializer = self.get_serializer(data=request.data) |
|
|
serializer.is_valid(raise_exception=True) |
|
|
serializer.is_valid(raise_exception=True) |
|
|
|
|
|
validated = dict(serializer.validated_data) |
|
|
|
|
|
|
|
|
|
|
|
# Ensure content/description fallback is synced with translations if provided |
|
|
|
|
|
translations = validated.get('translations') |
|
|
|
|
|
if isinstance(translations, list) and translations: |
|
|
|
|
|
from apps.hadis.serializers.category import get_localized_text |
|
|
|
|
|
default_text = get_localized_text(translations, fallback_lang='en') or '' |
|
|
|
|
|
if default_text: |
|
|
|
|
|
validated['content'] = default_text |
|
|
|
|
|
validated['description'] = default_text |
|
|
|
|
|
|
|
|
preferences = global_preferences_registry.manager() |
|
|
preferences = global_preferences_registry.manager() |
|
|
preferences['about_us_dobodi__about_us_dobodi'] = serializer.validated_data |
|
|
|
|
|
return Response(serializer.data) |
|
|
|
|
|
|
|
|
preferences['about_us_dobodi__about_us_dobodi'] = validated |
|
|
|
|
|
payload = self._build_response_payload(request, validated) |
|
|
|
|
|
return Response(self.get_serializer(payload).data) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""Todo : remove these views and use only context processors""" |
|
|
"""Todo : remove these views and use only context processors""" |
|
|
|