Skip to content

Commit 6a085e5

Browse files
committed
Refactor the Ajax mixin into HTMXFormMixin
Signed-off-by: tdruez <[email protected]>
1 parent 32216a9 commit 6a085e5

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

scanpipe/views.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -561,23 +561,21 @@ def get(self, request, *args, **kwargs):
561561
return response
562562

563563

564-
class FormAjaxMixin:
565-
def is_xhr(self):
566-
return self.request.META.get("HTTP_X_REQUESTED_WITH") == "XMLHttpRequest"
567-
564+
class HTMXFormMixin:
568565
def form_valid(self, form):
569566
response = super().form_valid(form)
570567

571-
if self.is_xhr():
572-
return JsonResponse({"redirect_url": self.get_success_url()}, status=201)
568+
if self.request.htmx:
569+
return HttpResponseClientRedirect(self.get_success_url())
573570

574571
return response
575572

576573
def form_invalid(self, form):
577574
response = super().form_invalid(form)
578575

579-
if self.is_xhr():
580-
return JsonResponse({"errors": str(form.errors)}, status=400)
576+
if self.request.htmx:
577+
# Re-render the form with errors
578+
return self.render_to_response(self.get_context_data(form=form))
581579

582580
return response
583581

@@ -697,7 +695,7 @@ def get_queryset(self):
697695
)
698696

699697

700-
class ProjectCreateView(ConditionalLoginRequired, FormAjaxMixin, generic.CreateView):
698+
class ProjectCreateView(ConditionalLoginRequired, generic.CreateView):
701699
model = Project
702700
form_class = ProjectForm
703701
template_name = "scanpipe/project_form.html"
@@ -716,6 +714,23 @@ def get_context_data(self, **kwargs):
716714
context["pipelines_available_groups"] = pipelines_available_groups
717715
return context
718716

717+
@property
718+
def is_xhr(self):
719+
# The request is XMLHttpRequest when using the input upload progress
720+
return self.request.META.get("HTTP_X_REQUESTED_WITH") == "XMLHttpRequest"
721+
722+
def form_valid(self, form):
723+
response = super().form_valid(form)
724+
if self.is_xhr:
725+
return JsonResponse({"redirect_url": self.get_success_url()}, status=201)
726+
return response
727+
728+
def form_invalid(self, form):
729+
response = super().form_invalid(form)
730+
if self.is_xhr:
731+
return JsonResponse({"errors": str(form.errors)}, status=400)
732+
return response
733+
719734

720735
class ProjectDetailView(ConditionalLoginRequired, generic.DetailView):
721736
model = Project
@@ -958,7 +973,7 @@ def download_config_file(project):
958973

959974

960975
class ProjectSettingsWebhookCreateView(
961-
ConditionalLoginRequired, FormAjaxMixin, UpdateView
976+
ConditionalLoginRequired, HTMXFormMixin, UpdateView
962977
):
963978
model = Project
964979
form_class = WebhookSubscriptionForm
@@ -1466,15 +1481,11 @@ def xlsx_report_response(project_qs, action_form):
14661481
)
14671482

14681483

1469-
class ProjectCloneView(ConditionalLoginRequired, FormAjaxMixin, UpdateView):
1484+
class ProjectCloneView(ConditionalLoginRequired, HTMXFormMixin, UpdateView):
14701485
model = Project
14711486
form_class = ProjectCloneForm
14721487
template_name = "scanpipe/forms/project_clone_form.html"
14731488

1474-
def form_valid(self, form):
1475-
super().form_valid(form)
1476-
return HttpResponseClientRedirect(self.get_success_url())
1477-
14781489

14791490
@conditional_login_required
14801491
def execute_pipelines_view(request, slug):

0 commit comments

Comments
 (0)