b42d2be4a1a63bd7e30c02a71d0943ad613bbe97
[zzz-floof.git] / floof / lib / base.py
1 """The base Controller API
2
3 Provides the BaseController class for subclassing.
4 """
5 from pylons.controllers import WSGIController
6 from pylons.controllers.util import abort, redirect
7 from pylons.templating import render_mako as render
8 from pylons import config, request, session, tmpl_context as c
9 from routes import request_config
10
11 from floof import model
12 from floof.model.users import User
13
14 class BaseController(WSGIController):
15
16 # NOTE: This could have been implemented as a middleware =]
17 def __before__(self):
18 c.route = request_config().mapper_dict
19
20 # Fetch current user object
21 try:
22 c.user = User.query.get(session['user_id'])
23 except:
24 pass
25
26 def __call__(self, environ, start_response):
27 """Invoke the Controller"""
28 # WSGIController.__call__ dispatches to the Controller method
29 # the request is routed to. This routing information is
30 # available in environ['pylons.routes_dict']
31
32 # Insert any code to be run per request here.
33
34 try:
35 return WSGIController.__call__(self, environ, start_response)
36 finally:
37 model.Session.remove()
38
39
40 def redirect_to_referrer(self):
41 """Performs a redirect_to to wherever we came from. Used for stuff
42 like logging in.
43 """
44 referrer = request.headers.get('REFERER', '/')
45 redirect(referrer, code=302)