mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-21 06:16:29 +00:00
Refactor how form errors are handled
- Use form.add_error (as the django gods intended)
This commit is contained in:
@ -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,27 @@ 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.
|
||||
|
@ -446,9 +446,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 = {
|
||||
@ -576,9 +576,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 = {
|
||||
@ -915,7 +916,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
|
||||
@ -1054,7 +1055,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)
|
||||
@ -1065,7 +1066,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!
|
||||
|
Reference in New Issue
Block a user