Adds a user-configurable setting to configure how dates are displayed (on the front end only!)

This commit is contained in:
Oliver 2022-02-28 15:57:34 +11:00
parent 010ce48ce0
commit b19719516b
4 changed files with 56 additions and 3 deletions

View File

@ -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:

View File

@ -28,7 +28,11 @@
<div id='setting-{{ setting.pk }}'>
<span id='setting-value-{{ setting.key.upper }}' fieldname='{{ setting.key.upper }}'>
{% if setting.value %}
{% if setting.is_choice %}
<strong>{{ setting.as_choice }}</strong>
{% else %}
<strong>{{ setting.value }}</strong>
{% endif %}
{% else %}
<em style='color: #855;'>{% trans "No value set" %}</em>
{% endif %}

View File

@ -15,6 +15,7 @@
<table class='table table-striped table-condensed'>
<tbody>
{% 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 %}
</tbody>

View File

@ -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 {