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 ) class AboutUsAPIView(GenericAPIView): serializer_class = AboutUsSerializer def get(self, request, *args, **kwargs): preferences = global_preferences_registry.manager() about_us = preferences.get('aboutus__aboutus', '') serializer = self.get_serializer(data={'content': about_us}) serializer.is_valid(raise_exception=True) 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(data=faq_course, many=True) serializer.is_valid(raise_exception=True) return Response(serializer.data) class FAQGeneralAPIView(GenericAPIView): serializer_class = FAQItemSerializer def get(self, request, *args, **kwargs): preferences = global_preferences_registry.manager() faq_general = preferences.get('FAQ_General__FAQ_General', []) serializer = self.get_serializer(data=faq_general, many=True) serializer.is_valid(raise_exception=True) return Response(serializer.data) class SupportAPIView(GenericAPIView): serializer_class = SupportSerializer def get(self, request, *args, **kwargs): preferences = global_preferences_registry.manager() support = preferences.get('support__support', {}) serializer = self.get_serializer(data=support) serializer.is_valid(raise_exception=True) return Response(serializer.data) class CardAPIView(GenericAPIView): serializer_class = CardSerializer def get(self, request, *args, **kwargs): preferences = global_preferences_registry.manager() card = preferences.get('card__card', {}) serializer = self.get_serializer(data=card) serializer.is_valid(raise_exception=True) return Response(serializer.data) class AboutUsDobodiAPIView(GenericAPIView): serializer_class = AboutUsDobodiSerializer def get(self, request, *args, **kwargs): preferences = global_preferences_registry.manager() about_us_dobodi = preferences.get('about_us_dobodi__about_us_dobodi', {}) serializer = self.get_serializer(data=about_us_dobodi) serializer.is_valid(raise_exception=True) return Response(serializer.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)