diff --git a/src/backend/InvenTree/common/models.py b/src/backend/InvenTree/common/models.py index 417ef238e7fa..39661e0ce45c 100644 --- a/src/backend/InvenTree/common/models.py +++ b/src/backend/InvenTree/common/models.py @@ -40,6 +40,7 @@ import build.validators import common.currency +import common.validators import InvenTree.fields import InvenTree.helpers import InvenTree.models @@ -1146,19 +1147,6 @@ def update_instance_name(setting): site_obj.save() -def validate_email_domains(setting): - """Validate the email domains setting.""" - if not setting.value: - return - - domains = setting.value.split(',') - for domain in domains: - if not domain: - raise ValidationError(_('An empty domain is not allowed.')) - if not re.match(r'^@[a-zA-Z0-9\.\-_]+$', domain): - raise ValidationError(_(f'Invalid domain name: {domain}')) - - def reload_plugin_registry(setting): """When a core plugin setting is changed, reload the plugin registry.""" from plugin import registry @@ -1550,7 +1538,12 @@ def save(self, *args, **kwargs): 'Minimum number of decimal places to display when rendering pricing data' ), 'default': 0, - 'validator': [int, MinValueValidator(0), MaxValueValidator(4)], + 'validator': [ + int, + MinValueValidator(0), + MaxValueValidator(4), + common.validators.validate_decimal_places_min, + ], }, 'PRICING_DECIMAL_PLACES': { 'name': _('Maximum Pricing Decimal Places'), @@ -1558,7 +1551,12 @@ def save(self, *args, **kwargs): 'Maximum number of decimal places to display when rendering pricing data' ), 'default': 6, - 'validator': [int, MinValueValidator(2), MaxValueValidator(6)], + 'validator': [ + int, + MinValueValidator(2), + MaxValueValidator(6), + common.validators.validate_decimal_places_max, + ], }, 'PRICING_USE_SUPPLIER_PRICING': { 'name': _('Use Supplier Pricing'), @@ -1944,7 +1942,7 @@ def save(self, *args, **kwargs): 'Restrict signup to certain domains (comma-separated, starting with @)' ), 'default': '', - 'before_save': validate_email_domains, + 'before_save': common.validators.validate_email_domains, }, 'SIGNUP_GROUP': { 'name': _('Group on signup'), diff --git a/src/backend/InvenTree/common/validators.py b/src/backend/InvenTree/common/validators.py new file mode 100644 index 000000000000..6a202b25beb5 --- /dev/null +++ b/src/backend/InvenTree/common/validators.py @@ -0,0 +1,45 @@ +"""Validation helpers for common models.""" + +from django.core.exceptions import ValidationError +from django.utils.translation import gettext_lazy as _ + + +def validate_decimal_places_min(value): + """Validator for PRICING_DECIMAL_PLACES_MIN setting.""" + from common.models import InvenTreeSetting + + try: + value = int(value) + places_max = int(InvenTreeSetting.get_setting('PRICING_DECIMAL_PLACES')) + except Exception: + return + + if value > places_max: + raise ValidationError(_('Minimum places cannot be greater than maximum places')) + + +def validate_decimal_places_max(value): + """Validator for PRICING_DECIMAL_PLACES_MAX setting.""" + from common.models import InvenTreeSetting + + try: + value = int(value) + places_min = int(InvenTreeSetting.get_setting('PRICING_DECIMAL_PLACES_MIN')) + except Exception: + return + + if value < places_min: + raise ValidationError(_('Maximum places cannot be less than minimum places')) + + +def validate_email_domains(setting): + """Validate the email domains setting.""" + if not setting.value: + return + + domains = setting.value.split(',') + for domain in domains: + if not domain: + raise ValidationError(_('An empty domain is not allowed.')) + if not re.match(r'^@[a-zA-Z0-9\.\-_]+$', domain): + raise ValidationError(_(f'Invalid domain name: {domain}')) diff --git a/src/frontend/src/defaults/formatters.tsx b/src/frontend/src/defaults/formatters.tsx index 4afcf734d5ad..892aa9e66352 100644 --- a/src/frontend/src/defaults/formatters.tsx +++ b/src/frontend/src/defaults/formatters.tsx @@ -77,8 +77,8 @@ export function formatCurrency( let formatter = new Intl.NumberFormat(locale, { style: 'currency', currency: currency, - maximumFractionDigits: maxDigits, - minimumFractionDigits: minDigits + maximumFractionDigits: Math.max(minDigits, maxDigits), + minimumFractionDigits: Math.min(minDigits, maxDigits) }); return formatter.format(value);