mirror of
https://github.com/inventree/InvenTree.git
synced 2025-07-01 03:00:54 +00:00
Refactor how form errors are handled
- When in doubt, refer to the django docs - There was a *much* better way (thanks, form.add_error)! - The anti-pattern was deleted, and lo, there was much rejoicing - Some other refactoring too
This commit is contained in:
@ -822,14 +822,11 @@ class StockItem(MPTTModel):
|
||||
raise ValidationError({"quantity": _("Quantity does not match serial numbers")})
|
||||
|
||||
# Test if each of the serial numbers are valid
|
||||
existing = []
|
||||
|
||||
for serial in serials:
|
||||
if self.part.checkIfSerialNumberExists(serial):
|
||||
existing.append(serial)
|
||||
existing = self.part.find_conflicting_serial_numbers(serials)
|
||||
|
||||
if len(existing) > 0:
|
||||
raise ValidationError({"serial_numbers": _("Serial numbers already exist: ") + str(existing)})
|
||||
exists = ','.join([str(x) for x in existing])
|
||||
raise ValidationError({"serial_numbers": _("Serial numbers already exist") + ': ' + exists})
|
||||
|
||||
# Create a new stock item for each unique serial number
|
||||
for serial in serials:
|
||||
|
@ -407,6 +407,13 @@ class VariantTest(StockTest):
|
||||
|
||||
self.assertEqual(chair.getLatestSerialNumber(), '22')
|
||||
|
||||
# Check for conflicting serial numbers
|
||||
to_check = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
|
||||
conflicts = chair.find_conflicting_serial_numbers(to_check)
|
||||
|
||||
self.assertEqual(len(conflicts), 6)
|
||||
|
||||
# Same operations on a sub-item
|
||||
variant = Part.objects.get(pk=10003)
|
||||
self.assertEqual(variant.getLatestSerialNumber(), '22')
|
||||
|
@ -417,8 +417,8 @@ class StockItemDeleteTestData(AjaxUpdateView):
|
||||
confirm = str2bool(request.POST.get('confirm', False))
|
||||
|
||||
if confirm is not True:
|
||||
form.errors['confirm'] = [_('Confirm test data deletion')]
|
||||
form.non_field_errors = [_('Check the confirmation box')]
|
||||
form.add_error('confirm', _('Confirm test data deletion'))
|
||||
form.add_error(None, _('Check the confirmation box'))
|
||||
else:
|
||||
stock_item.test_results.all().delete()
|
||||
valid = True
|
||||
@ -918,7 +918,7 @@ class StockItemUninstall(AjaxView, FormMixin):
|
||||
|
||||
if not confirmed:
|
||||
valid = False
|
||||
form.errors['confirm'] = [_('Confirm stock adjustment')]
|
||||
form.add_error('confirm', _('Confirm stock adjustment'))
|
||||
|
||||
data = {
|
||||
'form_valid': valid,
|
||||
@ -1116,7 +1116,7 @@ class StockAdjust(AjaxView, FormMixin):
|
||||
|
||||
if not confirmed:
|
||||
valid = False
|
||||
form.errors['confirm'] = [_('Confirm stock adjustment')]
|
||||
form.add_error('confirm', _('Confirm stock adjustment'))
|
||||
|
||||
data = {
|
||||
'form_valid': valid,
|
||||
@ -1416,7 +1416,7 @@ class StockItemSerialize(AjaxUpdateView):
|
||||
try:
|
||||
numbers = ExtractSerialNumbers(serials, quantity)
|
||||
except ValidationError as e:
|
||||
form.errors['serial_numbers'] = e.messages
|
||||
form.add_error('serial_numbers', e.messages)
|
||||
valid = False
|
||||
numbers = []
|
||||
|
||||
@ -1428,9 +1428,9 @@ class StockItemSerialize(AjaxUpdateView):
|
||||
|
||||
for k in messages.keys():
|
||||
if k in ['quantity', 'destination', 'serial_numbers']:
|
||||
form.errors[k] = messages[k]
|
||||
form.add_error(k, messages[k])
|
||||
else:
|
||||
form.non_field_errors = [messages[k]]
|
||||
form.add_error(None, messages[k])
|
||||
|
||||
valid = False
|
||||
|
||||
@ -1621,14 +1621,14 @@ class StockItemCreate(AjaxCreateView):
|
||||
part = None
|
||||
quantity = 1
|
||||
valid = False
|
||||
form.errors['quantity'] = [_('Invalid quantity')]
|
||||
form.add_error('quantity', _('Invalid quantity'))
|
||||
|
||||
if quantity < 0:
|
||||
form.errors['quantity'] = [_('Quantity cannot be less than zero')]
|
||||
form.add_error('quantity', _('Quantity cannot be less than zero'))
|
||||
valid = False
|
||||
|
||||
if part is None:
|
||||
form.errors['part'] = [_('Invalid part selection')]
|
||||
form.add_error('part', _('Invalid part selection'))
|
||||
else:
|
||||
# A trackable part must provide serial numbesr
|
||||
if part.trackable:
|
||||
@ -1641,15 +1641,14 @@ class StockItemCreate(AjaxCreateView):
|
||||
try:
|
||||
serials = ExtractSerialNumbers(sn, quantity)
|
||||
|
||||
existing = []
|
||||
|
||||
for serial in serials:
|
||||
if part.checkIfSerialNumberExists(serial):
|
||||
existing.append(serial)
|
||||
existing = part.find_conflicting_serial_numbers(serial)
|
||||
|
||||
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',
|
||||
_('Serial numbers already exist') + ': ' + exists
|
||||
)
|
||||
valid = False
|
||||
|
||||
else:
|
||||
@ -1681,7 +1680,7 @@ class StockItemCreate(AjaxCreateView):
|
||||
valid = True
|
||||
|
||||
except ValidationError as e:
|
||||
form.errors['serial_numbers'] = e.messages
|
||||
form.add_error('serial_numbers', e.messages)
|
||||
valid = False
|
||||
|
||||
else:
|
||||
|
Reference in New Issue
Block a user