cea6b0e089c41b146af0bd29bb8f5b000cf39018
[cascardo/ipsilon.git] / ipsilon / admin / info.py
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2014  Ipsilon Contributors see COPYING for license
4
5 import cherrypy
6 from ipsilon.util.page import admin_protect
7 from ipsilon.util.plugin import PluginObject
8 from ipsilon.admin.common import AdminPluginPage
9 from ipsilon.admin.common import AdminPage
10 from ipsilon.info.common import FACILITY
11
12
13 class InfoPluginsOrder(AdminPage):
14
15     def __init__(self, site, parent):
16         super(InfoPluginsOrder, self).__init__(site, form=True)
17         self.url = '%s/order' % parent.url
18         self.menu = [parent]
19
20     @admin_protect
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,
27                               options=opts)
28
29     @admin_protect
30     def POST(self, *args, **kwargs):
31         message = "Nothing was modified."
32         message_type = "info"
33         plugins_by_name = {p.name: p for p in self._site[FACILITY]['enabled']}
34
35         if 'order' in kwargs:
36             order = kwargs['order'].split(',')
37             if len(order) != 0:
38                 new_names = []
39                 new_plugins = []
40                 try:
41                     for v in order:
42                         val = v.strip()
43                         if val not in plugins_by_name:
44                             error = "Invalid plugin name: %s" % val
45                             raise ValueError(error)
46                         new_names.append(val)
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:
51                                 new_names.append(val)
52                                 new_plugins.append(plugins_by_name[val])
53
54                     po = PluginObject()
55                     po.name = "global"
56                     globalconf = dict()
57                     globalconf['order'] = ','.join(new_names)
58                     po.set_config(globalconf)
59                     po.save_plugin_config(FACILITY)
60
61                     # When all is saved update also live config. The
62                     # live config is a list of the actual plugin
63                     # objects.
64                     self._site[FACILITY]['enabled'] = new_plugins
65
66                     message = "New configuration saved."
67                     message_type = "success"
68
69                 except ValueError, e:
70                     message = str(e)
71                     message_type = "error"
72
73                 except Exception, e:  # pylint: disable=broad-except
74                     message = "Failed to save data!"
75                     message_type = "error"
76
77         opts = [p.name for p in self._site[FACILITY]['enabled']]
78         return self._template('admin/info_order.html',
79                               message=message,
80                               message_type=message_type,
81                               title='info plugins order',
82                               name='admin_info_order_form',
83                               menu=self.menu, action=self.url,
84                               options=opts)
85
86
87 class InfoPlugins(AdminPage):
88     def __init__(self, site, parent):
89         super(InfoPlugins, self).__init__(site)
90         self._master = parent
91         self.title = 'Info Plugins'
92         self.url = '%s/info' % parent.url
93         self.facility = FACILITY
94         parent.add_subtree('info', self)
95
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)
100
101         self.order = InfoPluginsOrder(self._site, self)
102
103     def root_with_msg(self, message=None, message_type=None):
104         info_plugins = self._site[FACILITY]
105         ordered = []
106         for p in info_plugins['enabled']:
107             ordered.append(p.name)
108         return self._template('admin/info.html', title=self.title,
109                               message=message,
110                               message_type=message_type,
111                               available=info_plugins['available'],
112                               enabled=ordered,
113                               menu=self._master.menu)
114
115     def root(self, *args, **kwargs):
116         return self.root_with_msg()
117
118     @admin_protect
119     def enable(self, plugin):
120         msg = None
121         plugins = self._site[FACILITY]
122         if plugin not in plugins['available']:
123             msg = "Unknown plugin %s" % plugin
124             return self.root_with_msg(msg, "error")
125         obj = plugins['available'][plugin]
126         if obj not in plugins['enabled']:
127             obj.enable(self._site)
128             msg = "Plugin %s enabled" % obj.name
129         return self.root_with_msg(msg, "success")
130     enable.public_function = True
131
132     @admin_protect
133     def disable(self, plugin):
134         msg = None
135         plugins = self._site[FACILITY]
136         if plugin not in plugins['available']:
137             msg = "Unknown plugin %s" % plugin
138             return self.root_with_msg(msg, "error")
139         obj = plugins['available'][plugin]
140         if obj in plugins['enabled']:
141             obj.disable(self._site)
142             msg = "Plugin %s disabled" % obj.name
143         return self.root_with_msg(msg, "success")
144     disable.public_function = True