mirror of
https://github.com/inventree/InvenTree.git
synced 2025-05-03 05:48:47 +00:00
Fix some existing problems with the extract_serial_numbers helper function
- Serial numbers don't really have to be "numbers" - Allow any text value once other higher-level checks have been performed
This commit is contained in:
parent
421db61f21
commit
fdc2cae6ba
@ -407,14 +407,16 @@ def DownloadFile(data, filename, content_type='application/text', inline=False):
|
|||||||
|
|
||||||
|
|
||||||
def extract_serial_numbers(serials, expected_quantity, next_number: int):
|
def extract_serial_numbers(serials, expected_quantity, next_number: int):
|
||||||
""" Attempt to extract serial numbers from an input string.
|
"""
|
||||||
- Serial numbers must be integer values
|
Attempt to extract serial numbers from an input string:
|
||||||
- Serial numbers must be positive
|
|
||||||
- Serial numbers can be split by whitespace / newline / commma chars
|
Requirements:
|
||||||
- Serial numbers can be supplied as an inclusive range using hyphen char e.g. 10-20
|
- Serial numbers can be either strings, or integers
|
||||||
- Serial numbers can be defined as ~ for getting the next available serial number
|
- Serial numbers can be split by whitespace / newline / commma chars
|
||||||
- Serial numbers can be supplied as <start>+ for getting all expecteded numbers starting from <start>
|
- Serial numbers can be supplied as an inclusive range using hyphen char e.g. 10-20
|
||||||
- Serial numbers can be supplied as <start>+<length> for getting <length> numbers starting from <start>
|
- Serial numbers can be defined as ~ for getting the next available serial number
|
||||||
|
- Serial numbers can be supplied as <start>+ for getting all expecteded numbers starting from <start>
|
||||||
|
- Serial numbers can be supplied as <start>+<length> for getting <length> numbers starting from <start>
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
serials: input string with patterns
|
serials: input string with patterns
|
||||||
@ -428,17 +430,18 @@ def extract_serial_numbers(serials, expected_quantity, next_number: int):
|
|||||||
if '~' in serials:
|
if '~' in serials:
|
||||||
serials = serials.replace('~', str(next_number))
|
serials = serials.replace('~', str(next_number))
|
||||||
|
|
||||||
|
# Split input string by whitespace or comma (,) characters
|
||||||
groups = re.split("[\s,]+", serials)
|
groups = re.split("[\s,]+", serials)
|
||||||
|
|
||||||
numbers = []
|
numbers = []
|
||||||
errors = []
|
errors = []
|
||||||
|
|
||||||
# helpers
|
# Helper function to check for duplicated numbers
|
||||||
def number_add(n):
|
def add_sn(sn):
|
||||||
if n in numbers:
|
if sn in numbers:
|
||||||
errors.append(_('Duplicate serial: {n}').format(n=n))
|
errors.append(_('Duplicate serial: {sn}').format(n=n))
|
||||||
else:
|
else:
|
||||||
numbers.append(n)
|
numbers.append(sn)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
expected_quantity = int(expected_quantity)
|
expected_quantity = int(expected_quantity)
|
||||||
@ -466,7 +469,7 @@ def extract_serial_numbers(serials, expected_quantity, next_number: int):
|
|||||||
|
|
||||||
if a < b:
|
if a < b:
|
||||||
for n in range(a, b + 1):
|
for n in range(a, b + 1):
|
||||||
number_add(n)
|
add_sn(n)
|
||||||
else:
|
else:
|
||||||
errors.append(_("Invalid group: {g}").format(g=group))
|
errors.append(_("Invalid group: {g}").format(g=group))
|
||||||
|
|
||||||
@ -495,21 +498,16 @@ def extract_serial_numbers(serials, expected_quantity, next_number: int):
|
|||||||
end = start + expected_quantity
|
end = start + expected_quantity
|
||||||
|
|
||||||
for n in range(start, end):
|
for n in range(start, end):
|
||||||
number_add(n)
|
add_sn(n)
|
||||||
# no case
|
# no case
|
||||||
else:
|
else:
|
||||||
errors.append(_("Invalid group: {g}").format(g=group))
|
errors.append(_("Invalid group: {g}").format(g=group))
|
||||||
|
|
||||||
# Group should be a number
|
# At this point, we assume that the "group" is just a single serial value
|
||||||
|
# Note: At this point it is treated only as a string
|
||||||
elif group:
|
elif group:
|
||||||
# try conversion
|
|
||||||
try:
|
|
||||||
number = int(group)
|
|
||||||
except:
|
|
||||||
# seem like it is not a number
|
|
||||||
raise ValidationError(_(f"Invalid group {group}"))
|
|
||||||
|
|
||||||
number_add(number)
|
add_sn(group)
|
||||||
|
|
||||||
# No valid input group detected
|
# No valid input group detected
|
||||||
else:
|
else:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user