3 # Copyright (C) 2014 Ipsilon Contributors see COPYING for license
6 from ipsilon.util.page import Page
7 from ipsilon.util.page import admin_protect
8 from ipsilon.util.plugin import PluginObject
9 from ipsilon.admin.common import AdminPluginPage
10 from ipsilon.info.common import FACILITY
13 class InfoPluginsOrder(Page):
15 def __init__(self, site, parent):
16 super(InfoPluginsOrder, self).__init__(site, form=True)
17 self.url = '%s/order' % parent.url
21 def GET(self, *args, **kwargs):
22 opts = [p.name for p in self._site[FACILITY]['enabled']]
23 return self._template('admin/info_order.html',
24 title='info plugins order',
25 name='admin_info_order_form',
26 menu=self.menu, action=self.url,
30 def POST(self, *args, **kwargs):
31 message = "Nothing was modified."
33 plugins_by_name = {p.name: p for p in self._site[FACILITY]['enabled']}
36 order = kwargs['order'].split(',')
43 if val not in plugins_by_name:
44 error = "Invalid plugin name: %s" % val
45 raise ValueError(error)
47 new_plugins.append(plugins_by_name[val])
48 if len(new_names) < len(plugins_by_name):
49 for val in plugins_by_name:
50 if val not in new_names:
52 new_plugins.append(plugins_by_name[val])
57 globalconf['order'] = ','.join(new_names)
58 po.set_config(globalconf)
59 po.save_plugin_config(FACILITY)
61 # When all is saved update also live config. The
62 # live config is a list of the actual plugin
64 self._site[FACILITY]['enabled'] = new_plugins
66 message = "New configuration saved."
67 message_type = "success"
71 message_type = "error"
73 except Exception, e: # pylint: disable=broad-except
74 message = "Failed to save data!"
75 message_type = "error"
77 opts = [p.name for p in self._site[FACILITY]['enabled']]
78 return self._template('admin/info_order.html',
80 message_type=message_type,
81 title='info plugins order',
82 name='admin_info_order_form',
83 menu=self.menu, action=self.url,
87 class InfoPlugins(Page):
88 def __init__(self, site, parent):
89 super(InfoPlugins, self).__init__(site)
91 self.title = 'Info Plugins'
92 self.url = '%s/info' % parent.url
93 self.facility = FACILITY
94 parent.add_subtree('info', self)
96 for plugin in self._site[FACILITY]['available']:
97 cherrypy.log.error('Admin info plugin: %s' % plugin)
98 obj = self._site[FACILITY]['available'][plugin]
99 self.__dict__[plugin] = AdminPluginPage(obj, self._site, self)
101 self.order = InfoPluginsOrder(self._site, self)
103 def root_with_msg(self, message=None, message_type=None):
104 info_plugins = self._site[FACILITY]
106 for p in info_plugins['enabled']:
107 ordered.append(p.name)
108 return self._template('admin/info.html', title=self.title,
110 message_type=message_type,
111 available=info_plugins['available'],
113 menu=self._master.menu)
115 def root(self, *args, **kwargs):
116 return self.root_with_msg()
118 def enable(self, plugin):
120 plugins = self._site[FACILITY]
121 if plugin not in plugins['available']:
122 msg = "Unknown plugin %s" % plugin
123 return self.root_with_msg(msg, "error")
124 obj = plugins['available'][plugin]
125 if obj not in plugins['enabled']:
126 obj.enable(self._site)
127 msg = "Plugin %s enabled" % obj.name
128 return self.root_with_msg(msg, "success")
129 enable.exposed = True
131 def disable(self, plugin):
133 plugins = self._site[FACILITY]
134 if plugin not in plugins['available']:
135 msg = "Unknown plugin %s" % plugin
136 return self.root_with_msg(msg, "error")
137 obj = plugins['available'][plugin]
138 if obj in plugins['enabled']:
139 obj.disable(self._site)
140 msg = "Plugin %s disabled" % obj.name
141 return self.root_with_msg(msg, "success")
142 disable.exposed = True