From b8cdcab10d81a6eb4ba2f8a52d07e0e2d2aaea50 Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 2 Dec 2021 01:03:30 +0100 Subject: [PATCH] fix validation --- InvenTree/InvenTree/models.py | 31 ++++++++++++++++-------------- InvenTree/InvenTree/serializers.py | 6 ++++-- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/InvenTree/InvenTree/models.py b/InvenTree/InvenTree/models.py index 12fcf87c05..b42d54cbe9 100644 --- a/InvenTree/InvenTree/models.py +++ b/InvenTree/InvenTree/models.py @@ -72,24 +72,27 @@ class ReferenceIndexingMixin(models.Model): reference = getattr(self, 'reference', '') - # Default value if we cannot convert to an integer - ref_int = 0 - - # Look at the start of the string - can it be "integerized"? - result = re.match(r"^(\d+)", reference) - - if result and len(result.groups()) == 1: - ref = result.groups()[0] - try: - ref_int = int(ref) - except: - ref_int = 0 - - self.reference_int = ref_int + self.reference_int = extract_int(reference) reference_int = models.BigIntegerField(default=0) +def extract_int(reference): + # Default value if we cannot convert to an integer + ref_int = 0 + + # Look at the start of the string - can it be "integerized"? + result = re.match(r"^(\d+)", reference) + + if result and len(result.groups()) == 1: + ref = result.groups()[0] + try: + ref_int = int(ref) + except: + ref_int = 0 + return ref_int + + class InvenTreeAttachment(models.Model): """ Provides an abstracted class for managing file attachments. diff --git a/InvenTree/InvenTree/serializers.py b/InvenTree/InvenTree/serializers.py index 3a237e4280..9da0609e8f 100644 --- a/InvenTree/InvenTree/serializers.py +++ b/InvenTree/InvenTree/serializers.py @@ -28,6 +28,8 @@ from rest_framework.fields import empty from rest_framework.exceptions import ValidationError from rest_framework.serializers import DecimalField +from .models import extract_int + class InvenTreeMoneySerializer(MoneyField): """ @@ -246,9 +248,9 @@ class ReferenceIndexingSerializerMixin(): for the BigIntegerField """ def validate_reference(self, value): - if int(value) < -models.BigIntegerField.MAX_BIGINT: + if extract_int(value) < -models.BigIntegerField.MAX_BIGINT: raise serializers.ValidationError('reference is to to small') - if int(value) > models.BigIntegerField.MAX_BIGINT: + if extract_int(value) > models.BigIntegerField.MAX_BIGINT: raise serializers.ValidationError('reference is to to big') return value