Re-render invalid forms and fix space handling

This commit is contained in:
Mark Veidemanis 2022-11-25 18:01:13 +00:00
parent b525611aaa
commit bcb3272064
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
1 changed files with 20 additions and 0 deletions

View File

@ -18,6 +18,11 @@ class ObjectNameMixin(object):
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(
" ", ""
)
super().__init__(*args, **kwargs)
@ -128,8 +133,13 @@ class ObjectCreate(ObjectNameMixin, CreateView):
response["HX-Trigger"] = f"{self.context_object_name_singular}Event"
return response
def form_invalid(self, form):
"""If the form is invalid, render the invalid form."""
return self.get(self.request, **self.kwargs, form=form)
def get(self, request, *args, **kwargs):
self.request = request
self.kwargs = kwargs
type = kwargs.get("type", None)
if not type:
return HttpResponseBadRequest("No type specified")
@ -150,6 +160,9 @@ class ObjectCreate(ObjectNameMixin, CreateView):
list_url = reverse(self.list_url_name, kwargs=list_url_args)
context = self.get_context_data()
form = kwargs.get("form", None)
if form:
context["form"] = form
context["unique"] = unique
context["window_content"] = self.window_content
context["context_object_name"] = self.context_object_name
@ -197,6 +210,10 @@ class ObjectUpdate(ObjectNameMixin, UpdateView):
response["HX-Trigger"] = f"{self.context_object_name_singular}Event"
return response
def form_invalid(self, form):
"""If the form is invalid, render the invalid form."""
return self.get(self.request, **self.kwargs, form=form)
def get(self, request, *args, **kwargs):
self.request = request
type = kwargs.get("type", None)
@ -215,6 +232,9 @@ class ObjectUpdate(ObjectNameMixin, UpdateView):
self.object = self.get_object()
submit_url = reverse(self.submit_url_name, kwargs={"type": type, "pk": pk})
context = self.get_context_data()
form = kwargs.get("form", None)
if form:
context["form"] = form
context["unique"] = unique
context["window_content"] = self.window_content
context["context_object_name"] = self.context_object_name