mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-15 19:45:46 +00:00
Merged master
This commit is contained in:
@ -110,7 +110,21 @@ class InvenTreeSetting(models.Model):
|
||||
'default': True,
|
||||
'validator': bool
|
||||
},
|
||||
|
||||
|
||||
'PART_TEMPLATE': {
|
||||
'name': _('Template'),
|
||||
'description': _('Parts are templates by default'),
|
||||
'default': False,
|
||||
'validator': bool,
|
||||
},
|
||||
|
||||
'PART_ASSEMBLY': {
|
||||
'name': _('Assembly'),
|
||||
'description': _('Parts can be assembled from other components by default'),
|
||||
'default': False,
|
||||
'validator': bool,
|
||||
},
|
||||
|
||||
'PART_COMPONENT': {
|
||||
'name': _('Component'),
|
||||
'description': _('Parts can be used as sub-components by default'),
|
||||
@ -139,7 +153,43 @@ class InvenTreeSetting(models.Model):
|
||||
'validator': bool,
|
||||
},
|
||||
|
||||
'STOCK_OWNERSHIP_CONTROL': {
|
||||
'PART_VIRTUAL': {
|
||||
'name': _('Virtual'),
|
||||
'description': _('Parts are virtual by default'),
|
||||
'default': False,
|
||||
'validator': bool,
|
||||
},
|
||||
|
||||
'STOCK_ENABLE_EXPIRY': {
|
||||
'name': _('Stock Expiry'),
|
||||
'description': _('Enable stock expiry functionality'),
|
||||
'default': False,
|
||||
'validator': bool,
|
||||
},
|
||||
|
||||
'STOCK_ALLOW_EXPIRED_SALE': {
|
||||
'name': _('Sell Expired Stock'),
|
||||
'description': _('Allow sale of expired stock'),
|
||||
'default': False,
|
||||
'validator': bool,
|
||||
},
|
||||
|
||||
'STOCK_STALE_DAYS': {
|
||||
'name': _('Stock Stale Time'),
|
||||
'description': _('Number of days stock items are considered stale before expiring'),
|
||||
'default': 0,
|
||||
'units': _('days'),
|
||||
'validator': [int],
|
||||
},
|
||||
|
||||
'STOCK_ALLOW_EXPIRED_BUILD': {
|
||||
'name': _('Build Expired Stock'),
|
||||
'description': _('Allow building with expired stock'),
|
||||
'default': False,
|
||||
'validator': bool,
|
||||
},
|
||||
|
||||
'STOCK_OWNERSHIP_CONTROL': {
|
||||
'name': _('Stock Ownership Control'),
|
||||
'description': _('Enable ownership control over stock locations and items'),
|
||||
'default': False,
|
||||
@ -345,6 +395,12 @@ class InvenTreeSetting(models.Model):
|
||||
if setting.is_bool():
|
||||
value = InvenTree.helpers.str2bool(value)
|
||||
|
||||
if setting.is_int():
|
||||
try:
|
||||
value = int(value)
|
||||
except (ValueError, TypeError):
|
||||
value = backup_value
|
||||
|
||||
else:
|
||||
value = backup_value
|
||||
|
||||
@ -433,18 +489,26 @@ class InvenTreeSetting(models.Model):
|
||||
|
||||
return
|
||||
|
||||
# Check if a 'type' has been specified for this value
|
||||
if type(validator) == type:
|
||||
# Boolean validator
|
||||
if validator == bool:
|
||||
# Value must "look like" a boolean value
|
||||
if InvenTree.helpers.is_bool(self.value):
|
||||
# Coerce into either "True" or "False"
|
||||
self.value = str(InvenTree.helpers.str2bool(self.value))
|
||||
else:
|
||||
raise ValidationError({
|
||||
'value': _('Value must be a boolean value')
|
||||
})
|
||||
|
||||
if validator == bool:
|
||||
# Value must "look like" a boolean value
|
||||
if InvenTree.helpers.is_bool(self.value):
|
||||
# Coerce into either "True" or "False"
|
||||
self.value = str(InvenTree.helpers.str2bool(self.value))
|
||||
else:
|
||||
raise ValidationError({
|
||||
'value': _('Value must be a boolean value')
|
||||
})
|
||||
# Integer validator
|
||||
if validator == int:
|
||||
try:
|
||||
# Coerce into an integer value
|
||||
self.value = str(int(self.value))
|
||||
except (ValueError, TypeError):
|
||||
raise ValidationError({
|
||||
'value': _('Value must be an integer value'),
|
||||
})
|
||||
|
||||
def validate_unique(self, exclude=None):
|
||||
""" Ensure that the key:value pair is unique.
|
||||
@ -486,6 +550,35 @@ class InvenTreeSetting(models.Model):
|
||||
|
||||
return InvenTree.helpers.str2bool(self.value)
|
||||
|
||||
def is_int(self):
|
||||
"""
|
||||
Check if the setting is required to be an integer value:
|
||||
"""
|
||||
|
||||
validator = InvenTreeSetting.get_setting_validator(self.key)
|
||||
|
||||
if validator == int:
|
||||
return True
|
||||
|
||||
if type(validator) in [list, tuple]:
|
||||
for v in validator:
|
||||
if v == int:
|
||||
return True
|
||||
|
||||
def as_int(self):
|
||||
"""
|
||||
Return the value of this setting converted to a boolean value.
|
||||
|
||||
If an error occurs, return the default value
|
||||
"""
|
||||
|
||||
try:
|
||||
value = int(self.value)
|
||||
except (ValueError, TypeError):
|
||||
value = self.default_value()
|
||||
|
||||
return value
|
||||
|
||||
|
||||
class PriceBreak(models.Model):
|
||||
"""
|
||||
|
@ -21,3 +21,11 @@ def currency_code_default():
|
||||
code = 'USD'
|
||||
|
||||
return code
|
||||
|
||||
|
||||
def stock_expiry_enabled():
|
||||
"""
|
||||
Returns True if the stock expiry feature is enabled
|
||||
"""
|
||||
|
||||
return InvenTreeSetting.get_setting('STOCK_ENABLE_EXPIRY')
|
||||
|
@ -31,7 +31,7 @@ class SettingsTest(TestCase):
|
||||
# There should be two settings objects in the database
|
||||
settings = InvenTreeSetting.objects.all()
|
||||
|
||||
self.assertEqual(settings.count(), 2)
|
||||
self.assertTrue(settings.count() >= 2)
|
||||
|
||||
instance_name = InvenTreeSetting.objects.get(pk=1)
|
||||
self.assertEqual(instance_name.key, 'INVENTREE_INSTANCE')
|
||||
|
Reference in New Issue
Block a user