Add page class handler
[cascardo/ipsilon.git] / src / util / page.py
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2013  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
22 def protect():
23     if cherrypy.request.login:
24         user = cherrypy.session.get('user', None)
25         if user == cherrypy.request.login:
26             return
27         else:
28             cherrypy.session.regenerate()
29             cherrypy.session['user'] = cherrypy.request.login
30
31 class Page(object):
32     def __init__(self, template_env):
33         self._env = template_env
34         self.username = None
35
36     def __call__(self, *args, **kwargs):
37         self.username = cherrypy.session.get('user', None)
38
39         if len(args) > 0:
40             op = getattr(self, args[0], None)
41             if callable(op) and getattr(self, args[0]+'.exposed', None):
42                 return op(args[1:], **kwargs)
43         else:
44             op = getattr(self, 'root', None)
45             if callable(op):
46                 return op(**kwargs)
47
48         return self.default(*args, **kwargs)
49
50     def default(self, *args, **kwargs):
51         raise cherrypy.HTTPError(404)
52
53     exposed = True