332 lines
11 KiB
Python
332 lines
11 KiB
Python
|
|
import logging
|
||
|
|
|
||
|
|
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 .forms import AboutUsForm, TermsAndConditionForm, FaqCategoryFrom, PrivacyPolicyForm
|
||
|
|
from .models import Faqs, Organization
|
||
|
|
from .api.serializers import FaqListSerializer
|
||
|
|
from module_project.mixins import DatatablesMixin
|
||
|
|
from django_datatables_view.base_datatable_view import BaseDatatableView
|
||
|
|
|
||
|
|
from module_project import constants
|
||
|
|
|
||
|
|
logger = logging.getLogger(__name__)
|
||
|
|
|
||
|
|
|
||
|
|
class FaqView(LoginRequiredMixin, generic.TemplateView):
|
||
|
|
page_name = None
|
||
|
|
resource = None
|
||
|
|
action = None
|
||
|
|
template_name = "module_cms/faq.html"
|
||
|
|
model = Faqs
|
||
|
|
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 FaqDatatableView(DatatablesMixin, LoginRequiredMixin, generic.View):
|
||
|
|
# model = Faqs
|
||
|
|
|
||
|
|
# def get_queryset(self):
|
||
|
|
# return self.model.objects.filter(deleted=False)
|
||
|
|
|
||
|
|
# def get(self, request):
|
||
|
|
# (
|
||
|
|
# draw,
|
||
|
|
# start,
|
||
|
|
# length,
|
||
|
|
# order_columns,
|
||
|
|
# order_directions,
|
||
|
|
# search_value,
|
||
|
|
# ) = self.get_datatables_params(request)
|
||
|
|
# queryset = self.get_queryset()
|
||
|
|
|
||
|
|
# page_obj, total_count, filtered_count = self.get_pagination(
|
||
|
|
# queryset, start, length
|
||
|
|
# )
|
||
|
|
|
||
|
|
# serializer = FaqListSerializer(
|
||
|
|
# page_obj.object_list, many=True
|
||
|
|
# )
|
||
|
|
|
||
|
|
# response = self.prepare_datatables_response(
|
||
|
|
# draw, total_count, filtered_count, serializer.data
|
||
|
|
# )
|
||
|
|
|
||
|
|
# return response
|
||
|
|
|
||
|
|
|
||
|
|
class FaqListJson(BaseDatatableView):
|
||
|
|
model = Faqs
|
||
|
|
columns = ["id", "question", "answer", "active", "deleted"]
|
||
|
|
order_columns = ["id", "question", "answer", "active", "deleted"]
|
||
|
|
|
||
|
|
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 FaqCreateOrUpdateView(generic.View):
|
||
|
|
pass
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
class AboutUsView(LoginRequiredMixin, generic.DetailView):
|
||
|
|
page_name = None
|
||
|
|
template_name = "module_cms/about_us_view.html"
|
||
|
|
model = Organization
|
||
|
|
context_object_name = "organization"
|
||
|
|
|
||
|
|
def get_object(self, queryset=None):
|
||
|
|
return self.model.objects.only("about_us").first()
|
||
|
|
|
||
|
|
def get_context_data(self, **kwargs):
|
||
|
|
context = super().get_context_data(**kwargs)
|
||
|
|
context["page_name"] = self.page_name
|
||
|
|
return context
|
||
|
|
|
||
|
|
|
||
|
|
class AboutUsCreateOrUpdateView(LoginRequiredMixin, generic.View):
|
||
|
|
# Set the page_name and resource
|
||
|
|
page_name = None
|
||
|
|
resource = None
|
||
|
|
|
||
|
|
# Initialize the action as ACTION_CREATE (can change based on logic)
|
||
|
|
action = None # Default action
|
||
|
|
|
||
|
|
template_name = "module_cms/about_us_add.html"
|
||
|
|
model = Organization
|
||
|
|
form_class = AboutUsForm
|
||
|
|
success_url = reverse_lazy("module_cms:about_us")
|
||
|
|
error_message = "An error occurred while saving the data."
|
||
|
|
|
||
|
|
# Determine the success message dynamically based on whether it's an update or create
|
||
|
|
def get_success_message(self):
|
||
|
|
self.success_message = (
|
||
|
|
constants.RECORD_CREATED if not self.object else constants.RECORD_UPDATED
|
||
|
|
)
|
||
|
|
return self.success_message
|
||
|
|
|
||
|
|
# Get the object (if exists) based on URL parameter 'pk'
|
||
|
|
def get_object(self):
|
||
|
|
return self.model.objects.only("about_us").first()
|
||
|
|
|
||
|
|
# Add page_name and operation to the context
|
||
|
|
def get_context_data(self, **kwargs):
|
||
|
|
context = {
|
||
|
|
"page_name": self.page_name,
|
||
|
|
"operation": "Add" if not self.object else "Edit",
|
||
|
|
}
|
||
|
|
context.update(kwargs) # Include any additional context data passed to the view
|
||
|
|
return context
|
||
|
|
|
||
|
|
def get(self, request, *args, **kwargs):
|
||
|
|
self.object = self.get_object()
|
||
|
|
|
||
|
|
# If an object is found, change action to ACTION_UPDATE
|
||
|
|
if self.object is not None:
|
||
|
|
self.action = None
|
||
|
|
|
||
|
|
form = self.form_class(instance=self.object)
|
||
|
|
context = self.get_context_data(form=form)
|
||
|
|
return render(request, self.template_name, context=context)
|
||
|
|
|
||
|
|
def post(self, request, *args, **kwargs):
|
||
|
|
self.object = self.get_object()
|
||
|
|
|
||
|
|
# If an object is found, change action to ACTION_UPDATE
|
||
|
|
if self.object is not None:
|
||
|
|
self.action = None
|
||
|
|
|
||
|
|
form = self.form_class(request.POST, instance=self.object)
|
||
|
|
if not form.is_valid():
|
||
|
|
print(form.errors)
|
||
|
|
context = self.get_context_data(form=form)
|
||
|
|
return render(request, self.template_name, context=context)
|
||
|
|
|
||
|
|
form.save()
|
||
|
|
messages.success(self.request, self.get_success_message())
|
||
|
|
return redirect(self.success_url)
|
||
|
|
|
||
|
|
|
||
|
|
class TermsConditionView(LoginRequiredMixin, generic.DetailView):
|
||
|
|
page_name = None
|
||
|
|
resource = None
|
||
|
|
action = None
|
||
|
|
template_name = "module_cms/terms_and_condition_view.html"
|
||
|
|
model = Organization
|
||
|
|
context_object_name = "organization"
|
||
|
|
|
||
|
|
def get_object(self, queryset=None):
|
||
|
|
return self.model.objects.only("terms_condition").first()
|
||
|
|
|
||
|
|
def get_context_data(self, **kwargs):
|
||
|
|
context = super().get_context_data(**kwargs)
|
||
|
|
context["page_name"] = self.page_name
|
||
|
|
return context
|
||
|
|
|
||
|
|
|
||
|
|
class TermsConditionCreateOrUpdateView(LoginRequiredMixin, generic.View):
|
||
|
|
# Set the page_name and resource
|
||
|
|
page_name = None
|
||
|
|
resource = None
|
||
|
|
|
||
|
|
# Initialize the action as ACTION_CREATE (can change based on logic)
|
||
|
|
action = None # Default action
|
||
|
|
|
||
|
|
template_name = "module_cms/terms_and_condition_edit.html"
|
||
|
|
model = Organization
|
||
|
|
form_class = TermsAndConditionForm
|
||
|
|
success_url = reverse_lazy("module_cms:terms_and_condition")
|
||
|
|
error_message = "An error occurred while saving the data."
|
||
|
|
|
||
|
|
# Determine the success message dynamically based on whether it's an update or create
|
||
|
|
def get_success_message(self):
|
||
|
|
self.success_message = (
|
||
|
|
constants.RECORD_CREATED if not self.object else constants.RECORD_UPDATED
|
||
|
|
)
|
||
|
|
return self.success_message
|
||
|
|
|
||
|
|
# Get the object (if exists) based on URL parameter 'pk'
|
||
|
|
def get_object(self):
|
||
|
|
return self.model.objects.only("terms_condition").first()
|
||
|
|
|
||
|
|
# Add page_name and operation to the context
|
||
|
|
def get_context_data(self, **kwargs):
|
||
|
|
context = {
|
||
|
|
"page_name": self.page_name,
|
||
|
|
"operation": "Add" if not self.object else "Edit",
|
||
|
|
}
|
||
|
|
context.update(kwargs) # Include any additional context data passed to the view
|
||
|
|
return context
|
||
|
|
|
||
|
|
def get(self, request, *args, **kwargs):
|
||
|
|
self.object = self.get_object()
|
||
|
|
|
||
|
|
# If an object is found, change action to ACTION_UPDATE
|
||
|
|
# if self.object is not None:
|
||
|
|
# self.action = resource_action.ACTION_UPDATE
|
||
|
|
|
||
|
|
form = self.form_class(instance=self.object)
|
||
|
|
context = self.get_context_data(form=form)
|
||
|
|
return render(request, self.template_name, context=context)
|
||
|
|
|
||
|
|
def post(self, request, *args, **kwargs):
|
||
|
|
self.object = self.get_object()
|
||
|
|
|
||
|
|
# If an object is found, change action to ACTION_UPDATE
|
||
|
|
# if self.object is not None:
|
||
|
|
# self.action = resource_action.ACTION_UPDATE
|
||
|
|
|
||
|
|
form = self.form_class(request.POST, instance=self.object)
|
||
|
|
if not form.is_valid():
|
||
|
|
print(form.errors)
|
||
|
|
context = self.get_context_data(form=form)
|
||
|
|
return render(request, self.template_name, context=context)
|
||
|
|
|
||
|
|
form.save()
|
||
|
|
messages.success(self.request, self.get_success_message())
|
||
|
|
return redirect(self.success_url)
|
||
|
|
|
||
|
|
|
||
|
|
class PrivacyPolicyView(LoginRequiredMixin, generic.DetailView):
|
||
|
|
page_name = None
|
||
|
|
resource = None
|
||
|
|
action = None
|
||
|
|
template_name = "module_cms/privacy_policy_view.html"
|
||
|
|
model = Organization
|
||
|
|
context_object_name = "organization"
|
||
|
|
|
||
|
|
def get_object(self, queryset=None):
|
||
|
|
return self.model.objects.only("privacy_policy").first()
|
||
|
|
|
||
|
|
def get_context_data(self, **kwargs):
|
||
|
|
context = super().get_context_data(**kwargs)
|
||
|
|
context["page_name"] = self.page_name
|
||
|
|
return context
|
||
|
|
|
||
|
|
|
||
|
|
class PrivacyPolicyCreateOrUpdateView(LoginRequiredMixin, generic.View):
|
||
|
|
# Set the page_name and resource
|
||
|
|
page_name = None
|
||
|
|
resource = None
|
||
|
|
|
||
|
|
# Initialize the action as ACTION_CREATE (can change based on logic)
|
||
|
|
action = None # Default action
|
||
|
|
|
||
|
|
template_name = "module_cms/privacy_policy_edit.html"
|
||
|
|
model = Organization
|
||
|
|
form_class = PrivacyPolicyForm
|
||
|
|
success_url = reverse_lazy("module_cms:privacy_policy")
|
||
|
|
error_message = "An error occurred while saving the data."
|
||
|
|
|
||
|
|
# Determine the success message dynamically based on whether it's an update or create
|
||
|
|
def get_success_message(self):
|
||
|
|
self.success_message = (
|
||
|
|
constants.RECORD_CREATED if not self.object else constants.RECORD_UPDATED
|
||
|
|
)
|
||
|
|
return self.success_message
|
||
|
|
|
||
|
|
# Get the object (if exists) based on URL parameter 'pk'
|
||
|
|
def get_object(self):
|
||
|
|
return self.model.objects.only("privacy_policy").first()
|
||
|
|
|
||
|
|
# Add page_name and operation to the context
|
||
|
|
def get_context_data(self, **kwargs):
|
||
|
|
context = {
|
||
|
|
"page_name": self.page_name,
|
||
|
|
"operation": "Add" if not self.object else "Edit",
|
||
|
|
}
|
||
|
|
context.update(kwargs) # Include any additional context data passed to the view
|
||
|
|
return context
|
||
|
|
|
||
|
|
def get(self, request, *args, **kwargs):
|
||
|
|
self.object = self.get_object()
|
||
|
|
|
||
|
|
# If an object is found, change action to ACTION_UPDATE
|
||
|
|
if self.object is not None:
|
||
|
|
self.action = None
|
||
|
|
|
||
|
|
form = self.form_class(instance=self.object)
|
||
|
|
context = self.get_context_data(form=form)
|
||
|
|
return render(request, self.template_name, context=context)
|
||
|
|
|
||
|
|
def post(self, request, *args, **kwargs):
|
||
|
|
self.object = self.get_object()
|
||
|
|
|
||
|
|
# If an object is found, change action to ACTION_UPDATE
|
||
|
|
if self.object is not None:
|
||
|
|
self.action = None
|
||
|
|
|
||
|
|
form = self.form_class(request.POST, instance=self.object)
|
||
|
|
if not form.is_valid():
|
||
|
|
print(form.errors)
|
||
|
|
context = self.get_context_data(form=form)
|
||
|
|
return render(request, self.template_name, context=context)
|
||
|
|
form.save()
|
||
|
|
messages.success(self.request, self.get_success_message())
|
||
|
|
return redirect(self.success_url)
|