mirror of
https://github.com/inventree/InvenTree.git
synced 2025-05-01 04:56:45 +00:00
Currency format fix (#7398)
* Fix for currency rendering - Handle case where max digits less than min digits * Add validators for settings
This commit is contained in:
parent
0cb762d6b2
commit
a5fa5f8ac3
@ -40,6 +40,7 @@ from rest_framework.exceptions import PermissionDenied
|
|||||||
|
|
||||||
import build.validators
|
import build.validators
|
||||||
import common.currency
|
import common.currency
|
||||||
|
import common.validators
|
||||||
import InvenTree.fields
|
import InvenTree.fields
|
||||||
import InvenTree.helpers
|
import InvenTree.helpers
|
||||||
import InvenTree.models
|
import InvenTree.models
|
||||||
@ -1146,19 +1147,6 @@ def update_instance_name(setting):
|
|||||||
site_obj.save()
|
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):
|
def reload_plugin_registry(setting):
|
||||||
"""When a core plugin setting is changed, reload the plugin registry."""
|
"""When a core plugin setting is changed, reload the plugin registry."""
|
||||||
from plugin import registry
|
from plugin import registry
|
||||||
@ -1550,7 +1538,12 @@ class InvenTreeSetting(BaseInvenTreeSetting):
|
|||||||
'Minimum number of decimal places to display when rendering pricing data'
|
'Minimum number of decimal places to display when rendering pricing data'
|
||||||
),
|
),
|
||||||
'default': 0,
|
'default': 0,
|
||||||
'validator': [int, MinValueValidator(0), MaxValueValidator(4)],
|
'validator': [
|
||||||
|
int,
|
||||||
|
MinValueValidator(0),
|
||||||
|
MaxValueValidator(4),
|
||||||
|
common.validators.validate_decimal_places_min,
|
||||||
|
],
|
||||||
},
|
},
|
||||||
'PRICING_DECIMAL_PLACES': {
|
'PRICING_DECIMAL_PLACES': {
|
||||||
'name': _('Maximum Pricing Decimal Places'),
|
'name': _('Maximum Pricing Decimal Places'),
|
||||||
@ -1558,7 +1551,12 @@ class InvenTreeSetting(BaseInvenTreeSetting):
|
|||||||
'Maximum number of decimal places to display when rendering pricing data'
|
'Maximum number of decimal places to display when rendering pricing data'
|
||||||
),
|
),
|
||||||
'default': 6,
|
'default': 6,
|
||||||
'validator': [int, MinValueValidator(2), MaxValueValidator(6)],
|
'validator': [
|
||||||
|
int,
|
||||||
|
MinValueValidator(2),
|
||||||
|
MaxValueValidator(6),
|
||||||
|
common.validators.validate_decimal_places_max,
|
||||||
|
],
|
||||||
},
|
},
|
||||||
'PRICING_USE_SUPPLIER_PRICING': {
|
'PRICING_USE_SUPPLIER_PRICING': {
|
||||||
'name': _('Use Supplier Pricing'),
|
'name': _('Use Supplier Pricing'),
|
||||||
@ -1944,7 +1942,7 @@ class InvenTreeSetting(BaseInvenTreeSetting):
|
|||||||
'Restrict signup to certain domains (comma-separated, starting with @)'
|
'Restrict signup to certain domains (comma-separated, starting with @)'
|
||||||
),
|
),
|
||||||
'default': '',
|
'default': '',
|
||||||
'before_save': validate_email_domains,
|
'before_save': common.validators.validate_email_domains,
|
||||||
},
|
},
|
||||||
'SIGNUP_GROUP': {
|
'SIGNUP_GROUP': {
|
||||||
'name': _('Group on signup'),
|
'name': _('Group on signup'),
|
||||||
|
45
src/backend/InvenTree/common/validators.py
Normal file
45
src/backend/InvenTree/common/validators.py
Normal file
@ -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}'))
|
@ -77,8 +77,8 @@ export function formatCurrency(
|
|||||||
let formatter = new Intl.NumberFormat(locale, {
|
let formatter = new Intl.NumberFormat(locale, {
|
||||||
style: 'currency',
|
style: 'currency',
|
||||||
currency: currency,
|
currency: currency,
|
||||||
maximumFractionDigits: maxDigits,
|
maximumFractionDigits: Math.max(minDigits, maxDigits),
|
||||||
minimumFractionDigits: minDigits
|
minimumFractionDigits: Math.min(minDigits, maxDigits)
|
||||||
});
|
});
|
||||||
|
|
||||||
return formatter.format(value);
|
return formatter.format(value);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user