From 8972a51bd62449f6a0683981db3865a9d28e7856 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 23 Feb 2021 14:12:16 +1100 Subject: [PATCH] Fixes for integer validator for inventree setting --- InvenTree/common/models.py | 62 ++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 20 deletions(-) diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index 52f38bea18..0514a081d0 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -125,6 +125,13 @@ class InvenTreeSetting(models.Model): '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': { 'name': _('Template'), 'description': _('Parts are templates by default'), @@ -521,11 +528,18 @@ class InvenTreeSetting(models.Model): 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: self.run_validator(validator) - if self.is_bool(): - self.value = InvenTree.helpers.str2bool(self.value) def run_validator(self, validator): """ @@ -535,39 +549,39 @@ class InvenTreeSetting(models.Model): if validator is None: return - # 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) - - return - - if callable(validator): - # We can accept function validators with a single argument - print("Running validator function") - validator(self.value) + value = self.value # Boolean validator - if validator == bool: + if self.is_bool(): # 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" - self.value = str(InvenTree.helpers.str2bool(self.value)) + value = InvenTree.helpers.str2bool(value) else: raise ValidationError({ 'value': _('Value must be a boolean value') }) - # Integer validator - if validator == int: + # Integer validator + if self.is_int(): + try: # Coerce into an integer value - self.value = str(int(self.value)) + value = int(value) except (ValueError, TypeError): raise ValidationError({ '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): """ Ensure that the key:value pair is unique. 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) - 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): """ @@ -623,6 +643,8 @@ class InvenTreeSetting(models.Model): if v == int: return True + return False + def as_int(self): """ Return the value of this setting converted to a boolean value.