From b0fb5657e8242779e2257682476641388a48644b Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 2 Dec 2021 12:11:09 +0100 Subject: [PATCH] also validate serial number --- InvenTree/stock/models.py | 13 ++----------- InvenTree/stock/serializers.py | 7 ++++++- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index 8457ca39ab..785961f328 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -39,6 +39,7 @@ import label.models from InvenTree.status_codes import StockStatus, StockHistoryCode from InvenTree.models import InvenTreeTree, InvenTreeAttachment from InvenTree.fields import InvenTreeModelMoneyField, InvenTreeURLField +from InvenTree.serializers import extract_int from users.models import Owner @@ -236,17 +237,7 @@ class StockItem(MPTTModel): serial_int = 0 if serial is not None: - - serial = str(serial) - - # Look at the start of the string - can it be "integerized"? - result = re.match(r'^(\d+)', serial) - - if result and len(result.groups()) == 1: - try: - serial_int = int(result.groups()[0]) - except: - serial_int = 0 + serial_int = extract_int(str(serial)) self.serial_int = serial_int diff --git a/InvenTree/stock/serializers.py b/InvenTree/stock/serializers.py index c74d674275..24a90f59d0 100644 --- a/InvenTree/stock/serializers.py +++ b/InvenTree/stock/serializers.py @@ -32,7 +32,7 @@ from company.serializers import SupplierPartSerializer import InvenTree.helpers import InvenTree.serializers -from InvenTree.serializers import InvenTreeDecimalField +from InvenTree.serializers import InvenTreeDecimalField, extract_int from part.serializers import PartBriefSerializer @@ -72,6 +72,11 @@ class StockItemSerializerBrief(InvenTree.serializers.InvenTreeModelSerializer): 'supplier_part', 'uid', ] + + def validate_serial(self, value): + if extract_int(value) > 2147483647: + raise serializers.ValidationError('serial is to to big') + return value class StockItemSerializer(InvenTree.serializers.InvenTreeModelSerializer):