Move login plugin configuration to its own module
[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.data import Store
22 from ipsilon.util.page import Page
23 from ipsilon.util.page import admin_protect
24
25
26 class AdminPluginPage(Page):
27
28     def __init__(self, obj, site, baseurl, facility):
29         super(AdminPluginPage, self).__init__(site)
30         self._obj = obj
31         self.url = '%s/%s' % (baseurl, obj.name)
32         self.facility = facility
33
34         # Get the defaults
35         self.plugin_config = obj.get_config_desc()
36         if not self.plugin_config:
37             self.plugin_config = []
38
39         # Now overlay the actual config
40         for option in self.plugin_config:
41             self.plugin_config[option][2] = obj.get_config_value(option)
42
43     @admin_protect
44     def GET(self, *args, **kwargs):
45         return self._template('admin/plugin_config.html',
46                               title='%s plugin' % self._obj.name,
47                               name='admin_%s_%s_form' % (self.facility,
48                                                          self._obj.name),
49                               action=self.url,
50                               options=self.plugin_config)
51
52     @admin_protect
53     def POST(self, *args, **kwargs):
54
55         message = "Nothing was modified."
56         message_type = "info"
57         new_values = dict()
58
59         for key, value in kwargs.iteritems():
60             if key in self.plugin_config:
61                 if value != self.plugin_config[key][2]:
62                     cherrypy.log.error("Storing [%s]: %s = %s" %
63                                        (self._obj.name, key, value))
64                     new_values[key] = value
65
66         if len(new_values) != 0:
67             # First we try to save in the database
68             try:
69                 store = Store()
70                 store.save_plugin_config(self.facility,
71                                          self._obj.name, new_values)
72                 message = "New configuration saved."
73                 message_type = "success"
74             except Exception:  # pylint: disable=broad-except
75                 message = "Failed to save data!"
76                 message_type = "error"
77
78             # And only if it succeeds we change the live object
79             for name, value in new_values.items():
80                 self._obj.set_config_value(name, value)
81                 self.plugin_config[name][2] = value
82
83         return self._template('admin/plugin_config.html',
84                               message=message,
85                               message_type=message_type,
86                               title='%s plugin' % self._obj.name,
87                               name='admin_%s_%s_form' % (self.facility,
88                                                          self._obj.name),
89                               action=self.url,
90                               options=self.plugin_config)
91
92     def root(self, *args, **kwargs):
93         cherrypy.log.error("method: %s" % cherrypy.request.method)
94         op = getattr(self, cherrypy.request.method, self.GET)
95         if callable(op):
96             return op(*args, **kwargs)
97
98
99 class Admin(Page):
100
101     def __init__(self, site, mount):
102         super(Admin, self).__init__(site)
103         self.url = '%s/%s' % (self.basepath, mount)
104
105     def root(self, *args, **kwargs):
106         return self._template('admin/index.html', title='Configuration')