Redirect anonymous users away
[cascardo/ipsilon.git] / ipsilon / admin / common.py
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2014  Simo Sorce <simo@redhat.com>
4 #
5 # see file 'COPYING' for use and warranty information
6 #
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.
11 #
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.
16 #
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/>.
19
20 import cherrypy
21 from ipsilon.util.page import Page
22 from ipsilon.util.page import admin_protect, auth_protect
23
24
25 class AdminPluginPage(Page):
26
27     def __init__(self, obj, site, parent):
28         super(AdminPluginPage, self).__init__(site, form=True)
29         self._obj = obj
30         self.title = '%s plugin' % obj.name
31         self.url = '%s/%s' % (parent.url, obj.name)
32         self.facility = parent.facility
33         self.menu = [parent]
34         self.back = parent.url
35
36         # Get the defaults
37         self.plugin_config = obj.get_config_desc()
38         if not self.plugin_config:
39             self.plugin_config = dict()
40
41         # Now overlay the actual config
42         for option in self.plugin_config:
43             self.plugin_config[option][2] = obj.get_config_value(option)
44
45         self.options_order = []
46         if hasattr(obj, 'conf_opt_order'):
47             self.options_order = obj.conf_opt_order
48
49         # append any undefined options
50         add = []
51         for k in self.plugin_config.keys():
52             if k not in self.options_order:
53                 add.append(k)
54         if len(add):
55             add.sort()
56             for k in add:
57                 self.options_order.append(k)
58
59     @admin_protect
60     def GET(self, *args, **kwargs):
61         return self._template('admin/plugin_config.html', title=self.title,
62                               name='admin_%s_%s_form' % (self.facility,
63                                                          self._obj.name),
64                               menu=self.menu, action=self.url, back=self.back,
65                               options_order=self.options_order,
66                               options=self.plugin_config)
67
68     @admin_protect
69     def POST(self, *args, **kwargs):
70
71         message = "Nothing was modified."
72         message_type = "info"
73         new_values = dict()
74
75         for key, value in kwargs.iteritems():
76             if key in self.plugin_config:
77                 if value != self.plugin_config[key][2]:
78                     cherrypy.log.error("Storing [%s]: %s = %s" %
79                                        (self._obj.name, key, value))
80                     new_values[key] = value
81
82         if len(new_values) != 0:
83             # First we try to save in the database
84             try:
85                 self._obj.save_plugin_config(self.facility, new_values)
86                 message = "New configuration saved."
87                 message_type = "success"
88             except Exception:  # pylint: disable=broad-except
89                 message = "Failed to save data!"
90                 message_type = "error"
91
92             # And only if it succeeds we change the live object
93             for name, value in new_values.items():
94                 self._obj.set_config_value(name, value)
95                 self.plugin_config[name][2] = value
96
97         return self._template('admin/plugin_config.html', title=self.title,
98                               message=message,
99                               message_type=message_type,
100                               name='admin_%s_%s_form' % (self.facility,
101                                                          self._obj.name),
102                               menu=self.menu, action=self.url,
103                               options=self.plugin_config)
104
105
106 class Admin(Page):
107
108     def __init__(self, site, mount):
109         super(Admin, self).__init__(site)
110         self.url = '%s/%s' % (self.basepath, mount)
111         self.menu = []
112
113     @auth_protect
114     def root(self, *args, **kwargs):
115         return self._template('admin/index.html',
116                               title='Configuration',
117                               menu=self.menu)
118
119     def add_subtree(self, name, page):
120         self.__dict__[name] = page
121         self.menu.append(page)
122
123     def del_subtree(self, name):
124         self.menu.remove(self.__dict__[name])
125         del self.__dict__[name]