from django.views.generic import TemplateView, FormView from django.http import Http404 from .forms import preference_form_builder from rest_framework.generics import GenericAPIView from rest_framework.response import Response from dynamic_preferences.registries import global_preferences_registry from .serializers import ( AboutUsSerializer, FAQItemSerializer, SupportSerializer, CardSerializer, AboutUsDobodiSerializer ) from rest_framework.authentication import TokenAuthentication from rest_framework.permissions import IsAuthenticated from apps.account.permissions import IsSuperAdmin class AboutUsAPIView(GenericAPIView): serializer_class = AboutUsSerializer authentication_classes = [TokenAuthentication] def get_permissions(self): if self.request.method in ['PUT', 'POST', 'PATCH']: return [IsSuperAdmin()] return [] def get(self, request, *args, **kwargs): preferences = global_preferences_registry.manager() about_us = preferences.get('aboutus__aboutus', '') serializer = self.get_serializer({'content': about_us}) return Response(serializer.data) def put(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) content = serializer.validated_data.get('content', '') preferences = global_preferences_registry.manager() preferences['aboutus__aboutus'] = content return Response(serializer.data) class FAQCourseAPIView(GenericAPIView): serializer_class = FAQItemSerializer def get(self, request, *args, **kwargs): preferences = global_preferences_registry.manager() faq_course = preferences.get('FAQ_Course__FAQ_Course', []) serializer = self.get_serializer(faq_course, many=True) return Response(serializer.data) class FAQGeneralAPIView(GenericAPIView): serializer_class = FAQItemSerializer authentication_classes = [TokenAuthentication] def get_permissions(self): if self.request.method in ['PUT', 'POST', 'PATCH']: return [IsAuthenticated(), IsSuperAdmin()] return [] def get(self, request, *args, **kwargs): preferences = global_preferences_registry.manager() faq_general = preferences.get('FAQ_General__FAQ_General', []) serializer = self.get_serializer(faq_general, many=True) return Response(serializer.data) def put(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data, many=True) serializer.is_valid(raise_exception=True) preferences = global_preferences_registry.manager() preferences['FAQ_General__FAQ_General'] = serializer.validated_data return Response(serializer.data) class SupportAPIView(GenericAPIView): serializer_class = SupportSerializer authentication_classes = [TokenAuthentication] def get_permissions(self): if self.request.method in ['PUT', 'POST', 'PATCH']: return [IsSuperAdmin()] return [] def get(self, request, *args, **kwargs): preferences = global_preferences_registry.manager() support = preferences.get('support__support', {}) if not isinstance(support, dict): support = {} serializer = self.get_serializer(support) return Response(serializer.data) def put(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) preferences = global_preferences_registry.manager() preferences['support__support'] = serializer.validated_data return Response(serializer.data) class CardAPIView(GenericAPIView): serializer_class = CardSerializer authentication_classes = [TokenAuthentication] def get_permissions(self): if self.request.method in ['PUT', 'POST', 'PATCH']: return [IsSuperAdmin()] return [] def get(self, request, *args, **kwargs): preferences = global_preferences_registry.manager() card = preferences.get('card__card', {}) if not isinstance(card, dict): card = {} serializer = self.get_serializer(card) return Response(serializer.data) def put(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) preferences = global_preferences_registry.manager() preferences['card__card'] = serializer.validated_data return Response(serializer.data) class AboutUsDobodiAPIView(GenericAPIView): serializer_class = AboutUsDobodiSerializer authentication_classes = [TokenAuthentication] def get_permissions(self): if self.request.method in ['PUT', 'POST', 'PATCH']: return [IsAuthenticated(), IsSuperAdmin()] 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}] footer_translations = data.get('footer_translations') if not isinstance(footer_translations, list): footer_translations = [] legacy_footer = data.get('footer_description') or '' if not footer_translations and legacy_footer: footer_translations = [{'language_code': 'en', 'text': legacy_footer, 'title': legacy_footer}] localized_content = get_localized_text(translations, request=request) or legacy_content or '' localized_footer = get_localized_text(footer_translations, request=request) or legacy_footer 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, 'footer_description': localized_footer, 'translations': translations, 'footer_translations': footer_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): preferences = global_preferences_registry.manager() about_us_dobodi = preferences.get('about_us_dobodi__about_us_dobodi', {}) payload = self._build_response_payload(request, about_us_dobodi) serializer = self.get_serializer(payload) return Response(serializer.data) def put(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) preferences = global_preferences_registry.manager() existing = preferences.get('about_us_dobodi__about_us_dobodi', {}) if not isinstance(existing, dict): existing = {} validated = {**existing, **dict(serializer.validated_data)} if 'translations' not in request.data and 'translations' in existing: validated['translations'] = existing['translations'] if 'footer_translations' not in request.data and 'footer_translations' in existing: validated['footer_translations'] = existing['footer_translations'] from apps.hadis.serializers.category import get_localized_text # Ensure content/description fallback is synced with translations if provided translations = validated.get('translations') if isinstance(translations, list) and translations: default_text = get_localized_text(translations, fallback_lang='en') or '' if default_text: validated['content'] = default_text validated['description'] = default_text # Ensure footer_description fallback is synced with footer_translations if provided footer_translations = validated.get('footer_translations') if isinstance(footer_translations, list) and footer_translations: default_footer = get_localized_text(footer_translations, fallback_lang='en') or '' if default_footer: validated['footer_description'] = default_footer 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""" class RegularTemplateView(TemplateView): """Used for testing context""" template_name = "dynamic_preferences/testcontext.html" class PreferenceFormView(FormView): """ Display a form for updating preferences of the given section provided via URL arg. If no section is provided, will display a form for all fields of a given registry. """ #: the registry for preference lookups registry = None #: will be used by :py:func:`forms.preference_form_builder` # to create the form form_class = None template_name = "dynamic_preferences/form.html" def dispatch(self, request, *args, **kwargs): self.section_name = kwargs.get("section", None) if self.section_name: try: self.section = self.registry.section_objects[self.section_name] except KeyError: raise Http404 else: self.section = None return super(PreferenceFormView, self).dispatch(request, *args, **kwargs) def get_form_class(self, *args, **kwargs): form_class = preference_form_builder(self.form_class, section=self.section_name) return form_class def get_context_data(self, *args, **kwargs): context = super(PreferenceFormView, self).get_context_data(*args, **kwargs) context["registry"] = self.registry context["section"] = self.section return context def get_success_url(self): return self.request.path def form_valid(self, form): form.update_preferences() return super(PreferenceFormView, self).form_valid(form)