fix validation

This commit is contained in:
Matthias 2021-12-02 01:03:30 +01:00
parent fc6f1b4acc
commit b8cdcab10d
No known key found for this signature in database
GPG Key ID: F50EF5741D33E076
2 changed files with 21 additions and 16 deletions

View File

@ -72,24 +72,27 @@ class ReferenceIndexingMixin(models.Model):
reference = getattr(self, 'reference', '') reference = getattr(self, 'reference', '')
# Default value if we cannot convert to an integer self.reference_int = extract_int(reference)
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
reference_int = models.BigIntegerField(default=0) 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): class InvenTreeAttachment(models.Model):
""" Provides an abstracted class for managing file attachments. """ Provides an abstracted class for managing file attachments.

View File

@ -28,6 +28,8 @@ from rest_framework.fields import empty
from rest_framework.exceptions import ValidationError from rest_framework.exceptions import ValidationError
from rest_framework.serializers import DecimalField from rest_framework.serializers import DecimalField
from .models import extract_int
class InvenTreeMoneySerializer(MoneyField): class InvenTreeMoneySerializer(MoneyField):
""" """
@ -246,9 +248,9 @@ class ReferenceIndexingSerializerMixin():
for the BigIntegerField for the BigIntegerField
""" """
def validate_reference(self, value): 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') 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') raise serializers.ValidationError('reference is to to big')
return value return value