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