Fixes for integer validator for inventree setting

This commit is contained in:
Oliver Walters 2021-02-23 14:12:16 +11:00
parent 571a03043e
commit 8972a51bd6

View File

@ -125,6 +125,13 @@ class InvenTreeSetting(models.Model):
'validator': bool 'validator': bool
}, },
'PART_RECENT_COUNT': {
'name': _('Recent Part Count'),
'description': _('Number of recent parts to display on index page'),
'default': 10,
'validator': [int, MinValueValidator(1),]
},
'PART_TEMPLATE': { 'PART_TEMPLATE': {
'name': _('Template'), 'name': _('Template'),
'description': _('Parts are templates by default'), 'description': _('Parts are templates by default'),
@ -521,11 +528,18 @@ class InvenTreeSetting(models.Model):
validator = InvenTreeSetting.get_setting_validator(self.key) validator = InvenTreeSetting.get_setting_validator(self.key)
if self.is_bool():
self.value = InvenTree.helpers.str2bool(self.value)
if self.is_int():
try:
self.value = int(self.value)
except (ValueError):
raise ValidationError(_('Must be an integer value'))
if validator is not None: if validator is not None:
self.run_validator(validator) self.run_validator(validator)
if self.is_bool():
self.value = InvenTree.helpers.str2bool(self.value)
def run_validator(self, validator): def run_validator(self, validator):
""" """
@ -535,39 +549,39 @@ class InvenTreeSetting(models.Model):
if validator is None: if validator is None:
return return
# If a list of validators is supplied, iterate through each one value = self.value
if type(validator) in [list, tuple]:
for v in validator:
self.run_validator(v)
return
if callable(validator):
# We can accept function validators with a single argument
print("Running validator function")
validator(self.value)
# Boolean validator # Boolean validator
if validator == bool: if self.is_bool():
# Value must "look like" a boolean value # Value must "look like" a boolean value
if InvenTree.helpers.is_bool(self.value): if InvenTree.helpers.is_bool(value):
# Coerce into either "True" or "False" # Coerce into either "True" or "False"
self.value = str(InvenTree.helpers.str2bool(self.value)) value = InvenTree.helpers.str2bool(value)
else: else:
raise ValidationError({ raise ValidationError({
'value': _('Value must be a boolean value') 'value': _('Value must be a boolean value')
}) })
# Integer validator # Integer validator
if validator == int: if self.is_int():
try: try:
# Coerce into an integer value # Coerce into an integer value
self.value = str(int(self.value)) value = int(value)
except (ValueError, TypeError): except (ValueError, TypeError):
raise ValidationError({ raise ValidationError({
'value': _('Value must be an integer value'), 'value': _('Value must be an integer value'),
}) })
# If a list of validators is supplied, iterate through each one
if type(validator) in [list, tuple]:
for v in validator:
self.run_validator(v)
if callable(validator):
# We can accept function validators with a single argument
validator(self.value)
def validate_unique(self, exclude=None): def validate_unique(self, exclude=None):
""" Ensure that the key:value pair is unique. """ Ensure that the key:value pair is unique.
In addition to the base validators, this ensures that the 'key' In addition to the base validators, this ensures that the 'key'
@ -597,7 +611,13 @@ class InvenTreeSetting(models.Model):
validator = InvenTreeSetting.get_setting_validator(self.key) validator = InvenTreeSetting.get_setting_validator(self.key)
return validator == bool if validator == bool:
return True
if type(validator) in [list, tuple]:
for v in validator:
if v == bool:
return True
def as_bool(self): def as_bool(self):
""" """
@ -623,6 +643,8 @@ class InvenTreeSetting(models.Model):
if v == int: if v == int:
return True return True
return False
def as_int(self): def as_int(self):
""" """
Return the value of this setting converted to a boolean value. Return the value of this setting converted to a boolean value.