mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
javascript for editing settings via API
This commit is contained in:
parent
324335a620
commit
5df4374607
@ -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):
|
||||
"""
|
||||
|
@ -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);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user