Allow type specific default values for restricted settings

See #1567
This commit is contained in:
Gina Häußge 2016-11-21 13:43:53 +01:00
parent bc82a18a03
commit 59e4394cdd

View file

@ -820,7 +820,9 @@ class SettingsPlugin(OctoPrintPlugin):
to retrieve the data in the correct format.
The default implementation will also replace any paths that have been restricted by your plugin through
:func:`~octoprint.plugin.SettingsPlugin.get_settings_restricted_paths` with ``None`` values where necessary.
:func:`~octoprint.plugin.SettingsPlugin.get_settings_restricted_paths` with either the provided
default value (if one was provided), an empty dictionary (as fallback for restricted dictionaries), an
empty list (as fallback for restricted lists) or ``None`` values where necessary.
Make sure to do your own restriction if you decide to fully overload this method.
:return: the current settings of the plugin, as a dictionary
@ -849,8 +851,25 @@ class SettingsPlugin(OctoPrintPlugin):
node = node[entry]
key = path[-1]
default_value_available = False
default_value = None
if isinstance(key, (list, tuple)):
# key, default_value tuple
key, default_value = key
default_value_available = True
if key in node:
node[key] = None
if default_value_available:
if callable(default_value):
default_value = default_value()
node[key] = default_value
else:
if isinstance(node[key], dict):
node[key] = dict()
elif isinstance(node[key], (list, tuple)):
node[key] = []
else:
node[key] = None
conditions = dict(user=lambda: current_user is not None and not current_user.is_anonymous(),
admin=lambda: current_user is not None and not current_user.is_anonymous() and current_user.is_admin(),