From 46a808c06492b64ab5451042ab2b6600571f7399 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikl=C3=B3s=20M=C3=A1rton?= Date: Thu, 1 Jun 2023 12:01:48 +0200 Subject: [PATCH] Add user settings to remember the last/favourite label template (#4938) * Add user settings to remember the last/favourite label template Fixes #4932 * Remove settings_value from translated templates Thanks Oliver for the hint! --- InvenTree/common/models.py | 30 ++++++++++++++++++++- InvenTree/label/api.py | 2 ++ InvenTree/templates/js/translated/label.js | 16 ++++++++--- InvenTree/templates/js/translated/modals.js | 15 ++++++++--- 4 files changed, 56 insertions(+), 7 deletions(-) diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index 45dcf50113..d010ecf34c 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -2100,7 +2100,35 @@ class InvenTreeUserSetting(BaseInvenTreeSetting): MinValueValidator(0), ], 'default': 100, - } + }, + + 'DEFAULT_PART_LABEL_TEMPLATE': { + 'name': _('Default part label template'), + 'description': _('The part label template to be automatically selected'), + 'validator': [ + int, + ], + 'default': '', + }, + + 'DEFAULT_ITEM_LABEL_TEMPLATE': { + 'name': _('Default stock item template'), + 'description': _('The stock item label template to be automatically selected'), + 'validator': [ + int, + ], + 'default': '', + }, + + 'DEFAULT_LOCATION_LABEL_TEMPLATE': { + 'name': _('Default stock location label template'), + 'description': _('The stock location label template to be automatically selected'), + 'validator': [ + int, + ], + 'default': '', + }, + } typ = 'user' diff --git a/InvenTree/label/api.py b/InvenTree/label/api.py index 578c37a896..87e69a2e72 100644 --- a/InvenTree/label/api.py +++ b/InvenTree/label/api.py @@ -148,6 +148,8 @@ class LabelPrintMixin(LabelFilterMixin): def get(self, request, *args, **kwargs): """Perform a GET request against this endpoint to print labels""" + common.models.InvenTreeUserSetting.set_setting('DEFAULT_' + self.ITEM_KEY.upper() + '_LABEL_TEMPLATE', + self.get_object().pk, None, user=request.user) return self.print(request, self.get_items()) def get_plugin(self, request): diff --git a/InvenTree/templates/js/translated/label.js b/InvenTree/templates/js/translated/label.js index f27a81903b..cf3d4c90aa 100644 --- a/InvenTree/templates/js/translated/label.js +++ b/InvenTree/templates/js/translated/label.js @@ -1,5 +1,6 @@ {% load i18n %} - +{% load static %} +{% load inventree_extras %} /* globals attachSelect, closeModal, @@ -29,7 +30,6 @@ * (via AJAX) from the server. */ function selectLabel(labels, items, options={}) { - // Array of available plugins for label printing var plugins = []; @@ -78,7 +78,6 @@ function selectLabel(labels, items, options={}) { } var modal = options.modal || '#modal-form'; - var label_list = makeOptionsList( labels, function(item) { @@ -92,6 +91,16 @@ function selectLabel(labels, items, options={}) { }, function(item) { return item.pk; + }, + null, + function(item) { + if (options.key == 'part') + return item.key == user_settings.DEFAULT_PART_LABEL_TEMPLATE; + else if (options.key == 'location') + return item.key == user_settings.DEFAULT_LOCATION_LABEL_TEMPLATE; + else if (options.key == 'stock') + return item.key == user_settings.DEFAULT_STOCK_LABEL_TEMPLATE; + return ''; } ); @@ -214,6 +223,7 @@ function printLabels(options) { }, plural_name: options.plural_name, singular_name: options.singular_name, + key: options.key, }); } }); diff --git a/InvenTree/templates/js/translated/modals.js b/InvenTree/templates/js/translated/modals.js index c02a0ac252..0cae0fa71e 100644 --- a/InvenTree/templates/js/translated/modals.js +++ b/InvenTree/templates/js/translated/modals.js @@ -174,7 +174,7 @@ function enableSubmitButton(options, enable=true) { } -function makeOption(text, value, title) { +function makeOption(text, value, title, selected) { /* Format an option for a select element */ @@ -184,6 +184,9 @@ function makeOption(text, value, title) { html += ` title='${title}'`; } + if (selected) { + html += 'selected="selected"'; + } html += `>${text}`; return html; @@ -200,8 +203,9 @@ function makeOption(text, value, title) { * - textFunc: Function which takes an element and generates the text to be displayed * - valueFunc: optional function which takes an element and generates the value * - titleFunc: optional function which takes an element and generates a title + * - selectedFunc: optional function which takes an element and generate true if the given item should be added as selected */ -function makeOptionsList(elements, textFunc, valueFunc, titleFunc) { +function makeOptionsList(elements, textFunc, valueFunc, titleFunc, selectedFunc) { var options = []; @@ -210,6 +214,7 @@ function makeOptionsList(elements, textFunc, valueFunc, titleFunc) { var text = textFunc(element); var value = null; var title = null; + var selected = null; if (valueFunc) { value = valueFunc(element); @@ -221,7 +226,11 @@ function makeOptionsList(elements, textFunc, valueFunc, titleFunc) { title = titleFunc(element); } - options.push(makeOption(text, value, title)); + if (selectedFunc) { + selected = selectedFunc(element); + } + + options.push(makeOption(text, value, title, selected)); }); return options;