Do not overwrite default plugin options
authorSimo Sorce <simo@redhat.com>
Fri, 10 Oct 2014 19:52:01 +0000 (15:52 -0400)
committerPatrick Uiterwijk <puiterwijk@redhat.com>
Fri, 24 Oct 2014 16:02:20 +0000 (18:02 +0200)
Change the admin plugin to not overwrite the plugin default options,
and only use the sanctioned pluginObject interfaces to read/write
config values.

Signed-off-by: Simo Sorce <simo@redhat.com>
Reviewed-by: Patrick Uiterwijk <puiterwijk@redhat.com>
ipsilon/admin/common.py
ipsilon/util/plugin.py
templates/admin/plugin_config.html

index 5928763..7f723ac 100755 (executable)
@@ -36,31 +36,27 @@ class AdminPage(Page):
 
 class AdminPluginPage(AdminPage):
 
 
 class AdminPluginPage(AdminPage):
 
-    def __init__(self, obj, site, parent):
+    def __init__(self, po, site, parent):
         super(AdminPluginPage, self).__init__(site, form=True)
         super(AdminPluginPage, self).__init__(site, form=True)
-        self._obj = obj
-        self.title = '%s plugin' % obj.name
-        self.url = '%s/%s' % (parent.url, obj.name)
+        self._po = po
+        self.title = '%s plugin' % po.name
+        self.url = '%s/%s' % (parent.url, po.name)
         self.facility = parent.facility
         self.menu = [parent]
         self.back = parent.url
 
         # Get the defaults
         self.facility = parent.facility
         self.menu = [parent]
         self.back = parent.url
 
         # Get the defaults
-        self.plugin_config = obj.get_config_desc()
-        if not self.plugin_config:
-            self.plugin_config = dict()
-
-        # Now overlay the actual config
-        for option in self.plugin_config:
-            self.plugin_config[option][2] = obj.get_config_value(option)
+        options = po.get_config_desc()
+        if options is None:
+            options = dict()
 
         self.options_order = []
 
         self.options_order = []
-        if hasattr(obj, 'conf_opt_order'):
-            self.options_order = obj.conf_opt_order
+        if hasattr(po, 'conf_opt_order'):
+            self.options_order = po.conf_opt_order
 
         # append any undefined options
         add = []
 
         # append any undefined options
         add = []
-        for k in self.plugin_config.keys():
+        for k in options.keys():
             if k not in self.options_order:
                 add.append(k)
         if len(add):
             if k not in self.options_order:
                 add.append(k)
         if len(add):
@@ -72,10 +68,10 @@ class AdminPluginPage(AdminPage):
     def GET(self, *args, **kwargs):
         return self._template('admin/plugin_config.html', title=self.title,
                               name='admin_%s_%s_form' % (self.facility,
     def GET(self, *args, **kwargs):
         return self._template('admin/plugin_config.html', title=self.title,
                               name='admin_%s_%s_form' % (self.facility,
-                                                         self._obj.name),
+                                                         self._po.name),
                               menu=self.menu, action=self.url, back=self.back,
                               options_order=self.options_order,
                               menu=self.menu, action=self.url, back=self.back,
                               options_order=self.options_order,
-                              options=self.plugin_config)
+                              plugin=self._po)
 
     @admin_protect
     def POST(self, *args, **kwargs):
 
     @admin_protect
     def POST(self, *args, **kwargs):
@@ -84,17 +80,22 @@ class AdminPluginPage(AdminPage):
         message_type = "info"
         new_values = dict()
 
         message_type = "info"
         new_values = dict()
 
+        # Get the defaults
+        options = self._po.get_config_desc()
+        if options is None:
+            options = dict()
+
         for key, value in kwargs.iteritems():
         for key, value in kwargs.iteritems():
-            if key in self.plugin_config:
-                if value != self.plugin_config[key][2]:
+            if key in options:
+                if value != self._po.get_config_value(key):
                     cherrypy.log.error("Storing [%s]: %s = %s" %
                     cherrypy.log.error("Storing [%s]: %s = %s" %
-                                       (self._obj.name, key, value))
+                                       (self._po.name, key, value))
                     new_values[key] = value
 
         if len(new_values) != 0:
             # First we try to save in the database
             try:
                     new_values[key] = value
 
         if len(new_values) != 0:
             # First we try to save in the database
             try:
