From 1dba9f66fb4fd4787c1619f3c7431e087e8c3cfc Mon Sep 17 00:00:00 2001 From: Paul R Date: Thu, 28 Apr 2022 10:34:01 +0100 Subject: [PATCH] [#2885] Support partial sequences in serial nos ('1, 2, 3+') --- InvenTree/InvenTree/helpers.py | 16 ++++++++-------- InvenTree/InvenTree/tests.py | 5 +++++ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/InvenTree/InvenTree/helpers.py b/InvenTree/InvenTree/helpers.py index b94383c874..45030ec0d7 100644 --- a/InvenTree/InvenTree/helpers.py +++ b/InvenTree/InvenTree/helpers.py @@ -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: diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index 11ca990b8e..b9e9dd5228 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -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)