2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-05-01 04:56:45 +00:00

Edit plugin settings via the "settings" display

This commit is contained in:
Oliver 2022-01-04 21:03:01 +11:00
parent dc9e25ebad
commit 928b90a833
4 changed files with 24 additions and 16 deletions

View File

@ -371,23 +371,14 @@ class BaseInvenTreeSetting(models.Model):
validator = self.__class__.get_setting_validator(self.key, **kwargs) validator = self.__class__.get_setting_validator(self.key, **kwargs)
if self.is_bool(): if validator is not None:
self.value = InvenTree.helpers.str2bool(self.value) self.run_validator(validator)
if self.is_int():
try:
self.value = int(self.value)
except (ValueError):
raise ValidationError(_('Must be an integer value'))
options = self.valid_options() options = self.valid_options()
if options and self.value not in options: if options and self.value not in options:
raise ValidationError(_("Chosen value is not a valid option")) raise ValidationError(_("Chosen value is not a valid option"))
if validator is not None:
self.run_validator(validator)
def run_validator(self, validator): def run_validator(self, validator):
""" """
Run a validator against the 'value' field for this InvenTreeSetting object. Run a validator against the 'value' field for this InvenTreeSetting object.
@ -399,7 +390,7 @@ class BaseInvenTreeSetting(models.Model):
value = self.value value = self.value
# Boolean validator # Boolean validator
if self.is_bool(): if validator is bool:
# Value must "look like" a boolean value # Value must "look like" a boolean value
if InvenTree.helpers.is_bool(value): if InvenTree.helpers.is_bool(value):
# Coerce into either "True" or "False" # Coerce into either "True" or "False"
@ -410,7 +401,7 @@ class BaseInvenTreeSetting(models.Model):
}) })
# Integer validator # Integer validator
if self.is_int(): if validator is int:
try: try:
# Coerce into an integer value # Coerce into an integer value

View File

@ -113,6 +113,12 @@ class PluginSetting(common.models.BaseInvenTreeSetting):
('plugin', 'key'), ('plugin', 'key'),
] ]
def clean(self, **kwargs):
kwargs['plugin'] = self.plugin
super().clean(**kwargs)
""" """
We override the following class methods, We override the following class methods,
so that we can pass the plugin instance so that we can pass the plugin instance

View File

@ -38,7 +38,7 @@
</td> </td>
<td> <td>
<div class='btn-group float-right'> <div class='btn-group float-right'>
<button class='btn btn-outline-secondary btn-small btn-edit-setting' pk='{{ setting.pk }}' setting='{{ setting.key.upper }}' title='{% trans "Edit setting" %}' {% if user_setting %}user='{{request.user.id}}'{% endif %}> <button class='btn btn-outline-secondary btn-small btn-edit-setting' pk='{{ setting.pk }}' setting='{{ setting.key.upper }}' title='{% trans "Edit setting" %}' {% if plugin %}plugin='{{ plugin.pk }}'{% endif %}{% if user_setting %}user='{{request.user.id}}'{% endif %}>
<span class='fas fa-edit icon-green'></span> <span class='fas fa-edit icon-green'></span>
</button> </button>
</div> </div>

View File

@ -62,16 +62,27 @@
$('table').find('.btn-edit-setting').click(function() { $('table').find('.btn-edit-setting').click(function() {
var setting = $(this).attr('setting'); var setting = $(this).attr('setting');
var pk = $(this).attr('pk'); var pk = $(this).attr('pk');
var plugin = $(this).attr('plugin');
var is_global = true; var is_global = true;
if ($(this).attr('user')){ if ($(this).attr('user')){
is_global = false; is_global = false;
} }
var title = '';
if (plugin != null) {
title = '{% trans "Edit Plugin Setting" %}';
} else if (is_global) {
title = '{% trans "Edit Global Setting" %}';
} else {
title = '{% trans "Edit User Setting" %}';
}
editSetting(pk, { editSetting(pk, {
plugin: plugin,
global: is_global, global: is_global,
title: is_global ? '{% trans "Edit Global Setting" %}' : '{% trans "Edit User Setting" %}', title: title,
}); });
}); });