Do not overwrite default plugin options
[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
23
24
25 class AdminPage(Page):
26
27     def __init__(self, *args, **kwargs):
28         super(AdminPage, self).__init__(*args, **kwargs)
29         self.default_headers.update({
30             'Cache-Control': 'no-cache, must-revalidate',
31             'Pragma': 'no-cache',
32             'Expires': 'Thu, 01 Dec 1994 16:00:00 GMT',
33         })
34         self.auth_protect = True
35
36
37 class AdminPluginPage(AdminPage):
38
39     def __init__(self, po, site, parent):
40         super(AdminPluginPage, self).__init__(site, form=True)
41         self._po = po
42         self.title = '%s plugin' % po.name
43         self.url = '%s/%s' % (parent.url, po.name)
44         self.facility = parent.facility
45         self.menu = [parent]
46         self.back = parent.url
47
48         # Get the defaults
49         options = po.get_config_desc()
50         if options is None:
51             options = dict()
52
53         self.options_order = []
54         if hasattr(po, 'conf_opt_order'):
55             self.options_order = po.conf_opt_order
56
57         # append any undefined options
58         add = []
59         for k in options.keys():
60             if k not in self.options_order:
61                 add.append(k)
62         if len(add):
63             add.sort()
64             for k in add:
65                 self.options_order.append(k)
66
67     @admin_protect
68     def GET(self, *args, **kwargs):
69         return self._template('admin/plugin_config.html', title=self.title,
70                               name='admin_%s_%s_form' % (self.facility,
71                                                          self._po.name),
72                               menu=self.menu, action=self.url, back=self.back,
73                               options_order=self.options_order,
74                               plugin=self._po)
75
76     @admin_protect
77     def POST(self, *args, **kwargs):
78
79         message = "Nothing was modified."
80         message_type = "info"
81         new_values = dict()
82
83         # Get the defaults
84         options = self._po.get_config_desc()
85         if options is None:
86             options = dict()
87
88         for key, value in kwargs.iteritems():
89             if key in options:
90                 if value != self._po.get_config_value(key):
91                     cherrypy.log.error("Storing [%s]: %s = %s" %
92                                        (self._po.name, key, value))
93                     new_values[key] = value
94
95         if len(new_values) != 0:
96             # First we try to save in the database
97             try:
98                 self._po.save_plugin_config(self.facility, new_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"
104
105             # And only if it succeeds we change the live object
106             self._po.refresh_plugin_config(self.facility)
107
108         return self._template('admin/plugin_config.html', title=self.title,
109                               message=message,
110                               message_type=message_type,
111                               name='admin_%s_%s_form' % (self.facility,
112                                                          self._po.name),
113                               menu=self.menu, action=self.url,
114                               plugin=self._po)
115
116
117 class Admin(AdminPage):
118
119     def __init__(self, site, mount):
120         super(Admin, self).__init__(site)
121         self.url = '%s/%s' % (self.basepath, mount)
122         self.menu = []
123
124     def root(self, *args, **kwargs):
125         return self._template('admin/index.html',
126                               title='Configuration',
127                               menu=self.menu)
128
129     def add_subtree(self, name, page):
130         self.__dict__[name] = page
131         self.menu.append(page)
132
133     def del_subtree(self, name):
134         self.menu.remove(self.__dict__[name])
135         del self.__dict__[name]