Use pylint check
[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         self.user = None
38
39     def __call__(self, *args, **kwargs):
40         # pylint: disable=star-args
41         self.username = cherrypy.session.get('user', None)
42         self.user = User(self.username)
43
44         if len(args) > 0:
45             op = getattr(self, args[0], None)
46             if callable(op) and getattr(self, args[0]+'.exposed', None):
47                 return op(args[1:], **kwargs)
48         else:
49             op = getattr(self, 'root', None)
50             if callable(op):
51                 return op(**kwargs)
52
53         return self.default(*args, **kwargs)
54
55     def _template(self, *args, **kwargs):
56         t = self._env.get_template(args[0])
57         return t.render(basepath=self.basepath, user=self.user, **kwargs)
58
59     def default(self, *args, **kwargs):
60         raise cherrypy.HTTPError(404)
61
62     exposed = True