]> asedeno.scripts.mit.edu Git - bluechips.git/blob - bluechips/lib/base.py
ripped out toscawidgets, replaced with formencode, put split editing on main expendit...
[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 import bluechips.lib.helpers as h
11 from bluechips import model
12 from bluechips.model import meta
13
14 from paste.request import construct_url
15 from paste.httpexceptions import HTTPMovedPermanently
16
17 class BaseController(WSGIController):
18
19     def __call__(self, environ, start_response):
20         """Invoke the Controller"""
21         # WSGIController.__call__ dispatches to the Controller method
22         # the request is routed to. This routing information is
23         # available in environ['pylons.routes_dict']
24         if environ['pylons.routes_dict']['controller'] != 'error':
25             if environ['PATH_INFO'].endswith('/index'):
26                 environ['PATH_INFO'] = environ['PATH_INFO'][:-5]
27                 raise HTTPMovedPermanently(construct_url(environ))
28             if not environ['PATH_INFO'].endswith('/') and \
29                     environ['pylons.routes_dict']['action'] == 'index':
30                 environ['PATH_INFO'] += '/'
31                 raise HTTPMovedPermanently(construct_url(environ))
32         try:
33             return WSGIController.__call__(self, environ, start_response)
34         finally:
35             meta.Session.remove()
36
37 def update_sar(record, form_result):
38     """
39     Update a SQLAlchemy record with the results of a validated form submission
40     """
41     for key, value in form_result.items():
42         setattr(record, key, value)
43
44 __all__ = ['c', 'h', 'render', 'model', 'meta', '_', 'ungettext', 'N_',
45            'BaseController', 'update_sar']