3 # Copyright (C) 2014 Simo Sorce <simo@redhat.com>
5 # see file 'COPYING' for use and warranty information
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 from ipsilon.util.page import Page
22 from ipsilon.util.page import admin_protect
23 from ipsilon.util.plugin import PluginObject
26 class AdminPage(Page):
28 def __init__(self, *args, **kwargs):
29 super(AdminPage, self).__init__(*args, **kwargs)
30 self.default_headers.update({
31 'Cache-Control': 'no-cache, must-revalidate',
33 'Expires': 'Thu, 01 Dec 1994 16:00:00 GMT',
35 self.auth_protect = True
38 class AdminPluginConfig(AdminPage):
40 def __init__(self, po, site, parent):
41 super(AdminPluginConfig, self).__init__(site, form=True)
43 self.title = '%s plugin' % po.name
44 self.url = '%s/%s' % (parent.url, po.name)
45 self.facility = parent.facility
47 self.back = parent.url
50 options = po.get_config_desc()
54 self.options_order = []
55 if hasattr(po, 'conf_opt_order'):
56 self.options_order = po.conf_opt_order
58 # append any undefined options
60 for k in options.keys():
61 if k not in self.options_order:
66 self.options_order.append(k)
68 def root_with_msg(self, message=None, message_type=None):
69 return self._template('admin/plugin_config.html', title=self.title,
70 menu=self.menu, action=self.url, back=self.back,
71 message=message, message_type=message_type,
72 name='admin_%s_%s_form' % (self.facility,
74 options_order=self.options_order,
78 def GET(self, *args, **kwargs):
79 return self.root_with_msg()
82 def POST(self, *args, **kwargs):
84 message = "Nothing was modified."
89 options = self._po.get_config_desc()
93 for key, value in kwargs.iteritems():
95 if value != self._po.get_config_value(key):
96 cherrypy.log.error("Storing [%s]: %s = %s" %
97 (self._po.name, key, value))
98 new_values[key] = value
100 if len(new_values) != 0:
101 # First we try to save in the database
103 self._po.save_plugin_config(self.facility, new_values)
104 message = "New configuration saved."
105 message_type = "success"
106 except Exception: # pylint: disable=broad-except
107 message = "Failed to save data!"
108 message_type = "error"
110 # And only if it succeeds we change the live object
111 self._po.refresh_plugin_config(self.facility)
113 return self.root_with_msg(message=message,
114 message_type=message_type)
117 class AdminPluginsOrder(AdminPage):
119 def __init__(self, site, parent, facility):
120 super(AdminPluginsOrder, self).__init__(site, form=True)
122 self.facility = facility
123 self.url = '%s/order' % parent.url
127 def GET(self, *args, **kwargs):
128 return self.parent.root_with_msg()
131 def POST(self, *args, **kwargs):
132 message = "Nothing was modified."
133 message_type = "info"
134 by_name = {p.name: p for p in self._site[self.facility]['enabled']}
136 if 'order' in kwargs:
137 order = kwargs['order'].split(',')
144 if val not in by_name:
145 error = "Invalid plugin name: %s" % val
146 raise ValueError(error)
147 new_names.append(val)
148 new_plugins.append(by_name[val])
149 if len(new_names) < len(by_name):
151 if val not in new_names:
152 new_names.append(val)
153 new_plugins.append(by_name[val])
155 self.parent.save_enabled_plugins(new_names)
156 self.parent.reorder_plugins(new_names)
158 # When all is saved update also live config. The
159 # live config is a list of the actual plugin
161 self._site[self.facility]['enabled'] = new_plugins
163 message = "New configuration saved."
164 message_type = "success"
166 except ValueError, e:
168 message_type = "error"
170 except Exception, e: # pylint: disable=broad-except
171 message = "Failed to save data!"
172 message_type = "error"
174 return self.parent.root_with_msg(message=message,
175 message_type=message_type)
178 class AdminPlugins(AdminPage):
179 def __init__(self, name, site, parent, facility, ordered=True):
180 super(AdminPlugins, self).__init__(site)
181 self._master = parent
183 self.title = '%s plugins' % name
184 self.url = '%s/%s' % (parent.url, name)
185 self.facility = facility
186 self.template = 'admin/plugins.html'
188 parent.add_subtree(name, self)
190 for plugin in self._site[facility]['available']:
191 cherrypy.log.error('Admin info plugin: %s' % plugin)
192 obj = self._site[facility]['available'][plugin]
193 page = AdminPluginConfig(obj, self._site, self)
194 if hasattr(obj, 'admin'):
195 obj.admin.mount(page)
196 self.add_subtree(plugin, page)
199 self.order = AdminPluginsOrder(self._site, self, facility)
201 def save_enabled_plugins(self, names):
205 globalconf['order'] = ','.join(names)
206 po.set_config(globalconf)
207 po.save_plugin_config(self.facility)
209 def reorder_plugins(self, names):
212 def root_with_msg(self, message=None, message_type=None):
213 plugins = self._site[self.facility]
216 for plugin in plugins['enabled']:
217 enabled.append(plugin.name)
219 for _, plugin in plugins['available'].iteritems():
220 if plugin.is_enabled:
221 enabled.append(plugin.name)
223 targs = {'title': self.title,
224 'menu': self._master.menu,
226 'message_type': message_type,
227 'available': plugins['available'],
231 targs['order_name'] = '%s_order_form' % self.name
232 targs['order_action'] = self.order.url
234 # pylint: disable=star-args
235 return self._template(self.template, **targs)
237 def root(self, *args, **kwargs):
238 return self.root_with_msg()
241 def enable(self, plugin):
243 plugins = self._site[self.facility]
244 if plugin not in plugins['available']:
245 msg = "Unknown plugin %s" % plugin
246 return self.root_with_msg(msg, "error")
247 obj = plugins['available'][plugin]
248 if not obj.is_enabled:
249 obj.enable(self._site)
251 enabled = list(x.name for x in plugins['enabled'])
252 self.save_enabled_plugins(enabled)
253 msg = "Plugin %s enabled" % obj.name
254 return self.root_with_msg(msg, "success")
255 enable.public_function = True
258 def disable(self, plugin):
260 plugins = self._site[self.facility]
261 if plugin not in plugins['available']:
262 msg = "Unknown plugin %s" % plugin
263 return self.root_with_msg(msg, "error")
264 obj = plugins['available'][plugin]
266 obj.disable(self._site)
268 enabled = list(x.name for x in plugins['enabled'])
269 self.save_enabled_plugins(enabled)
270 msg = "Plugin %s disabled" % obj.name
271 return self.root_with_msg(msg, "success")
272 disable.public_function = True
275 class Admin(AdminPage):
277 def __init__(self, site, mount):
278 super(Admin, self).__init__(site)
281 self.url = '%s/%s' % (self.basepath, mount)
284 def root(self, *args, **kwargs):
285 return self._template('admin/index.html',
286 title='Configuration',
290 def add_subtree(self, name, page):
291 self.__dict__[name] = page
292 self.menu.append(page)
294 def del_subtree(self, name):
295 self.menu.remove(self.__dict__[name])
296 del self.__dict__[name]
298 def get_menu_urls(self):
300 for item in self.menu:
301 name = getattr(item, 'name', None)
303 urls['%s_url' % name] = cherrypy.url('/%s/%s' % (self.mount,
309 cherrypy.response.headers.update({'Content-Type': 'image/svg+xml'})
310 urls = self.get_menu_urls()
311 # pylint: disable=star-args
312 return self._template('admin/ipsilon-scheme.svg', **urls)
313 scheme.public_function = True