Caught and handled ValidationError

This commit is contained in:
Alyssa Bigley 2021-06-11 13:42:26 -04:00
parent 55b7cf21cc
commit 934543b595

View File

@ -670,9 +670,10 @@ class BulkImportView(GetReturnURLMixin, ObjectPermissionRequiredMixin, View):
from_form=self.model_form, from_form=self.model_form,
required=False required=False
) )
def used_both_methods(self):
def used_both_csv_fields(self):
if self.cleaned_data['csv_file'][1] and self.cleaned_data['csv'][1]: if self.cleaned_data['csv_file'][1] and self.cleaned_data['csv'][1]:
raise ValidationError('') return True
return False return False
return ImportForm(*args, **kwargs) return ImportForm(*args, **kwargs)
@ -700,10 +701,13 @@ class BulkImportView(GetReturnURLMixin, ObjectPermissionRequiredMixin, View):
new_objs = [] new_objs = []
form = self._import_form(request.POST, request.FILES) form = self._import_form(request.POST, request.FILES)
if form.is_valid() and not form.used_both_methods(): if form.is_valid():
logger.debug("Form validation was successful") logger.debug("Form validation was successful")
try: try:
if form.used_both_csv_fields():
form.add_error('csv_file', "Choose one of two import methods")
raise ValidationError("")
# Iterate through CSV data and bind each row to a new model form instance. # Iterate through CSV data and bind each row to a new model form instance.
with transaction.atomic(): with transaction.atomic():
if request.FILES: if request.FILES: