Add Info providers Admin pages
[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         self.back = parent.url
36
37         # Get the defaults
38         self.plugin_config = obj.get_config_desc()
39         if not self.plugin_config:
40             self.plugin_config = dict()
41
42         # Now overlay the actual config
43         for option in self.plugin_config:
44             self.plugin_config[option][2] = obj.get_config_value(option)
45
46         self.options_order = []
47         if hasattr(obj, 'conf_opt_order'):
48             self.options_order = obj.conf_opt_order
49
50         # append any undefined options
51         add = []
52         for k in self.plugin_config.keys():
53             if k not in self.options_order:
54                 add.append(k)
55         if len(add):
56             add.sort()
57             for k in add:
58                 self.options_order.append(k)
59
60     @admin_protect
61     def GET(self, *args, **kwargs):
62         return self._template('admin/plugin_config.html', title=self.title,
63                               name='admin_%s_%s_form' % (self.facility,
64                                                          self._obj.name),
65                               menu=self.menu, action=self.url, back=self.back,
66                               options_order=self.options_order,
67                               options=self.plugin_config)
68
69     @admin_protect
70     def POST(self, *args, **kwargs):
71
72         message = "Nothing was modified."
73         message_type = "info"
74         new_values = dict()
75
76         for key, value in kwargs.iteritems():
77             if key in self.plugin_config:
78                 if value != self.plugin_config[key][2]:
79                     cherrypy.log.error("Storing [%s]: %s = %s" %
80                                        (self._obj.name, key, value))
81                     new_values[key] = value
82
83         if len(new_values) != 0:
84             # First we try to save in the database
85             try:
86                 store = Store()
87                 store.save_plugin_config(self.facility,
88                                          self._obj.name, new_values)
89                 message = "New configuration saved."
90                 message_type = "success"
91             except Exception:  # pylint: disable=broad-except
92                 message = "Failed to save data!"
93                 message_type = "error"
94
95             # And only if it succeeds we change the live object
96             for name, value in new_values.items():
97                 self._obj.set_config_value(name, value)
98                 self.plugin_config[name][2] = value
99
100         return self._template('admin/plugin_config.html', title=self.title,
101                               message=message,
102                               message_type=message_type,
103                               name='admin_%s_%s_form' % (self.facility,
104                                                          self._obj.name),
105                               menu=self.menu, action=self.url,
106                               options=self.plugin_config)
107
108
109 class Admin(Page):
110
111     def __init__(self, site, mount):
112         super(Admin, self).__init__(site)
113         self.url = '%s/%s' % (self.basepath, mount)
114         self.menu = []
115
116     def root(self, *args, **kwargs):
117         return self._template('admin/index.html',
118                               title='Configuration',
119                               menu=self.menu)
120
121     def add_subtree(self, name, page):
122         self.__dict__[name] = page
123         self.menu.append(page)
124
125     def del_subtree(self, name):
126         self.menu.remove(self.__dict__[name])
127         del self.__dict__[name]