diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index 8254d161d6..6844fa2ab7 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -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 diff --git a/InvenTree/common/views.py b/InvenTree/common/views.py index cfcee8bfa9..ff72a44e3d 100644 --- a/InvenTree/common/views.py +++ b/InvenTree/common/views.py @@ -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) diff --git a/InvenTree/templates/InvenTree/settings/global.html b/InvenTree/templates/InvenTree/settings/global.html index 7dcdd54bea..775d30b915 100644 --- a/InvenTree/templates/InvenTree/settings/global.html +++ b/InvenTree/templates/InvenTree/settings/global.html @@ -17,6 +17,7 @@ {% 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" %}