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

View File

@ -6,7 +6,7 @@ Django views for interacting with common models
from __future__ import unicode_literals
from django.utils.translation import ugettext as _
from django.forms import CheckboxInput
from django.forms import CheckboxInput, Select
from InvenTree.views import AjaxCreateView, AjaxUpdateView, AjaxDeleteView
from InvenTree.helpers import str2bool
@ -75,7 +75,11 @@ class SettingEdit(AjaxUpdateView):
setting = self.get_object()
if setting.is_bool():
choices = setting.choices()
if choices is not None:
form.fields['value'].widget = Select(choices=choices)
elif setting.is_bool():
form.fields['value'].widget = CheckboxInput()
self.object.value = str2bool(setting.value)

View File

@ -17,6 +17,7 @@
<tbody>
{% include "InvenTree/settings/setting.html" with key="INVENTREE_INSTANCE" %}
{% include "InvenTree/settings/setting.html" with key="INVENTREE_COMPANY_NAME" %}
{% include "InvenTree/settings/setting.html" with key="INVENTREE_DEFAULT_CURRENCY" %}
</tbody>
</table>