From 59e4394cdd8050721cc2beacca54ee446ba168a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Mon, 21 Nov 2016 13:43:53 +0100 Subject: [PATCH 1/2] Allow type specific default values for restricted settings See #1567 --- src/octoprint/plugin/types.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/octoprint/plugin/types.py b/src/octoprint/plugin/types.py index bd938e59..9ac9a162 100644 --- a/src/octoprint/plugin/types.py +++ b/src/octoprint/plugin/types.py @@ -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(), From a88cd9fcfcddc9443881c0031598cd5363b51b1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Mon, 21 Nov 2016 13:44:26 +0100 Subject: [PATCH 2/2] Copy settings in on_settings_load so we don't mutate them --- src/octoprint/plugin/types.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/octoprint/plugin/types.py b/src/octoprint/plugin/types.py index 9ac9a162..84104ce4 100644 --- a/src/octoprint/plugin/types.py +++ b/src/octoprint/plugin/types.py @@ -828,8 +828,9 @@ class SettingsPlugin(OctoPrintPlugin): :return: the current settings of the plugin, as a dictionary """ from flask.ext.login import current_user + import copy - data = self._settings.get_all_data() + data = copy.deepcopy(self._settings.get_all_data(merged=True)) if self.config_version_key in data: del data[self.config_version_key]