from django.conf import settings from django.shortcuts import render from django.contrib import messages from django.contrib.auth.mixins import LoginRequiredMixin from django.db.models import Q from django.shortcuts import render, redirect, get_object_or_404 from django.urls import reverse_lazy from django.views import generic from module_iam.models import IAmPrincipal from module_iam import iam_constant from module_project.service import EmailService from .models import ContactUs, Feedback from module_project.mixins import DatatablesMixin from django_datatables_view.base_datatable_view import BaseDatatableView from module_project.mixins import ActionMixin from module_project import constants from module_project.utils import JsonResponseUtil # Create your views here. class ContactUsView(LoginRequiredMixin, generic.TemplateView): page_name = iam_constant.RESOURCE_MANAGE_CONTACT_US resource = None action = None template_name = "module_support/contact_us.html" model = ContactUs context_objext_name = "obj" def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["page_name"] = self.page_name return context class ContactUsListJson(BaseDatatableView): model = ContactUs columns = ["id", "email_address", "subject", "message", "active", "deleted"] order_columns = ["id", "email_address", "subject", "message", "active", "deleted"] def get_initial_queryset(self): deleted_flag = self.request.GET.get('deleted_flag', None) return self.model.objects.filter(deleted=deleted_flag) def filter_queryset(self, qs): # Implement your custom filtering logic here print(f"request is {self.request.GET}") search_value = self.request.GET.get("search[value]", None) if search_value: qs = qs.filter( Q(id__icontains=search_value) | Q(question__icontains=search_value) | Q(answer__icontains=search_value) ) for column in self.columns: search_value = self.request.GET.get(f'columns[{self.columns.index(column)}][search][value]', None) if search_value: qs = qs.filter(**{f"{column}__icontains": search_value}) return qs class ContactUsActionView(ActionMixin): model = ContactUs class ContactUsArchiveView(LoginRequiredMixin, generic.TemplateView): page_name = iam_constant.RESOURCE_MANAGE_CONTACT_US resource = None action = None template_name = "module_support/contactus_archive_list.html" model = ContactUs def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["page_name"] = self.page_name return context class ContactUsReplyView(LoginRequiredMixin, generic.View): page_name = iam_constant.RESOURCE_MANAGE_CONTACT_US model = ContactUs success_message = constants.DATA_SAVED def post(self, request, *args, **kwargs): id = self.kwargs.get("id") message = request.POST.get("message") if id or message: try: instance = self.model.objects.get(id=id) instance.reply = message instance.save() email_service = EmailService( subject=f"Reply of your inquiry - {instance.subject}", body=message, to=instance.email, from_email=settings.EMAIL_HOST_USER, ) email_service.send() JsonResponseUtil.success(message=self.success_message) except self.model.DoesNotExist: JsonResponseUtil.error(message=constants.FAILURE, errors="Invalid contact us ID.") except Exception as e: JsonResponseUtil.error(message=constants.FAILURE, errors=str(e)) else: JsonResponseUtil.error(message=constants.FAILURE, errors="Missing 'id' or 'message' in the request") # Redirect to the desired URL after form submission return JsonResponseUtil.success(message=constants.SUCCESS) class FeedbackView(LoginRequiredMixin, generic.TemplateView): page_name = iam_constant.RESOURCE_MANAGE_FEEDBACK resource = iam_constant.RESOURCE_MANAGE_FEEDBACK action = None template_name = "module_support/feedback.html" model = Feedback context_objext_name = "obj" def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["page_name"] = self.page_name return context class FeedbackListJson(BaseDatatableView): model = Feedback columns = ["id", "principal.email", "feedback_reaction", "comment", "active"] order_columns = ["id", "principal.email", "feedback_reaction", "comment", "active"] def get_initial_queryset(self): deleted_flag = self.request.GET.get('deleted_flag', None) return self.model.objects.filter(deleted=deleted_flag) def filter_queryset(self, qs): # Implement your custom filtering logic here print(f"request is {self.request.GET}") search_value = self.request.GET.get("search[value]", None) if search_value: qs = qs.filter( Q(id__icontains=search_value) | Q(feedback_reaction__icontains=search_value) | Q(comment__icontains=search_value) ) return qs class FeedbackActionView(ActionMixin): model = Feedback pass