2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-18 13:05:42 +00:00

Refactor how form errors are handled

- Use form.add_error (as the django gods intended)
This commit is contained in:
Oliver Walters
2020-10-28 23:33:33 +11:00
parent fb28204dfd
commit c533f59405
8 changed files with 80 additions and 65 deletions

View File

@ -74,7 +74,7 @@ class BuildCancel(AjaxUpdateView):
if confirm:
build.cancelBuild(request.user)
else:
form.errors['confirm_cancel'] = [_('Confirm build cancellation')]
form.add_error('confirm_cancel', _('Confirm build cancellation'))
valid = False
data = {
@ -128,8 +128,8 @@ class BuildAutoAllocate(AjaxUpdateView):
valid = False
if confirm is False:
form.errors['confirm'] = [_('Confirm stock allocation')]
form.non_field_errors = [_('Check the confirmation box at the bottom of the list')]
form.add_error('confirm', _('Confirm stock allocation'))
form.add_error(None, _('Check the confirmation box at the bottom of the list'))
else:
build.autoAllocate()
valid = True
@ -163,8 +163,8 @@ class BuildUnallocate(AjaxUpdateView):
valid = False
if confirm is False:
form.errors['confirm'] = [_('Confirm unallocation of build stock')]
form.non_field_errors = [_('Check the confirmation box')]
form.add_error('confirm', _('Confirm unallocation of build stock'))
form.add_error(None, _('Check the confirmation box'))
else:
build.unallocateStock()
valid = True
@ -266,15 +266,13 @@ class BuildComplete(AjaxUpdateView):
valid = False
if confirm is False:
form.errors['confirm'] = [
_('Confirm completion of build'),
]
form.add_error('confirm', _('Confirm completion of build'))
else:
try:
location = StockLocation.objects.get(id=loc_id)
valid = True
except (ValueError, StockLocation.DoesNotExist):
form.errors['location'] = [_('Invalid location selected')]
form.add_error('location', _('Invalid location selected'))
serials = []
@ -291,24 +289,20 @@ class BuildComplete(AjaxUpdateView):
# Exctract a list of provided serial numbers
serials = ExtractSerialNumbers(sn, build.quantity)
existing = []
for serial in serials:
if build.part.checkIfSerialNumberExists(serial):
existing.append(serial)
existing = build.part.find_conflicting_serial_numbers(serials)
if len(existing) > 0:
exists = ",".join([str(x) for x in existing])
form.errors['serial_numbers'] = [_('The following serial numbers already exist: ({sn})'.format(sn=exists))]
form.add_error('serial_numbers', _('The following serial numbers already exist: ({sn})'.format(sn=exists)))
valid = False
except ValidationError as e:
form.errors['serial_numbers'] = e.messages
form.add_error('serial_numbers', e.messages)
valid = False
if valid:
if not build.completeBuild(location, serials, request.user):
form.non_field_errors = [('Build could not be completed')]
form.add_error(None, _('Build could not be completed'))
valid = False
data = {