Refactor login plugin enablement code
[cascardo/ipsilon.git] / ipsilon / admin / login.py
index 1f5e9fa..d11c1f1 100755 (executable)
@@ -27,16 +27,17 @@ from ipsilon.login.common import FACILITY
 
 class LoginPluginsOrder(Page):
 
-    def __init__(self, site, baseurl):
+    def __init__(self, site, parent):
         super(LoginPluginsOrder, self).__init__(site)
-        self.url = '%s/order' % baseurl
+        self.url = '%s/order' % parent.url
+        self.menu = [parent]
 
     @admin_protect
     def GET(self, *args, **kwargs):
         return self._template('admin/login_order.html',
                               title='login plugins order',
                               name='admin_login_order_form',
-                              action=self.url,
+                              menu=self.menu, action=self.url,
                               options=self._site[FACILITY]['enabled'])
 
     @admin_protect
@@ -87,7 +88,7 @@ class LoginPluginsOrder(Page):
                               message_type=message_type,
                               title='login plugins order',
                               name='admin_login_order_form',
-                              action=self.url,
+                              menu=self.menu, action=self.url,
                               options=self._site[FACILITY]['enabled'])
 
     def root(self, *args, **kwargs):
@@ -100,19 +101,56 @@ class LoginPluginsOrder(Page):
 class LoginPlugins(Page):
     def __init__(self, site, parent):
         super(LoginPlugins, self).__init__(site)
-        parent.login = self
+        self._master = parent
+        self.title = 'Login Plugins'
         self.url = '%s/login' % parent.url
+        self.facility = FACILITY
+        parent.add_subtree('login', self)
 
         for plugin in self._site[FACILITY]['available']:
             cherrypy.log.error('Admin login plugin: %s' % plugin)
             obj = self._site[FACILITY]['available'][plugin]
-            self.__dict__[plugin] = AdminPluginPage(obj, self._site,
-                                                    self.url, FACILITY)
+            self.__dict__[plugin] = AdminPluginPage(obj, self._site, self)
 
-        self.order = LoginPluginsOrder(self._site, self.url)
+        self.order = LoginPluginsOrder(self._site, self)
 
-    def root(self, *args, **kwargs):
+    def root_with_msg(self, message=None, message_type=None):
         login_plugins = self._site[FACILITY]
-        return self._template('admin/login.html', title='Login Plugins',
+        ordered = []
+        for p in login_plugins['enabled']:
+            ordered.append(p.name)
+        return self._template('admin/login.html', title=self.title,
+                              message=message,
+                              message_type=message_type,
                               available=login_plugins['available'],
-                              enabled=login_plugins['enabled'])
+                              enabled=ordered,
+                              menu=self._master.menu)
+
+    def root(self, *args, **kwargs):
+        return self.root_with_msg()
+
+    def enable(self, plugin):
+        msg = None
+        plugins = self._site[FACILITY]
+        if plugin not in plugins['available']:
+            msg = "Unknown plugin %s" % plugin
+            return self.root_with_msg(msg, "error")
+        obj = plugins['available'][plugin]
+        if obj not in plugins['enabled']:
+            obj.enable(self._site)
+            msg = "Plugin %s enabled" % obj.name
+        return self.root_with_msg(msg, "success")
+    enable.exposed = True
+
+    def disable(self, plugin):
+        msg = None
+        plugins = self._site[FACILITY]
+        if plugin not in plugins['available']:
+            msg = "Unknown plugin %s" % plugin
+            return self.root_with_msg(msg, "error")
+        obj = plugins['available'][plugin]
+        if obj in plugins['enabled']:
+            obj.disable(self._site)
+            msg = "Plugin %s disabled" % obj.name
+        return self.root_with_msg(msg, "success")
+    disable.exposed = True