[#2885] Support partial sequences in serial nos ('1, 2, 3+')

This commit is contained in:
Paul R 2022-04-28 10:34:01 +01:00
parent 8077469a3f
commit 1dba9f66fb
2 changed files with 13 additions and 8 deletions

View File

@ -439,6 +439,12 @@ def extract_serial_numbers(serials, expected_quantity, next_number: int):
# Helper function to check for duplicated numbers
def add_sn(sn):
# Attempt integer conversion first, so numerical strings are never stored
try:
sn = int(sn)
except ValueError:
pass
if sn in numbers:
errors.append(_('Duplicate serial: {sn}').format(sn=sn))
else:
@ -496,7 +502,7 @@ def extract_serial_numbers(serials, expected_quantity, next_number: int):
# case 1
else:
end = start + expected_quantity
end = start + (expected_quantity - len(numbers))
for n in range(start, end):
add_sn(n)
@ -506,13 +512,7 @@ def extract_serial_numbers(serials, expected_quantity, next_number: int):
# At this point, we assume that the "group" is just a single serial value
elif group:
try:
# First attempt to add as an integer value
add_sn(int(group))
except (ValueError):
# As a backup, add as a string value
add_sn(group)
add_sn(group)
# No valid input group detected
else:

View File

@ -252,6 +252,11 @@ class TestSerialNumberExtraction(TestCase):
sn = e("1, 2, 3, 4, 5", 5, 1)
self.assertEqual(len(sn), 5)
# Test partially specifying serials
sn = e("1, 2, 4+", 5, 1)
self.assertEqual(len(sn), 5)
self.assertEqual(sn, [1, 2, 4, 5, 6])
# Test multiple placeholders
sn = e("1 2 ~ ~ ~", 5, 3)
self.assertEqual(len(sn), 5)