-                self._obj.save_plugin_config(self.facility, new_values)
+                self._po.save_plugin_config(self.facility, new_values)
                 message = "New configuration saved."
                 message_type = "success"
             except Exception:  # pylint: disable=broad-except
                 message = "New configuration saved."
                 message_type = "success"
             except Exception:  # pylint: disable=broad-except
@@ -102,17 +103,15 @@ class AdminPluginPage(AdminPage):
                 message_type = "error"
 
             # And only if it succeeds we change the live object
                 message_type = "error"
 
             # And only if it succeeds we change the live object
-            for name, value in new_values.items():
-                self._obj.set_config_value(name, value)
-                self.plugin_config[name][2] = value
+            self._po.refresh_plugin_config(self.facility)
 
         return self._template('admin/plugin_config.html', title=self.title,
                               message=message,
                               message_type=message_type,
                               name='admin_%s_%s_form' % (self.facility,
 
         return self._template('admin/plugin_config.html', title=self.title,
                               message=message,
                               message_type=message_type,
                               name='admin_%s_%s_form' % (self.facility,
-                                                         self._obj.name),
+                                                         self._po.name),
                               menu=self.menu, action=self.url,
                               menu=self.menu, action=self.url,
-                              options=self.plugin_config)
+                              plugin=self._po)
 
 
 class Admin(AdminPage):
 
 
 class Admin(AdminPage):
index 903f548..48edf0e 100755 (executable)
@@ -115,7 +115,7 @@ class PluginObject(Log):
         self._options = None
         self._data = AdminStore()
 
         self._options = None
         self._data = AdminStore()
 
-    def get_config_desc(self):
+    def get_config_desc(self, name=None):
         """ The configuration description is a dictionary that provides
             A description of the supported configuration options, as well
             as the default configuration option values.
         """ The configuration description is a dictionary that provides
             A description of the supported configuration options, as well
             as the default configuration option values.
@@ -124,7 +124,13 @@ class PluginObject(Log):
              - option type
              - default value
         """
              - option type
              - default value
         """
-        return self._options
+        if name is None:
+            return self._options
+
+        opt = self._options.get(name, None)
+        if opt is None:
+            return ''
+        return opt[0]
 
     def set_config(self, config):
         self._config = config
 
     def set_config(self, config):
         self._config = config
@@ -152,6 +158,10 @@ class PluginObject(Log):
     def get_plugin_config(self, facility):
         return self._data.load_options(facility, self.name)
 
     def get_plugin_config(self, facility):
         return self._data.load_options(facility, self.name)
 
+    def refresh_plugin_config(self, facility):
+        config = self.get_plugin_config(facility)
+        self.set_config(config)
+
     def save_plugin_config(self, facility, config=None):
         if config is None:
             config = self._config
     def save_plugin_config(self, facility, config=None):
         if config is None:
             config = self._config
index d95bf75..70d56f0 100644 (file)
@@ -12,9 +12,9 @@
         {% for o in options_order %}
             <div class="form-group">
             <label for="{{ o }}">{{ o }}:</label>
         {% for o in options_order %}
             <div class="form-group">
             <label for="{{ o }}">{{ o }}:</label>
-                <input type="text" class="form-control" name="{{ o }}" value="{{ options[o][2] }}">
+                <input type="text" class="form-control" name="{{ o }}" value="{{ plugin.get_config_value(o) }}">
             </div>
             </div>
-            <span class="help-block">{{ options[o][0] }}</span>
+            <span class="help-block">{{ plugin.get_config_desc(o) }}</span>
         {% endfor %}
 
         <button id="submit" class="btn btn-primary" name="submit" type="submit" value="Submit">
         {% endfor %}
 
         <button id="submit" class="btn btn-primary" name="submit" type="submit" value="Submit">