Redirect anonymous users away
[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 Page
7 from ipsilon.util.page import admin_protect, auth_protect
8 from ipsilon.util.plugin import PluginObject
9 from ipsilon.admin.common import AdminPluginPage
10 from ipsilon.info.common import FACILITY
11
12
13 class InfoPluginsOrder(Page):
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(Page):
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     @auth_protect
116     def root(self, *args, **kwargs):
117         return self.root_with_msg()
118
119     @admin_protect
120     def enable(self, plugin):
121         msg = None
122         plugins = self._site[FACILITY]
123         if plugin not in plugins['available']:
124             msg = "Unknown plugin %s" % plugin
125             return self.root_with_msg(msg, "error")
126         obj = plugins['available'][plugin]
127         if obj not in plugins['enabled']:
128             obj.enable(self._site)
129             msg = "Plugin %s enabled" % obj.name
130         return self.root_with_msg(msg, "success")
131     enable.exposed = True
132
133     @admin_protect
134     def disable(self, plugin):
135         msg = None
136         plugins = self._site[FACILITY]
137         if plugin not in plugins['available']:
138             msg = "Unknown plugin %s" % plugin
139             return self.root_with_msg(msg, "error")
140         obj = plugins['available'][plugin]
141         if obj in plugins['enabled']:
142             obj.disable(self._site)
143             msg = "Plugin %s disabled" % obj.name
144         return self.root_with_msg(msg, "success")
145     disable.exposed = True