Fix Imports
[cascardo/ipsilon.git] / ipsilon / 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 from ipsilon.util.user import User
21 import cherrypy
22
23 def protect():
24     if cherrypy.request.login:
25         user = cherrypy.session.get('user', None)
26         if user == cherrypy.request.login:
27             return
28         else:
29             cherrypy.session.regenerate()
30             cherrypy.session['user'] = cherrypy.request.login
31
32 class Page(object):
33     def __init__(self, template_env):
34         self._env = template_env
35         self.basepath = cherrypy.config.get('base.mount', "")
36         self.username = None
37
38     def __call__(self, *args, **kwargs):
39         self.username = cherrypy.session.get('user', None)
40         self.user = User(self.username)
41
42         if len(args) > 0:
43             op = getattr(self, args[0], None)
44             if callable(op) and getattr(self, args[0]+'.exposed', None):
45                 return op(args[1:], **kwargs)
46         else:
47             op = getattr(self, 'root', None)
48             if callable(op):
49                 return op(**kwargs)
50
51         return self.default(*args, **kwargs)
52
53     def _template(self, *args, **kwargs):
54         t = self._env.get_template(args[0])
55         return t.render(basepath=self.basepath, user=self.user, **kwargs)
56
57     def default(self, *args, **kwargs):
58         raise cherrypy.HTTPError(404)
59
60     exposed = True