2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-07-04 04:30:56 +00:00

Implement default currency selection

- Add 'choices' option to InvenTreeSetting class
- Add support for ChoiceField in InvenTreeSetting form
This commit is contained in:
Oliver Walters
2020-11-10 17:08:08 +11:00
parent 48c20c600a
commit 978fd7c683
3 changed files with 46 additions and 2 deletions

View File

@ -12,6 +12,8 @@ import decimal
from django.db import models
from django.conf import settings
import djmoney.settings
from django.utils.translation import ugettext as _
from django.core.validators import MinValueValidator, MaxValueValidator
from django.core.exceptions import ValidationError
@ -59,6 +61,13 @@ class InvenTreeSetting(models.Model):
'default': 'My company name',
},
'INVENTREE_DEFAULT_CURRENCY': {
'name': _('Default Currency'),
'description': _('Default currency'),
'default': 'USD',
'choices': djmoney.settings.CURRENCY_CHOICES,
},
'PART_IPN_REGEX': {
'name': _('IPN Regex'),
'description': _('Regular expression pattern for matching Part IPN')
@ -226,6 +235,29 @@ class InvenTreeSetting(models.Model):
else:
return ''
@classmethod
def get_setting_choices(cls, key):
"""
Return the validator choices available for a particular setting.
"""
key = str(key).strip().upper()
if key in cls.GLOBAL_SETTINGS:
setting = cls.GLOBAL_SETTINGS[key]
choices = setting.get('choices', None)
else:
choices = None
"""
TODO:
if type(choices) is function:
# Evaluate the function (we expect it will return a list of tuples...)
return choices()
"""
return choices
@classmethod
def get_setting_object(cls, key):
"""
@ -396,6 +428,13 @@ class InvenTreeSetting(models.Model):
except InvenTreeSetting.DoesNotExist:
pass
def choices(self):
"""
Return the available choices for this setting (or None if no choices are defined)
"""
return InvenTreeSetting.get_setting_choices(self.key)
def is_bool(self):
"""
Check if this setting is required to be a boolean value