]> asedeno.scripts.mit.edu Git - bluechips.git/blob - bluechips/lib/base.py
renamed dashboard.png to status.png for consistency
[bluechips.git] / bluechips / lib / base.py
1 """The base Controller API
2
3 Provides the BaseController class for subclassing.
4 """
5 from decorator import decorator
6
7 from pylons import request, tmpl_context as c
8 from pylons.controllers import WSGIController
9 from pylons.i18n import _, ungettext, N_
10 from pylons.templating import render_mako as render
11
12 import bluechips.lib.helpers as h
13 from bluechips import model
14 from bluechips.model import meta
15
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         try:
25             return WSGIController.__call__(self, environ, start_response)
26         finally:
27             meta.Session.remove()
28
29 def update_sar(record, form_result):
30     """
31     Update a SQLAlchemy record with the results of a validated form submission
32     """
33     for key, value in form_result.items():
34         setattr(record, key, value)
35
36 def redirect_on_get(action):
37     """
38     Decorator for a controller action. If the action is called with a GET
39     method, 302 redirect to the action specified.
40     """
41
42     @decorator
43     def redirect_on_get_wrap(func, *args, **kwargs):
44         if request.method == 'GET':
45             controller = request.environ['pylons.routes_dict']['controller']
46             return h.redirect_to(controller=controller, action=action)
47         else:
48             return func(*args, **kwargs)
49     return redirect_on_get_wrap
50
51
52 __all__ = ['c', 'h', 'render', 'model', 'meta', '_', 'ungettext', 'N_',
53            'BaseController', 'update_sar', 'redirect_on_get']