From b19719516bbd22c08c8c3e66f2b82c6a322764d3 Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 28 Feb 2022 15:57:34 +1100 Subject: [PATCH] Adds a user-configurable setting to configure how dates are displayed (on the front end only!) --- InvenTree/common/models.py | 47 ++++++++++++++++++- .../templates/InvenTree/settings/setting.html | 4 ++ .../InvenTree/settings/user_display.html | 1 + InvenTree/templates/js/dynamic/settings.js | 7 ++- 4 files changed, 56 insertions(+), 3 deletions(-) diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index 9ef5a4d0c3..927abd94cd 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -443,12 +443,12 @@ class BaseInvenTreeSetting(models.Model): except self.DoesNotExist: pass - def choices(self): + def choices(self, **kwargs): """ Return the available choices for this setting (or None if no choices are defined) """ - return self.__class__.get_setting_choices(self.key) + return self.__class__.get_setting_choices(self.key, **kwargs) def valid_options(self): """ @@ -462,6 +462,34 @@ class BaseInvenTreeSetting(models.Model): return [opt[0] for opt in choices] + def is_choice(self, **kwargs): + """ + Check if this setting is a "choice" field + """ + + return self.__class__.get_setting_choices(self.key, **kwargs) != None + + def as_choice(self, **kwargs): + """ + Render this setting as the "display" value of a choice field, + e.g. if the choices are: + [('A4', 'A4 paper'), ('A3', 'A3 paper')], + and the value is 'A4', + then display 'A4 paper' + """ + + choices = self.get_setting_choices(self.key, **kwargs) + + if not choices: + return self.value + + for value, display in choices: + if value == self.value: + return display + + return self.value + + def is_bool(self, **kwargs): """ Check if this setting is required to be a boolean value @@ -1212,6 +1240,21 @@ class InvenTreeUserSetting(BaseInvenTreeSetting): 'default': False, 'validator': bool, }, + + 'DATE_DISPLAY_FORMAT': { + 'name': _('Date Format'), + 'description': _('Preferred format for displaying dates'), + 'default': 'YYYY-MM-DD', + 'choices': [ + ('YYYY-MM-DD', '2022-02-22'), + ('YYYY/MM/DD', '2022/22/22'), + ('DD-MM-YYYY', '22-02-2022'), + ('DD/MM/YYYY', '22/02/2022'), + ('MM-DD-YYYY', '02-22-2022'), + ('MM/DD/YYYY', '02/22/2022'), + ('MMM DD YYYY', 'Feb 22 2022'), + ] + } } class Meta: diff --git a/InvenTree/templates/InvenTree/settings/setting.html b/InvenTree/templates/InvenTree/settings/setting.html index 743e37bf8e..9ef6008292 100644 --- a/InvenTree/templates/InvenTree/settings/setting.html +++ b/InvenTree/templates/InvenTree/settings/setting.html @@ -28,7 +28,11 @@
{% if setting.value %} + {% if setting.is_choice %} + {{ setting.as_choice }} + {% else %} {{ setting.value }} + {% endif %} {% else %} {% trans "No value set" %} {% endif %} diff --git a/InvenTree/templates/InvenTree/settings/user_display.html b/InvenTree/templates/InvenTree/settings/user_display.html index 9f5c22f991..6432b858a3 100644 --- a/InvenTree/templates/InvenTree/settings/user_display.html +++ b/InvenTree/templates/InvenTree/settings/user_display.html @@ -15,6 +15,7 @@ {% include "InvenTree/settings/setting.html" with key="STICKY_HEADER" icon="fa-bars" user_setting=True %} + {% include "InvenTree/settings/setting.html" with key="DATE_DISPLAY_FORMAT" icon="fa-calendar-alt" user_setting=True %} {% include "InvenTree/settings/setting.html" with key="FORMS_CLOSE_USING_ESCAPE" icon="fa-window-close" user_setting=True %} {% include "InvenTree/settings/setting.html" with key="PART_SHOW_QUANTITY_IN_FORMS" icon="fa-hashtag" user_setting=True %} diff --git a/InvenTree/templates/js/dynamic/settings.js b/InvenTree/templates/js/dynamic/settings.js index 133edfba20..4e7d36f72b 100644 --- a/InvenTree/templates/js/dynamic/settings.js +++ b/InvenTree/templates/js/dynamic/settings.js @@ -40,12 +40,15 @@ function editSetting(pk, options={}) { url = `/api/settings/user/${pk}/`; } + var reload_required = false; + // First, read the settings object from the server inventreeGet(url, {}, { success: function(response) { if (response.choices && response.choices.length > 0) { response.type = 'choice'; + reload_required = true; } // Construct the field @@ -89,7 +92,9 @@ function editSetting(pk, options={}) { var setting = response.key; - if (response.type == 'boolean') { + if (reload_required) { + location.reload(); + } else if (response.type == 'boolean') { var enabled = response.value.toString().toLowerCase() == 'true'; $(`#setting-value-${setting}`).prop('checked', enabled); } else {