2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-21 06:16:29 +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:
Oliver Walters
2020-10-28 23:33:33 +11:00
parent d06b4d7c9f
commit 091a9d9803
8 changed files with 112 additions and 88 deletions

View File

@ -360,7 +360,7 @@ class Part(MPTTModel):
# And recursively check too
item.sub_part.checkAddToBOM(parent)
def checkIfSerialNumberExists(self, sn):
def checkIfSerialNumberExists(self, sn, exclude_self=False):
"""
Check if a serial number exists for this Part.
@ -369,10 +369,28 @@ class Part(MPTTModel):
"""
parts = Part.objects.filter(tree_id=self.tree_id)
stock = StockModels.StockItem.objects.filter(part__in=parts, serial=sn)
if exclude_self:
stock = stock.exclude(pk=self.pk)
return stock.exists()
def find_conflicting_serial_numbers(self, serials):
"""
For a provided list of serials, return a list of those which are conflicting.
"""
conflicts = []
for serial in serials:
if self.checkIfSerialNumberExists(serial, exclude_self=True):
conflicts.append(serial)
return conflicts
def getLatestSerialNumber(self):
"""
Return the "latest" serial number for this Part.

View File

@ -445,9 +445,9 @@ class PartDuplicate(AjaxCreateView):
confirmed = str2bool(request.POST.get('confirm_creation', False))
if not confirmed:
form.errors['confirm_creation'] = ['Possible matches exist - confirm creation of new part']
form.pre_form_warning = 'Possible matches exist - confirm creation of new part'
msg = _('Possible matches exist - confirm creation of new part')
form.add_error('confirm_creation', msg)
form.pre_form_warning = msg
valid = False
data = {
@ -575,9 +575,10 @@ class PartCreate(AjaxCreateView):
confirmed = str2bool(request.POST.get('confirm_creation', False))
if not confirmed:
form.errors['confirm_creation'] = ['Possible matches exist - confirm creation of new part']
msg = _('Possible matches exist - confirm creation of new part')
form.add_error('confirm_creation', msg)
form.pre_form_warning = 'Possible matches exist - confirm creation of new part'
form.pre_form_warning = msg
valid = False
data = {
@ -862,7 +863,7 @@ class BomValidate(AjaxUpdateView):
if confirmed:
part.validate_bom(request.user)
else:
form.errors['validate'] = ['Confirm that the BOM is valid']
form.add_error('validate', _('Confirm that the BOM is valid'))
data = {
'form_valid': confirmed
@ -1001,7 +1002,7 @@ class BomUpload(InvenTreeRoleMixin, FormView):
bom_file_valid = False
if bom_file is None:
self.form.errors['bom_file'] = [_('No BOM file provided')]
self.form.add_error('bom_file', _('No BOM file provided'))
else:
# Create a BomUploadManager object - will perform initial data validation
# (and raise a ValidationError if there is something wrong with the file)
@ -1012,7 +1013,7 @@ class BomUpload(InvenTreeRoleMixin, FormView):
errors = e.error_dict
for k, v in errors.items():
self.form.errors[k] = v
self.form.add_error(k, v)
if bom_file_valid:
# BOM file is valid? Proceed to the next step!