From 5df4374607a002815625c85402ccc1977fd53f3f Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 9 Nov 2021 21:16:37 +1100 Subject: [PATCH] javascript for editing settings via API --- InvenTree/common/serializers.py | 6 -- InvenTree/templates/js/dynamic/settings.js | 65 ++++++++++++++++++++++ InvenTree/templates/js/translated/forms.js | 21 ++++--- 3 files changed, 78 insertions(+), 14 deletions(-) diff --git a/InvenTree/common/serializers.py b/InvenTree/common/serializers.py index 8121681bdf..e5c6c18b76 100644 --- a/InvenTree/common/serializers.py +++ b/InvenTree/common/serializers.py @@ -34,12 +34,6 @@ class SettingsSerializer(InvenTreeModelSerializer): return obj.choices() - value = serializers.SerializerMethodField() - - def get_value(self, obj): - - return obj.to_native_value() - class GlobalSettingsSerializer(SettingsSerializer): """ diff --git a/InvenTree/templates/js/dynamic/settings.js b/InvenTree/templates/js/dynamic/settings.js index b147d6993a..136812779b 100644 --- a/InvenTree/templates/js/dynamic/settings.js +++ b/InvenTree/templates/js/dynamic/settings.js @@ -19,3 +19,68 @@ const global_settings = { {% endfor %} }; +/* + * Edit a setting value + */ +function editSetting(pk, options={}) { + + // Is this a global setting or a user setting? + var global = options.global || false; + + var url = ''; + + if (global) { + url = `/api/settings/global/${pk}/`; + } else { + url = `/api/settings/user/${pk}/`; + } + + // First, read the settings object from the server + inventreeGet(url, {}, { + success: function(response) { + + // Construct the field + var fields = { + value: { + label: response.name, + help_text: response.description, + type: response.type, + choices: response.choices, + } + }; + + constructChangeForm(fields, { + url: url, + method: 'PATCH', + title: "edit setting", + processResults: function(data, fields, opts) { + + switch (data.type) { + case 'boolean': + // Convert to boolean value + data.value = data.value.toString().toLowerCase() == 'true'; + break; + case 'integer': + // Convert to integer value + data.value = parseInt(data.value.toString()); + break; + default: + break; + } + + return data; + }, + processBeforeUpload: function(data) { + // Convert value to string + data.value = data.value.toString(); + + return data; + } + }); + }, + error: function(xhr) { + showApiError(xhr, url); + } + }); + +} \ No newline at end of file diff --git a/InvenTree/templates/js/translated/forms.js b/InvenTree/templates/js/translated/forms.js index 18ba08d512..2f25fef259 100644 --- a/InvenTree/templates/js/translated/forms.js +++ b/InvenTree/templates/js/translated/forms.js @@ -198,14 +198,6 @@ function constructChangeForm(fields, options) { json: 'application/json', }, success: function(data) { - - // Push existing 'value' to each field - for (const field in data) { - - if (field in fields) { - fields[field].value = data[field]; - } - } // An optional function can be provided to process the returned results, // before they are rendered to the form @@ -218,6 +210,14 @@ function constructChangeForm(fields, options) { } } + // Push existing 'value' to each field + for (const field in data) { + + if (field in fields) { + fields[field].value = data[field]; + } + } + // Store the entire data object options.instance = data; @@ -724,6 +724,11 @@ function submitFormData(fields, options) { data = form_data; } + // Optionally pre-process the data before uploading to the server + if (options.processBeforeUpload) { + data = options.processBeforeUpload(data); + } + // Submit data upload_func( options.url,