Use indirection to report error strings
[cascardo/ipsilon.git] / ipsilon / admin / common.py
index 9c82142..af315ff 100755 (executable)
@@ -23,6 +23,20 @@ from ipsilon.util.page import admin_protect
 from ipsilon.util import config as pconfig
 
 
+ADMIN_STATUS_OK = "success"
+ADMIN_STATUS_ERROR = "danger"
+ADMIN_STATUS_WARN = "warning"
+
+
+class AdminError(Exception):
+    def __init__(self, message):
+        super(AdminError, self).__init__(message)
+        self.message = message
+
+    def __str__(self):
+        return str(self.message)
+
+
 class AdminPage(Page):
 
     def __init__(self, *args, **kwargs):
@@ -61,6 +75,11 @@ class AdminPluginConfig(AdminPage):
     @admin_protect
     def POST(self, *args, **kwargs):
 
+        if self._po.is_readonly:
+            return self.root_with_msg(
+                message="Configuration is marked Read-Only",
+                message_type=ADMIN_STATUS_WARN)
+
         message = "Nothing was modified."
         message_type = "info"
         new_db_values = dict()
@@ -97,10 +116,10 @@ class AdminPluginConfig(AdminPage):
             try:
                 self._po.save_plugin_config(new_db_values)
                 message = "New configuration saved."
-                message_type = "success"
+                message_type = ADMIN_STATUS_OK
             except Exception:  # pylint: disable=broad-except
                 message = "Failed to save data!"
-                message_type = "error"
+                message_type = ADMIN_STATUS_ERROR
 
             # Then refresh the actual objects
             self._po.refresh_plugin_config()
@@ -131,6 +150,12 @@ class AdminPluginsOrder(AdminPage):
 
     @admin_protect
     def POST(self, *args, **kwargs):
+
+        if self._site[self.facility].is_readonly:
+            return self.parent.root_with_msg(
+                message="Configuration is marked Read-Only",
+                message_type=ADMIN_STATUS_WARN)
+
         message = "Nothing was modified."
         message_type = "info"
         cur_enabled = self._get_enabled_list()
@@ -158,15 +183,15 @@ class AdminPluginsOrder(AdminPage):
                     self._site[self.facility].refresh_enabled()
 
                     message = "New configuration saved."
-                    message_type = "success"
+                    message_type = ADMIN_STATUS_OK
 
                 except ValueError, e:
                     message = str(e)
-                    message_type = "error"
+                    message_type = ADMIN_STATUS_ERROR
 
                 except Exception, e:  # pylint: disable=broad-except
                     message = "Failed to save data!"
-                    message_type = "error"
+                    message_type = ADMIN_STATUS_ERROR
 
         return self.parent.root_with_msg(message=message,
                                          message_type=message_type)
@@ -219,34 +244,46 @@ class AdminPlugins(AdminPage):
     def root(self, *args, **kwargs):
         return self.root_with_msg()
 
-    @admin_protect
-    def enable(self, plugin):
-        msg = None
+    def _get_plugin_obj(self, plugin):
         plugins = self._site[self.facility]
+        if plugins.is_readonly:
+            msg = "Configuration is marked Read-Only"
+            raise AdminError(msg)
         if plugin not in plugins.available:
             msg = "Unknown plugin %s" % plugin
-            return self.root_with_msg(msg, "error")
+            raise AdminError(msg)
         obj = plugins.available[plugin]
+        if obj.is_readonly:
+            msg = "Plugin Configuration is marked Read-Only"
+            raise AdminError(msg)
+        return obj
+
+    @admin_protect
+    def enable(self, plugin):
+        msg = None
+        try:
+            obj = self._get_plugin_obj(plugin)
+        except AdminError, e:
+            return self.root_with_msg(str(e), ADMIN_STATUS_WARN)
         if not obj.is_enabled:
             obj.enable()
             obj.save_enabled_state()
             msg = "Plugin %s enabled" % obj.name
-        return self.root_with_msg(msg, "success")
+        return self.root_with_msg(msg, ADMIN_STATUS_OK)
     enable.public_function = True
 
     @admin_protect
     def disable(self, plugin):
         msg = None
-        plugins = self._site[self.facility]
-        if plugin not in plugins.available:
-            msg = "Unknown plugin %s" % plugin
-            return self.root_with_msg(msg, "error")
-        obj = plugins.available[plugin]
+        try:
+            obj = self._get_plugin_obj(plugin)
+        except AdminError, e:
+            return self.root_with_msg(str(e), ADMIN_STATUS_WARN)
         if obj.is_enabled:
             obj.disable()
             obj.save_enabled_state()
             msg = "Plugin %s disabled" % obj.name
-        return self.root_with_msg(msg, "success")
+        return self.root_with_msg(msg, ADMIN_STATUS_OK)
     disable.public_function = True