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 import config as pconfig
26 class AdminError(Exception):
27 def __init__(self, message):
28 super(AdminError, self).__init__(message)
29 self.message = message
32 return str(self.message)
35 class AdminPage(Page):
37 def __init__(self, *args, **kwargs):
38 super(AdminPage, self).__init__(*args, **kwargs)
39 self.default_headers.update({
40 'Cache-Control': 'no-cache, must-revalidate',
42 'Expires': 'Thu, 01 Dec 1994 16:00:00 GMT',
44 self.auth_protect = True
47 class AdminPluginConfig(AdminPage):
49 def __init__(self, po, site, parent):
50 super(AdminPluginConfig, self).__init__(site, form=True)
52 self.title = '%s plugin' % po.name
53 self.url = '%s/%s' % (parent.url, po.name)
54 self.facility = parent.facility
56 self.back = parent.url
58 def root_with_msg(self, message=None, message_type=None):
59 return self._template('admin/plugin_config.html', title=self.title,
60 menu=self.menu, action=self.url, back=self.back,
61 message=message, message_type=message_type,
62 name='admin_%s_%s_form' % (self.facility,
64 config=self._po.get_config_obj())
67 def GET(self, *args, **kwargs):
68 return self.root_with_msg()
71 def POST(self, *args, **kwargs):
73 if self._po.is_readonly:
74 return self.root_with_msg(
75 message="Configuration is marked Read-Only",
76 message_type="warning")
78 message = "Nothing was modified."
80 new_db_values = dict()
82 conf = self._po.get_config_obj()
84 for name, option in conf.iteritems():
87 if isinstance(option, pconfig.List):
88 value = [x.strip() for x in value.split('\n')]
89 elif isinstance(option, pconfig.Condition):
92 if isinstance(option, pconfig.Condition):
94 elif isinstance(option, pconfig.Choice):
96 for a in option.get_allowed():
97 aname = '%s_%s' % (name, a)
103 if value != option.get_value():
104 cherrypy.log.error("Storing [%s]: %s = %s" %
105 (self._po.name, name, value))
106 option.set_value(value)
107 new_db_values[name] = option.export_value()
109 if len(new_db_values) != 0:
110 # First we try to save in the database
112 self._po.save_plugin_config(new_db_values)
113 message = "New configuration saved."
114 message_type = "success"
115 except Exception: # pylint: disable=broad-except
116 message = "Failed to save data!"
117 message_type = "error"
119 # Then refresh the actual objects
120 self._po.refresh_plugin_config()
122 return self.root_with_msg(message=message,
123 message_type=message_type)
126 class AdminPluginsOrder(AdminPage):
128 def __init__(self, site, parent, facility):
129 super(AdminPluginsOrder, self).__init__(site, form=True)
131 self.facility = facility
132 self.url = '%s/order' % parent.url
136 def GET(self, *args, **kwargs):
137 return self.parent.root_with_msg()
139 def _get_enabled_list(self):
141 for p in self._site[self.facility].available.values():
147 def POST(self, *args, **kwargs):
149 if self._site[self.facility].is_readonly:
150 return self.parent.root_with_msg(
151 message="Configuration is marked Read-Only",
152 message_type="warning")
154 message = "Nothing was modified."
155 message_type = "info"
156 cur_enabled = self._get_enabled_list()
158 if 'order' in kwargs:
159 order = kwargs['order'].split(',')
165 if val not in cur_enabled:
166 error = "Invalid plugin name: %s" % val
167 raise ValueError(error)
168 new_order.append(val)
169 if len(new_order) < len(cur_enabled):
170 for val in cur_enabled:
171 if val not in new_order:
172 new_order.append(val)
174 self.parent.save_enabled_plugins(new_order)
176 # When all is saved update also live config. The
177 # live config is the ordered list of plugin names.
178 self._site[self.facility].refresh_enabled()
180 message = "New configuration saved."
181 message_type = "success"
183 except ValueError, e:
185 message_type = "error"
187 except Exception, e: # pylint: disable=broad-except
188 message = "Failed to save data!"
189 message_type = "error"
191 return self.parent.root_with_msg(message=message,
192 message_type=message_type)
195 class AdminPlugins(AdminPage):
196 def __init__(self, name, site, parent, facility, ordered=True):
197 super(AdminPlugins, self).__init__(site)
198 self._master = parent
200 self.title = '%s plugins' % name
201 self.url = '%s/%s' % (parent.url, name)
202 self.facility = facility
203 self.template = 'admin/plugins.html'
205 parent.add_subtree(name, self)
207 for plugin in self._site[facility].available:
208 cherrypy.log.error('Admin info plugin: %s' % plugin)
209 obj = self._site[facility].available[plugin]
210 page = AdminPluginConfig(obj, self._site, self)
211 if hasattr(obj, 'admin'):
212 obj.admin.mount(page)
213 self.add_subtree(plugin, page)
216 self.order = AdminPluginsOrder(self._site, self, facility)
218 def save_enabled_plugins(self, names):
219 self._site[self.facility].save_enabled(names)
221 def root_with_msg(self, message=None, message_type=None):
222 plugins = self._site[self.facility]
224 targs = {'title': self.title,
225 'menu': self._master.menu,
227 'message_type': message_type,
228 'available': plugins.available,
229 'enabled': plugins.enabled,
233 targs['order_name'] = '%s_order_form' % self.name
234 targs['order_action'] = self.order.url
236 # pylint: disable=star-args
237 return self._template(self.template, **targs)
239 def root(self, *args, **kwargs):
240 return self.root_with_msg()
242 def _get_plugin_obj(self, plugin):
243 plugins = self._site[self.facility]
244 if plugins.is_readonly:
245 msg = "Configuration is marked Read-Only"
246 raise AdminError(msg)
247 if plugin not in plugins.available:
248 msg = "Unknown plugin %s" % plugin
249 raise AdminError(msg)
250 obj = plugins.available[plugin]
252 msg = "Plugin Configuration is marked Read-Only"
253 raise AdminError(msg)
257 def enable(self, plugin):
260 obj = self._get_plugin_obj(plugin)
261 except AdminError, e:
262 return self.root_with_msg(str(e), "warning")
263 if not obj.is_enabled:
265 obj.save_enabled_state()
266 msg = "Plugin %s enabled" % obj.name
267 return self.root_with_msg(msg, "success")
268 enable.public_function = True
271 def disable(self, plugin):
274 obj = self._get_plugin_obj(plugin)
275 except AdminError, e:
276 return self.root_with_msg(str(e), "warning")
279 obj.save_enabled_state()
280 msg = "Plugin %s disabled" % obj.name
281 return self.root_with_msg(msg, "success")
282 disable.public_function = True
285 class Admin(AdminPage):
287 def __init__(self, site, mount):
288 super(Admin, self).__init__(site)
291 self.url = '%s/%s' % (self.basepath, mount)
294 def root(self, *args, **kwargs):
295 return self._template('admin/index.html',
296 title='Configuration',
300 def add_subtree(self, name, page):
301 self.__dict__[name] = page
302 self.menu.append(page)
304 def del_subtree(self, name):
305 self.menu.remove(self.__dict__[name])
306 del self.__dict__[name]
308 def get_menu_urls(self):
310 for item in self.menu:
311 name = getattr(item, 'name', None)
313 urls['%s_url' % name] = cherrypy.url('/%s/%s' % (self.mount,
319 cherrypy.response.headers.update({'Content-Type': 'image/svg+xml'})
320 urls = self.get_menu_urls()
321 # pylint: disable=star-args
322 return self._template('admin/ipsilon-scheme.svg', **urls)
323 scheme.public_function = True