]> asedeno.scripts.mit.edu Git - bluechips.git/blob - bluechips/lib/base.py
d94eeb664e40d3fda2561fda30b042f1f50e3893
[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             c.user = meta.Session.query(model.User).\
36                     filter_by(username=unicode(environ['REMOTE_USER'])).one()
37             return WSGIController.__call__(self, environ, start_response)
38         finally:
39             meta.Session.remove()
40
41 def update_sar(record, form_result):
42     """
43     Update a SQLAlchemy record with the results of a validated form submission
44     """
45     for key, value in form_result.items():
46         setattr(record, key, value)
47
48 __all__ = ['c', 'h', 'render', 'validate', 'valid',
49            'model', 'meta', '_', 'ungettext', 'N_', 'BaseController',
50            'update_sar']