Make variables passed to CRUD helpers consistent

This commit is contained in:
2022-12-18 17:37:34 +00:00
parent 246674b03e
commit 8de99c1bcd
8 changed files with 25 additions and 65 deletions

View File

@@ -185,15 +185,18 @@ class ObjectCreate(RestrictedViewMixin, ObjectNameMixin, CreateView):
window_content = "window-content/object-form.html"
parser_classes = [FormParser]
page_title = None
page_subtitle = None
model = None
submit_url_name = None
list_url_name = None
# WARNING: TAKEN FROM locals()
list_url_args = ["type"]
submit_url_args = ["type"]
request = None
# Whether to hide the cancel button in the form
hide_cancel = False
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.title = "Create " + self.context_object_name_singular
@@ -230,17 +233,22 @@ class ObjectCreate(RestrictedViewMixin, ObjectNameMixin, CreateView):
self.request = request
self.kwargs = kwargs
list_url_args = {}
for arg in self.list_url_args:
list_url_args[arg] = locals()[arg]
if type == "widget":
self.hide_cancel = True
if type == "page":
type = "modal"
self.object = None
submit_url = reverse(self.submit_url_name, kwargs={"type": type})
list_url = reverse(self.list_url_name, kwargs=list_url_args)
submit_url_args = {}
for arg in self.submit_url_args:
if arg in locals():
submit_url_args[arg] = locals()[arg]
elif arg in kwargs:
submit_url_args[arg] = kwargs[arg]
submit_url = reverse(self.submit_url_name, kwargs=submit_url_args)
context = self.get_context_data()
form = kwargs.get("form", None)
if form:
@@ -250,8 +258,12 @@ class ObjectCreate(RestrictedViewMixin, ObjectNameMixin, CreateView):
context["context_object_name"] = self.context_object_name
context["context_object_name_singular"] = self.context_object_name_singular
context["submit_url"] = submit_url
context["list_url"] = list_url
context["type"] = type
context["hide_cancel"] = self.hide_cancel
if self.page_title:
context["page_title"] = self.page_title
if self.page_subtitle:
context["page_subtitle"] = self.page_subtitle
response = self.render_to_response(context)
# response["HX-Trigger"] = f"{self.context_object_name_singular}Event"
return response
@@ -392,6 +404,9 @@ class ObjectUpdate(RestrictedViewMixin, ObjectNameMixin, UpdateView):
return HttpResponseBadRequest("Invalid type specified")
self.template_name = f"wm/{type}.html"
unique = str(uuid.uuid4())[:8]
if type == "widget":
self.hide_cancel = True
if type == "page":
type = "modal"