2022-10-29 11:43:13 +00:00
|
|
|
import uuid
|
|
|
|
|
2022-11-28 18:09:41 +00:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
|
|
|
from django.core.paginator import Paginator
|
|
|
|
from django.db.models import QuerySet
|
2022-12-08 07:20:46 +00:00
|
|
|
from django.http import Http404, HttpResponse, HttpResponseBadRequest
|
2022-10-29 11:43:13 +00:00
|
|
|
from django.urls import reverse
|
|
|
|
from django.views.generic.detail import DetailView
|
|
|
|
from django.views.generic.edit import CreateView, DeleteView, UpdateView
|
|
|
|
from django.views.generic.list import ListView
|
|
|
|
from rest_framework.parsers import FormParser
|
|
|
|
|
|
|
|
from core.util import logs
|
|
|
|
|
|
|
|
log = logs.get_logger(__name__)
|
|
|
|
|
|
|
|
|
2022-11-28 18:09:41 +00:00
|
|
|
class RestrictedViewMixin:
|
|
|
|
"""
|
|
|
|
This mixin overrides two helpers in order to pass the user object to the filters.
|
|
|
|
get_queryset alters the objects returned for list views.
|
|
|
|
get_form_kwargs passes the request object to the form class. Remaining permissions
|
|
|
|
checks are in forms.py
|
|
|
|
"""
|
|
|
|
|
|
|
|
allow_empty = True
|
|
|
|
queryset = None
|
|
|
|
model = None
|
|
|
|
paginate_by = None
|
|
|
|
paginate_orphans = 0
|
|
|
|
context_object_name = None
|
|
|
|
paginator_class = Paginator
|
|
|
|
page_kwarg = "page"
|
|
|
|
ordering = None
|
|
|
|
|
2022-12-08 07:20:07 +00:00
|
|
|
def get_queryset(self, **kwargs):
|
2022-11-28 18:09:41 +00:00
|
|
|
"""
|
|
|
|
This function is overriden to filter the objects by the requesting user.
|
|
|
|
"""
|
|
|
|
if self.queryset is not None:
|
|
|
|
queryset = self.queryset
|
|
|
|
if isinstance(queryset, QuerySet):
|
|
|
|
# queryset = queryset.all()
|
|
|
|
queryset = queryset.filter(user=self.request.user)
|
|
|
|
elif self.model is not None:
|
|
|
|
queryset = self.model._default_manager.filter(user=self.request.user)
|
|
|
|
else:
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
"%(cls)s is missing a QuerySet. Define "
|
|
|
|
"%(cls)s.model, %(cls)s.queryset, or override "
|
|
|
|
"%(cls)s.get_queryset()." % {"cls": self.__class__.__name__}
|
|
|
|
)
|
|
|
|
if hasattr(self, "get_ordering"):
|
|
|
|
ordering = self.get_ordering()
|
|
|
|
if ordering:
|
|
|
|
if isinstance(ordering, str):
|
|
|
|
ordering = (ordering,)
|
|
|
|
queryset = queryset.order_by(*ordering)
|
|
|
|
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
def get_form_kwargs(self):
|
|
|
|
"""Passes the request object to the form class.
|
|
|
|
This is necessary to only display members that belong to a given user"""
|
|
|
|
|
|
|
|
kwargs = super().get_form_kwargs()
|
|
|
|
kwargs["request"] = self.request
|
|
|
|
return kwargs
|
|
|
|
|
|
|
|
|
2022-11-04 07:20:42 +00:00
|
|
|
class ObjectNameMixin(object):
|
|
|
|
def __init__(self, *args, **kwargs):
|
2022-12-08 07:20:07 +00:00
|
|
|
if self.model is None:
|
|
|
|
self.title = self.context_object_name.title()
|
|
|
|
self.title_singular = self.context_object_name_singular.title()
|
|
|
|
else:
|
|
|
|
self.title_singular = self.model._meta.verbose_name.title() # Hook
|
|
|
|
self.context_object_name_singular = self.title_singular.lower() # hook
|
|
|
|
self.title = self.model._meta.verbose_name_plural.title() # Hooks
|
|
|
|
self.context_object_name = self.title.lower() # hooks
|
|
|
|
|
|
|
|
self.context_object_name = self.context_object_name.replace(" ", "")
|
|
|
|
self.context_object_name_singular = (
|
|
|
|
self.context_object_name_singular.replace(" ", "")
|
|
|
|
)
|
2022-11-04 07:20:42 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
|
2022-11-28 18:09:41 +00:00
|
|
|
class ObjectList(RestrictedViewMixin, ObjectNameMixin, ListView):
|
2022-10-29 11:43:13 +00:00
|
|
|
allowed_types = ["modal", "widget", "window", "page"]
|
|
|
|
window_content = "window-content/objects.html"
|
|
|
|
list_template = None
|
|
|
|
|
|
|
|
page_title = None
|
|
|
|
page_subtitle = None
|
|
|
|
|
2022-10-29 13:05:01 +00:00
|
|
|
list_url_name = None
|
|
|
|
# WARNING: TAKEN FROM locals()
|
|
|
|
list_url_args = ["type"]
|
|
|
|
|
2022-10-29 11:43:13 +00:00
|
|
|
submit_url_name = None
|
|
|
|
|
2022-10-30 10:57:41 +00:00
|
|
|
delete_all_url_name = None
|
2022-12-08 07:20:07 +00:00
|
|
|
widget_options = None
|
2022-10-30 10:57:41 +00:00
|
|
|
|
2022-10-29 11:43:13 +00:00
|
|
|
# copied from BaseListView
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
type = kwargs.get("type", None)
|
|
|
|
if not type:
|
|
|
|
return HttpResponseBadRequest("No type specified")
|
|
|
|
if type not in self.allowed_types:
|
|
|
|
return HttpResponseBadRequest("Invalid type specified")
|
2022-12-08 07:20:46 +00:00
|
|
|
|
|
|
|
self.request = request
|
|
|
|
self.object_list = self.get_queryset(**kwargs)
|
|
|
|
if isinstance(self.object_list, HttpResponse):
|
|
|
|
return self.object_list
|
|
|
|
if isinstance(self.object_list, HttpResponseBadRequest):
|
|
|
|
return self.object_list
|
|
|
|
allow_empty = self.get_allow_empty()
|
|
|
|
|
2022-10-29 11:43:13 +00:00
|
|
|
self.template_name = f"wm/{type}.html"
|
|
|
|
unique = str(uuid.uuid4())[:8]
|
2022-10-29 13:05:01 +00:00
|
|
|
|
|
|
|
list_url_args = {}
|
|
|
|
for arg in self.list_url_args:
|
2022-12-08 07:20:07 +00:00
|
|
|
if arg in locals():
|
|
|
|
list_url_args[arg] = locals()[arg]
|
2022-12-18 15:10:28 +00:00
|
|
|
elif arg in kwargs:
|
|
|
|
list_url_args[arg] = kwargs[arg]
|
2022-10-29 13:05:01 +00:00
|
|
|
|
2022-11-14 17:14:47 +00:00
|
|
|
orig_type = type
|
2022-10-29 11:43:13 +00:00
|
|
|
if type == "page":
|
|
|
|
type = "modal"
|
|
|
|
|
|
|
|
if not allow_empty:
|
|
|
|
# When pagination is enabled and object_list is a queryset,
|
|
|
|
# it's better to do a cheap query than to load the unpaginated
|
|
|
|
# queryset in memory.
|
|
|
|
if self.get_paginate_by(self.object_list) is not None and hasattr(
|
|
|
|
self.object_list, "exists"
|
|
|
|
):
|
|
|
|
is_empty = not self.object_list.exists()
|
|
|
|
else:
|
|
|
|
is_empty = not self.object_list
|
|
|
|
if is_empty:
|
|
|
|
raise Http404("Empty list")
|
2022-10-29 13:05:01 +00:00
|
|
|
|
2022-10-29 11:43:13 +00:00
|
|
|
context = self.get_context_data()
|
|
|
|
context["title"] = self.title + f" ({type})"
|
|
|
|
context["title_singular"] = self.title_singular
|
|
|
|
context["unique"] = unique
|
|
|
|
context["window_content"] = self.window_content
|
|
|
|
context["list_template"] = self.list_template
|
|
|
|
context["page_title"] = self.page_title
|
|
|
|
context["page_subtitle"] = self.page_subtitle
|
|
|
|
context["type"] = type
|
2022-10-29 13:05:01 +00:00
|
|
|
context["context_object_name"] = self.context_object_name
|
|
|
|
context["context_object_name_singular"] = self.context_object_name_singular
|
2022-12-08 07:20:07 +00:00
|
|
|
|
|
|
|
if self.submit_url_name is not None:
|
|
|
|
context["submit_url"] = reverse(self.submit_url_name, kwargs={"type": type})
|
|
|
|
|
|
|
|
if self.list_url_name is not None:
|
|
|
|
context["list_url"] = reverse(self.list_url_name, kwargs=list_url_args)
|
|
|
|
|
2022-10-30 10:57:41 +00:00
|
|
|
if self.delete_all_url_name:
|
|
|
|
context["delete_all_url"] = reverse(self.delete_all_url_name)
|
2022-12-08 07:20:07 +00:00
|
|
|
if self.widget_options:
|
|
|
|
context["widget_options"] = self.widget_options
|
2022-10-29 13:05:01 +00:00
|
|
|
|
|
|
|
# Return partials for HTMX
|
|
|
|
if self.request.htmx:
|
2022-11-29 07:20:39 +00:00
|
|
|
if request.headers["HX-Target"] == self.context_object_name + "-table":
|
|
|
|
self.template_name = self.list_template
|
|
|
|
elif orig_type == "page":
|
2022-11-14 17:14:47 +00:00
|
|
|
self.template_name = self.list_template
|
|
|
|
else:
|
|
|
|
context["window_content"] = self.list_template
|
2022-10-29 11:43:13 +00:00
|
|
|
return self.render_to_response(context)
|
|
|
|
|
|
|
|
|
2022-11-28 18:09:41 +00:00
|
|
|
class ObjectCreate(RestrictedViewMixin, ObjectNameMixin, CreateView):
|
2022-10-29 11:43:13 +00:00
|
|
|
allowed_types = ["modal", "widget", "window", "page"]
|
|
|
|
window_content = "window-content/object-form.html"
|
|
|
|
parser_classes = [FormParser]
|
|
|
|
|
|
|
|
model = None
|
|
|
|
submit_url_name = None
|
|
|
|
|
2022-10-29 13:05:01 +00:00
|
|
|
list_url_name = None
|
|
|
|
# WARNING: TAKEN FROM locals()
|
|
|
|
list_url_args = ["type"]
|
|
|
|
|
2022-10-29 11:43:13 +00:00
|
|
|
request = None
|
|
|
|
|
2022-12-08 07:20:46 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.title = "Create " + self.context_object_name_singular
|
|
|
|
|
2022-10-30 10:57:41 +00:00
|
|
|
def post_save(self, obj):
|
|
|
|
pass
|
|
|
|
|
2022-10-29 11:43:13 +00:00
|
|
|
def form_valid(self, form):
|
|
|
|
obj = form.save(commit=False)
|
|
|
|
if self.request is None:
|
|
|
|
raise Exception("Request is None")
|
|
|
|
obj.user = self.request.user
|
|
|
|
obj.save()
|
|
|
|
form.save_m2m()
|
2022-10-30 10:57:41 +00:00
|
|
|
self.post_save(obj)
|
2022-10-29 11:43:13 +00:00
|
|
|
context = {"message": "Object created", "class": "success"}
|
2022-10-29 13:05:01 +00:00
|
|
|
response = self.render_to_response(context)
|
|
|
|
response["HX-Trigger"] = f"{self.context_object_name_singular}Event"
|
|
|
|
return response
|
2022-10-29 11:43:13 +00:00
|
|
|
|
2022-11-25 18:01:13 +00:00
|
|
|
def form_invalid(self, form):
|
|
|
|
"""If the form is invalid, render the invalid form."""
|
|
|
|
return self.get(self.request, **self.kwargs, form=form)
|
|
|
|
|
2022-10-29 11:43:13 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
type = kwargs.get("type", None)
|
|
|
|
if not type:
|
|
|
|
return HttpResponseBadRequest("No type specified")
|
|
|
|
if type not in self.allowed_types:
|
|
|
|
return HttpResponseBadRequest("Invalid type specified")
|
|
|
|
self.template_name = f"wm/{type}.html"
|
|
|
|
unique = str(uuid.uuid4())[:8]
|
2022-10-29 13:05:01 +00:00
|
|
|
|
2022-12-08 07:20:46 +00:00
|
|
|
self.request = request
|
|
|
|
self.kwargs = kwargs
|
|
|
|
|
2022-10-29 13:05:01 +00:00
|
|
|
list_url_args = {}
|
|
|
|
for arg in self.list_url_args:
|
|
|
|
list_url_args[arg] = locals()[arg]
|
|
|
|
|
2022-10-29 11:43:13 +00:00
|
|
|
if type == "page":
|
|
|
|
type = "modal"
|
|
|
|
|
|
|
|
self.object = None
|
|
|
|
submit_url = reverse(self.submit_url_name, kwargs={"type": type})
|
2022-10-29 13:05:01 +00:00
|
|
|
|
|
|
|
list_url = reverse(self.list_url_name, kwargs=list_url_args)
|
2022-10-29 11:43:13 +00:00
|
|
|
context = self.get_context_data()
|
2022-11-25 18:01:13 +00:00
|
|
|
form = kwargs.get("form", None)
|
|
|
|
if form:
|
|
|
|
context["form"] = form
|
2022-10-29 11:43:13 +00:00
|
|
|
context["unique"] = unique
|
|
|
|
context["window_content"] = self.window_content
|
|
|
|
context["context_object_name"] = self.context_object_name
|
2022-10-29 13:05:01 +00:00
|
|
|
context["context_object_name_singular"] = self.context_object_name_singular
|
2022-10-29 11:43:13 +00:00
|
|
|
context["submit_url"] = submit_url
|
2022-10-29 13:05:01 +00:00
|
|
|
context["list_url"] = list_url
|
2022-10-29 11:43:13 +00:00
|
|
|
context["type"] = type
|
2022-11-29 07:20:39 +00:00
|
|
|
response = self.render_to_response(context)
|
|
|
|
# response["HX-Trigger"] = f"{self.context_object_name_singular}Event"
|
|
|
|
return response
|
2022-10-29 11:43:13 +00:00
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
self.request = request
|
|
|
|
self.template_name = "partials/notify.html"
|
|
|
|
return super().post(request, *args, **kwargs)
|
|
|
|
|
|
|
|
|
2022-11-28 18:09:41 +00:00
|
|
|
class ObjectRead(RestrictedViewMixin, ObjectNameMixin, DetailView):
|
2022-10-29 11:43:13 +00:00
|
|
|
allowed_types = ["modal", "widget", "window", "page"]
|
|
|
|
window_content = "window-content/object.html"
|
2022-12-08 07:20:46 +00:00
|
|
|
detail_template = "partials/generic-detail.html"
|
|
|
|
|
|
|
|
page_title = None
|
|
|
|
page_subtitle = None
|
2022-10-29 11:43:13 +00:00
|
|
|
|
|
|
|
model = None
|
2022-12-08 07:20:46 +00:00
|
|
|
# submit_url_name = None
|
|
|
|
|
|
|
|
detail_url_name = None
|
|
|
|
# WARNING: TAKEN FROM locals()
|
|
|
|
detail_url_args = ["type"]
|
|
|
|
|
|
|
|
request = None
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
type = kwargs.get("type", None)
|
|
|
|
if not type:
|
|
|
|
return HttpResponseBadRequest("No type specified")
|
|
|
|
if type not in self.allowed_types:
|
|
|
|
return HttpResponseBadRequest()
|
|
|
|
self.template_name = f"wm/{type}.html"
|
|
|
|
unique = str(uuid.uuid4())[:8]
|
|
|
|
|
|
|
|
detail_url_args = {}
|
|
|
|
for arg in self.detail_url_args:
|
|
|
|
if arg in locals():
|
|
|
|
detail_url_args[arg] = locals()[arg]
|
|
|
|
elif arg in kwargs:
|
|
|
|
detail_url_args[arg] = kwargs[arg]
|
|
|
|
|
|
|
|
self.request = request
|
|
|
|
self.object = self.get_object(**kwargs)
|
|
|
|
if isinstance(self.object, HttpResponse):
|
|
|
|
return self.object
|
|
|
|
|
|
|
|
orig_type = type
|
|
|
|
if type == "page":
|
|
|
|
type = "modal"
|
|
|
|
|
|
|
|
context = self.get_context_data()
|
|
|
|
|
|
|
|
context["title"] = self.title + f" ({type})"
|
|
|
|
context["title_singular"] = self.title_singular
|
|
|
|
context["unique"] = unique
|
|
|
|
context["window_content"] = self.window_content
|
|
|
|
context["detail_template"] = self.detail_template
|
|
|
|
if self.page_title:
|
|
|
|
context["page_title"] = self.page_title
|
|
|
|
if self.page_subtitle:
|
|
|
|
context["page_subtitle"] = self.page_subtitle
|
|
|
|
context["type"] = type
|
|
|
|
context["context_object_name"] = self.context_object_name
|
|
|
|
context["context_object_name_singular"] = self.context_object_name_singular
|
|
|
|
|
|
|
|
if self.detail_url_name is not None:
|
|
|
|
context["detail_url"] = reverse(
|
|
|
|
self.detail_url_name, kwargs=detail_url_args
|
|
|
|
)
|
|
|
|
|
|
|
|
# Return partials for HTMX
|
|
|
|
if self.request.htmx:
|
|
|
|
if request.headers["HX-Target"] == self.context_object_name + "-info":
|
|
|
|
self.template_name = self.detail_template
|
|
|
|
elif orig_type == "page":
|
|
|
|
self.template_name = self.detail_template
|
|
|
|
else:
|
|
|
|
context["window_content"] = self.detail_template
|
|
|
|
|
|
|
|
return self.render_to_response(context)
|
2022-10-29 11:43:13 +00:00
|
|
|
|
|
|
|
|
2022-11-28 18:09:41 +00:00
|
|
|
class ObjectUpdate(RestrictedViewMixin, ObjectNameMixin, UpdateView):
|
2022-10-29 11:43:13 +00:00
|
|
|
allowed_types = ["modal", "widget", "window", "page"]
|
|
|
|
window_content = "window-content/object-form.html"
|
|
|
|
parser_classes = [FormParser]
|
|
|
|
|
|
|
|
model = None
|
|
|
|
submit_url_name = None
|
|
|
|
|
|
|
|
request = None
|
|
|
|
|
2022-12-08 07:20:46 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.title = "Update " + self.context_object_name_singular
|
|
|
|
|
2022-10-30 10:57:41 +00:00
|
|
|
def post_save(self, obj):
|
|
|
|
pass
|
|
|
|
|
2022-10-29 11:43:13 +00:00
|
|
|
def form_valid(self, form):
|
|
|
|
obj = form.save(commit=False)
|
|
|
|
if self.request is None:
|
|
|
|
raise Exception("Request is None")
|
|
|
|
obj.save()
|
|
|
|
form.save_m2m()
|
2022-10-30 10:57:41 +00:00
|
|
|
self.post_save(obj)
|
2022-10-29 11:43:13 +00:00
|
|
|
context = {"message": "Object updated", "class": "success"}
|
2022-10-29 13:05:01 +00:00
|
|
|
response = self.render_to_response(context)
|
|
|
|
response["HX-Trigger"] = f"{self.context_object_name_singular}Event"
|
|
|
|
return response
|
2022-10-29 11:43:13 +00:00
|
|
|
|
2022-11-25 18:01:13 +00:00
|
|
|
def form_invalid(self, form):
|
|
|
|
"""If the form is invalid, render the invalid form."""
|
|
|
|
return self.get(self.request, **self.kwargs, form=form)
|
|
|
|
|
2022-10-29 11:43:13 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
self.request = request
|
|
|
|
type = kwargs.get("type", None)
|
|
|
|
pk = kwargs.get("pk", None)
|
|
|
|
if not type:
|
|
|
|
return HttpResponseBadRequest("No type specified")
|
|
|
|
if not pk:
|
|
|
|
return HttpResponseBadRequest("No pk specified")
|
|
|
|
if type not in self.allowed_types:
|
|
|
|
return HttpResponseBadRequest("Invalid type specified")
|
|
|
|
self.template_name = f"wm/{type}.html"
|
|
|
|
unique = str(uuid.uuid4())[:8]
|
|
|
|
if type == "page":
|
|
|
|
type = "modal"
|
|
|
|
|
|
|
|
self.object = self.get_object()
|
|
|
|
submit_url = reverse(self.submit_url_name, kwargs={"type": type, "pk": pk})
|
|
|
|
context = self.get_context_data()
|
2022-11-25 18:01:13 +00:00
|
|
|
form = kwargs.get("form", None)
|
|
|
|
if form:
|
|
|
|
context["form"] = form
|
2022-12-08 07:20:46 +00:00
|
|
|
context["title"] = self.title + f" ({type})"
|
|
|
|
context["title_singular"] = self.title_singular
|
2022-10-29 11:43:13 +00:00
|
|
|
context["unique"] = unique
|
|
|
|
context["window_content"] = self.window_content
|
|
|
|
context["context_object_name"] = self.context_object_name
|
2022-10-29 13:05:01 +00:00
|
|
|
context["context_object_name_singular"] = self.context_object_name_singular
|
2022-10-29 11:43:13 +00:00
|
|
|
context["submit_url"] = submit_url
|
|
|
|
context["type"] = type
|
2022-11-29 07:20:39 +00:00
|
|
|
response = self.render_to_response(context)
|
|
|
|
# response["HX-Trigger"] = f"{self.context_object_name_singular}Event"
|
|
|
|
return response
|
2022-10-29 11:43:13 +00:00
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
self.request = request
|
|
|
|
self.template_name = "partials/notify.html"
|
|
|
|
return super().post(request, *args, **kwargs)
|
|
|
|
|
|
|
|
|
2022-11-28 18:09:41 +00:00
|
|
|
class ObjectDelete(RestrictedViewMixin, ObjectNameMixin, DeleteView):
|
2022-10-29 11:43:13 +00:00
|
|
|
model = None
|
|
|
|
template_name = "partials/notify.html"
|
|
|
|
|
|
|
|
# Overriden to prevent success URL from being used
|
|
|
|
def delete(self, request, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Call the delete() method on the fetched object and then redirect to the
|
|
|
|
success URL.
|
|
|
|
"""
|
|
|
|
self.object = self.get_object()
|
|
|
|
# success_url = self.get_success_url()
|
|
|
|
self.object.delete()
|
|
|
|
context = {"message": "Object deleted", "class": "success"}
|
2022-10-29 13:05:01 +00:00
|
|
|
response = self.render_to_response(context)
|
|
|
|
response["HX-Trigger"] = f"{self.context_object_name_singular}Event"
|
|
|
|
return response
|
2022-10-29 11:43:13 +00:00
|
|
|
|
|
|
|
# This will be used in newer Django versions, until then we get a warning
|
|
|
|
def form_valid(self, form):
|
|
|
|
"""
|
|
|
|
Call the delete() method on the fetched object.
|
|
|
|
"""
|
|
|
|
self.object = self.get_object()
|
|
|
|
self.object.delete()
|
|
|
|
context = {"message": "Object deleted", "class": "success"}
|
2022-10-29 13:05:01 +00:00
|
|
|
response = self.render_to_response(context)
|
|
|
|
response["HX-Trigger"] = f"{self.context_object_name_singular}Event"
|
|
|
|
return response
|