Convert all forms to use util.Page form support
[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, parent):
29         super(AdminPluginPage, self).__init__(site, form=True)
30         self._obj = obj
31         self.title = '%s plugin' % obj.name
32         self.url = '%s/%s' % (parent.url, obj.name)
33         self.facility = parent.facility
34         self.menu = [parent]
35
36         # Get the defaults
37         self.plugin_config = obj.get_config_desc()
38         if not self.plugin_config:
39             self.plugin_config = []
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     @admin_protect
46     def GET(self, *args, **kwargs):
47         return self._template('admin/plugin_config.html', title=self.title,
48                               name='admin_%s_%s_form' % (self.facility,
49                                                          self._obj.name),
50                               menu=self.menu, action=self.url,
51                               options=self.plugin_config)
52
53     @admin_protect
54     def POST(self, *args, **kwargs):
55
56         message = "Nothing was modified."
57         message_type = "info"
58         new_values = dict()
59
60         for key, value in kwargs.iteritems():
61             if key in self.plugin_config:
62                 if value != self.plugin_config[key][2]:
63                     cherrypy.log.error("Storing [%s]: %s = %s" %
64                                        (self._obj.name, key, value))
65                     new_values[key] = value
66
67         if len(new_values) != 0:
68             # First we try to save in the database
69             try:
70                 store = Store()
71                 store.save_plugin_config(self.facility,
72                                          self._obj.name, new_values)
73                 message = "New configuration saved."
74                 message_type = "success"
75             except Exception:  # pylint: disable=broad-except
76                 message = "Failed to save data!"
77                 message_type = "error"
78
79             # And only if it succeeds we change the live object
80             for name, value in new_values.items():
81                 self._obj.set_config_value(name, value)
82                 self.plugin_config[name][2] = value
83
84         return self._template('admin/plugin_config.html', title=self.title,
85                               message=message,
86                               message_type=message_type,
87                               name='admin_%s_%s_form' % (self.facility,
88                                                          self._obj.name),
89                               menu=self.menu, action=self.url,
90                               options=self.plugin_config)
91
92
93 class Admin(Page):
94
95     def __init__(self, site, mount):
96         super(Admin, self).__init__(site)
97         self.url = '%s/%s' % (self.basepath, mount)
98         self.menu = []
99
100     def root(self, *args, **kwargs):
101         return self._template('admin/index.html',
102                               title='Configuration',
103                               menu=self.menu)
104
105     def add_subtree(self, name, page):
106         self.__dict__[name] = page
107         self.menu.append(page)
108
109     def del_subtree(self, name):
110         self.menu.remove(self.__dict__[name])
111         del self.__dict__[name]