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 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
49 def root_with_msg(self, message=None, message_type=None):
50 return self._template('admin/plugin_config.html', title=self.title,
51 menu=self.menu, action=self.url, back=self.back,
52 message=message, message_type=message_type,
53 name='admin_%s_%s_form' % (self.facility,
55 config=self._po.get_config_obj())
58 def GET(self, *args, **kwargs):
59 return self.root_with_msg()
62 def POST(self, *args, **kwargs):
64 message = "Nothing was modified."
66 new_db_values = dict()
68 conf = self._po.get_config_obj()
70 for name, option in conf.iteritems():
73 if isinstance(option, pconfig.List):
74 value = [x.strip() for x in value.split('\n')]
75 elif isinstance(option, pconfig.Condition):
78 if isinstance(option, pconfig.Condition):
80 elif isinstance(option, pconfig.Choice):
82 for a in option.get_allowed():
83 aname = '%s_%s' % (name, a)
89 if value != option.get_value():
90 cherrypy.log.error("Storing [%s]: %s = %s" %
91 (self._po.name, name, value))
92 option.set_value(value)
93 new_db_values[name] = option.export_value()
95 if len(new_db_values) != 0:
96 # First we try to save in the database
98 self._po.save_plugin_config(new_db_values)
99 message = "New configuration saved."
100 message_type = "success"
101 except Exception: # pylint: disable=broad-except
102 message = "Failed to save data!"
103 message_type = "error"
105 # Then refresh the actual objects
106 self._po.refresh_plugin_config()
108 return self.root_with_msg(message=message,
109 message_type=message_type)
112 class AdminPluginsOrder(AdminPage):
114 def __init__(self, site, parent, facility):
115 super(AdminPluginsOrder, self).__init__(site, form=True)
117 self.facility = facility
118 self.url = '%s/order' % parent.url
122 def GET(self, *args, **kwargs):
123 return self.parent.root_with_msg()
125 def _get_enabled_list(self):
127 for p in self._site[self.facility].available.values():
133 def POST(self, *args, **kwargs):
134 message = "Nothing was modified."
135 message_type = "info"
136 cur_enabled = self._get_enabled_list()
138 if 'order' in kwargs:
139 order = kwargs['order'].split(',')
145 if val not in cur_enabled:
146 error = "Invalid plugin name: %s" % val
147 raise ValueError(error)
148 new_order.append(val)
149 if len(new_order) < len(cur_enabled):
150 for val in cur_enabled:
151 if val not in new_order:
152 new_order.append(val)
154 self.parent.save_enabled_plugins(new_order)
156 # When all is saved update also live config. The
157 # live config is the ordered list of plugin names.
158 self._site[self.facility].refresh_enabled()
160 message = "New configuration saved."
161 message_type = "success"
163 except ValueError, e:
165 message_type = "error"
167 except Exception, e: # pylint: disable=broad-except
168 message = "Failed to save data!"
169 message_type = "error"
171 return self.parent.root_with_msg(message=message,
172 message_type=message_type)
175 class AdminPlugins(AdminPage):
176 def __init__(self, name, site, parent, facility, ordered=True):
177 super(AdminPlugins, self).__init__(site)
178 self._master = parent
180 self.title = '%s plugins' % name
181 self.url = '%s/%s' % (parent.url, name)
182 self.facility = facility
183 self.template = 'admin/plugins.html'
185 parent.add_subtree(name, self)
187 for plugin in self._site[facility].available:
188 cherrypy.log.error('Admin info plugin: %s' % plugin)
189 obj = self._site[facility].available[plugin]
190 page = AdminPluginConfig(obj, self._site, self)
191 if hasattr(obj, 'admin'):
192 obj.admin.mount(page)
193 self.add_subtree(plugin, page)
196 self.order = AdminPluginsOrder(self._site, self, facility)
198 def save_enabled_plugins(self, names):
199 self._site[self.facility].save_enabled(names)
201 def root_with_msg(self, message=None, message_type=None):
202 plugins = self._site[self.facility]
204 targs = {'title': self.title,
205 'menu': self._master.menu,
207 'message_type': message_type,
208 'available': plugins.available,
209 'enabled': plugins.enabled,
213 targs['order_name'] = '%s_order_form' % self.name
214 targs['order_action'] = self.order.url
216 # pylint: disable=star-args
217 return self._template(self.template, **targs)
219 def root(self, *args, **kwargs):
220 return self.root_with_msg()
223 def enable(self, plugin):
225 plugins = self._site[self.facility]
226 if plugin not in plugins.available:
227 msg = "Unknown plugin %s" % plugin
228 return self.root_with_msg(msg, "error")
229 obj = plugins.available[plugin]
230 if not obj.is_enabled:
232 obj.save_enabled_state()
233 msg = "Plugin %s enabled" % obj.name
234 return self.root_with_msg(msg, "success")
235 enable.public_function = True
238 def disable(self, plugin):
240 plugins = self._site[self.facility]
241 if plugin not in plugins.available:
242 msg = "Unknown plugin %s" % plugin
243 return self.root_with_msg(msg, "error")
244 obj = plugins.available[plugin]
247 obj.save_enabled_state()
248 msg = "Plugin %s disabled" % obj.name
249 return self.root_with_msg(msg, "success")
250 disable.public_function = True
253 class Admin(AdminPage):
255 def __init__(self, site, mount):
256 super(Admin, self).__init__(site)
259 self.url = '%s/%s' % (self.basepath, mount)
262 def root(self, *args, **kwargs):
263 return self._template('admin/index.html',
264 title='Configuration',
268 def add_subtree(self, name, page):
269 self.__dict__[name] = page
270 self.menu.append(page)
272 def del_subtree(self, name):
273 self.menu.remove(self.__dict__[name])
274 del self.__dict__[name]
276 def get_menu_urls(self):
278 for item in self.menu:
279 name = getattr(item, 'name', None)
281 urls['%s_url' % name] = cherrypy.url('/%s/%s' % (self.mount,
287 cherrypy.response.headers.update({'Content-Type': 'image/svg+xml'})
288 urls = self.get_menu_urls()
289 # pylint: disable=star-args
290 return self._template('admin/ipsilon-scheme.svg', **urls)
291 scheme.public_function = True