From b08efa4de7217dc9a2fcd81cc4879178cfaf872e Mon Sep 17 00:00:00 2001 From: Paul R Date: Thu, 28 Apr 2022 10:38:04 +0100 Subject: [PATCH] [#2885] Don't interpolate serial groups if they are not numeric --- InvenTree/InvenTree/helpers.py | 5 +++-- InvenTree/InvenTree/tests.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/InvenTree/InvenTree/helpers.py b/InvenTree/InvenTree/helpers.py index 45030ec0d7..68f42bab87 100644 --- a/InvenTree/InvenTree/helpers.py +++ b/InvenTree/InvenTree/helpers.py @@ -466,7 +466,7 @@ def extract_serial_numbers(serials, expected_quantity, next_number: int): if '-' in group: items = group.split('-') - if len(items) == 2: + if len(items) == 2 and all([i.isnumeric() for i in items]): a = items[0].strip() b = items[1].strip() @@ -484,7 +484,8 @@ def extract_serial_numbers(serials, expected_quantity, next_number: int): errors.append(_("Invalid group: {g}").format(g=group)) continue else: - errors.append(_("Invalid group: {g}").format(g=group)) + # More than 2 hyphens or non-numeric group so add without interpolating + add_sn(group) # plus signals either # 1: 'start+': expected number of serials, starting at start diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index b9e9dd5228..51e5527535 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -257,6 +257,16 @@ class TestSerialNumberExtraction(TestCase): self.assertEqual(len(sn), 5) self.assertEqual(sn, [1, 2, 4, 5, 6]) + # Test groups are not interpolated with more than one hyphen in a word + sn = e("1, 2, TG-4SR-92, 4+", 5, 1) + self.assertEqual(len(sn), 5) + self.assertEqual(sn, [1, 2, "TG-4SR-92", 4, 5]) + + # Test groups are not interpolated with alpha characters + sn = e("1, A-2, 3+", 5, 1) + self.assertEqual(len(sn), 5) + self.assertEqual(sn, [1, "A-2", 3, 4, 5]) + # Test multiple placeholders sn = e("1 2 ~ ~ ~", 5, 3) self.assertEqual(len(sn), 5) @@ -317,6 +327,10 @@ class TestSerialNumberExtraction(TestCase): with self.assertRaises(ValidationError): e("10, a, 7-70j", 4, 1) + # Test groups are not interpolated with word characters + with self.assertRaises(ValidationError): + e("1, 2, 3, E-5", 5, 1) + def test_combinations(self): e = helpers.extract_serial_numbers