]> asedeno.scripts.mit.edu Git - bluechips.git/blob - bluechips/lib/base.py
whoops, there was already user in request.environ
[bluechips.git] / bluechips / lib / base.py
1 """The base Controller API
2
3 Provides the BaseController class for subclassing.
4 """
5 from pylons import tmpl_context as c
6 from pylons.controllers import WSGIController
7 from pylons.i18n import _, ungettext, N_
8 from pylons.templating import render_mako as render
9
10 from tw.mods.pylonshf import validate, valid
11
12 import bluechips.lib.helpers as h
13 from bluechips import model
14 from bluechips.model import meta
15
16 from paste.request import construct_url
17 from paste.httpexceptions import HTTPMovedPermanently
18
19 class BaseController(WSGIController):
20
21     def __call__(self, environ, start_response):
22         """Invoke the Controller"""
23         # WSGIController.__call__ dispatches to the Controller method
24         # the request is routed to. This routing information is
25         # available in environ['pylons.routes_dict']
26         if environ['pylons.routes_dict']['controller'] != 'error':
27             if environ['PATH_INFO'].endswith('/index'):
28                 environ['PATH_INFO'] = environ['PATH_INFO'][:-5]
29                 raise HTTPMovedPermanently(construct_url(environ))
30             if not environ['PATH_INFO'].endswith('/') and \
31                     environ['pylons.routes_dict']['action'] == 'index':
32                 environ['PATH_INFO'] += '/'
33                 raise HTTPMovedPermanently(construct_url(environ))
34         try:
35             return WSGIController.__call__(self, environ, start_response)
36         finally:
37             meta.Session.remove()
38
39 def update_sar(record, form_result):
40     """
41     Update a SQLAlchemy record with the results of a validated form submission
42     """
43     for key, value in form_result.items():
44         setattr(record, key, value)
45
46 __all__ = ['c', 'h', 'render', 'validate', 'valid',
47            'model', 'meta', '_', 'ungettext', 'N_', 'BaseController',
48            'update_sar']