Skip to content

Commit

Permalink
feat(generic): make importer return a message on error
Browse files Browse the repository at this point in the history
The Select2QuerysetView does not handle errors well. We override the
`post` method of the view, so we can catch exceptions and add an error
message to the response. Once the custom select2js is updated, the error
messge will be displayed in the select input field.
  • Loading branch information
b1rger committed Jun 26, 2024
1 parent cd36424 commit c60ad36
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions apis_core/generic/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django import http
from django.conf import settings
from django.contrib.auth.mixins import PermissionRequiredMixin
from django.views.generic import DetailView
Expand Down Expand Up @@ -246,6 +247,12 @@ def get_results(self, context):
def create_object(self, value):
return create_object_from_uri(value, self.queryset.model)

def post(self, request, *args, **kwargs):
try:
return super().post(request, *args, **kwargs)
except Exception as e:
return http.JsonResponse({"error": str(e)})


class Import(GenericModelMixin, PermissionRequiredMixin, FormView):
template_name = "generic/generic_import_form.html"
Expand Down

0 comments on commit c60ad36

Please sign in to comment.