mirror of
https://github.com/inventree/InvenTree.git
synced 2025-05-07 15:58:49 +00:00
Improved serial number extraction
- Check data types - Translation for error messages
This commit is contained in:
parent
6c5b09efd4
commit
15b38000d5
@ -131,11 +131,21 @@ def ExtractSerialNumbers(serials, expected_quantity):
|
|||||||
expected_quantity: The number of (unique) serial numbers we expect
|
expected_quantity: The number of (unique) serial numbers we expect
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
serials = serials.strip()
|
||||||
|
|
||||||
groups = re.split("[\s,]+", serials)
|
groups = re.split("[\s,]+", serials)
|
||||||
|
|
||||||
numbers = []
|
numbers = []
|
||||||
errors = []
|
errors = []
|
||||||
|
|
||||||
|
try:
|
||||||
|
expected_quantity = int(expected_quantity)
|
||||||
|
except ValueError:
|
||||||
|
raise ValidationError([_("Invalid quantity provided")])
|
||||||
|
|
||||||
|
if len(serials) == 0:
|
||||||
|
raise ValidationError([_("Empty serial number string")])
|
||||||
|
|
||||||
for group in groups:
|
for group in groups:
|
||||||
|
|
||||||
group = group.strip()
|
group = group.strip()
|
||||||
@ -155,34 +165,34 @@ def ExtractSerialNumbers(serials, expected_quantity):
|
|||||||
if a < b:
|
if a < b:
|
||||||
for n in range(a, b + 1):
|
for n in range(a, b + 1):
|
||||||
if n in numbers:
|
if n in numbers:
|
||||||
errors.append('Duplicate serial: {n}'.format(n=n))
|
errors.append(_('Duplicate serial: {n}'.format(n=n)))
|
||||||
else:
|
else:
|
||||||
numbers.append(n)
|
numbers.append(n)
|
||||||
else:
|
else:
|
||||||
errors.append("Invalid group: {g}".format(g=group))
|
errors.append(_("Invalid group: {g}".format(g=group)))
|
||||||
|
|
||||||
except ValueError:
|
except ValueError:
|
||||||
errors.append("Invalid group: {g}".format(g=group))
|
errors.append(_("Invalid group: {g}".format(g=group)))
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
errors.append("Invalid group: {g}".format(g=group))
|
errors.append(_("Invalid group: {g}".format(g=group)))
|
||||||
continue
|
continue
|
||||||
|
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
n = int(group)
|
n = int(group)
|
||||||
if n in numbers:
|
if n in numbers:
|
||||||
errors.append("Duplicate serial: {n}".format(n=n))
|
errors.append(_("Duplicate serial: {n}".format(n=n)))
|
||||||
else:
|
else:
|
||||||
numbers.append(n)
|
numbers.append(n)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
errors.append("Invalid group: {g}".format(g=group))
|
errors.append(_("Invalid group: {g}".format(g=group)))
|
||||||
|
|
||||||
if len(errors) > 0:
|
if len(errors) > 0:
|
||||||
raise ValidationError(errors)
|
raise ValidationError(errors)
|
||||||
|
|
||||||
if len(numbers) == 0:
|
if len(numbers) == 0:
|
||||||
raise ValidationError(["No serial numbers found"])
|
raise ValidationError([_("No serial numbers found")])
|
||||||
|
|
||||||
# The number of extracted serial numbers must match the expected quantity
|
# The number of extracted serial numbers must match the expected quantity
|
||||||
if not expected_quantity == len(numbers):
|
if not expected_quantity == len(numbers):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user