]> asedeno.scripts.mit.edu Git - bluechips.git/blob - bluechips/lib/base.py
ab4b185efa442aaed57a0186991a21b32cde6cf7
[bluechips.git] / bluechips / lib / base.py
1 """The base Controller API
2
3 Provides the BaseController class for subclassing.
4 """
5
6 # Monkey-patch around a webhelpers/Pylons incompatibility
7 try:
8     import webhelpers.pylonslib.secure_form
9     import webhelpers.html
10     webhelpers.html.secure_form = webhelpers.pylonslib.secure_form
11 except ImportError:
12     pass
13
14 from decorator import decorator
15
16 from pylons import request, session, tmpl_context as c
17 from pylons.controllers import WSGIController
18 from pylons.i18n import _, ungettext, N_
19 from pylons.templating import render_mako
20
21 from mako.exceptions import TopLevelLookupException
22
23 import bluechips.lib.helpers as h
24 from bluechips import model
25 from bluechips.model import meta
26
27
28 class BaseController(WSGIController):
29
30     def __call__(self, environ, start_response):
31         """Invoke the Controller"""
32         # WSGIController.__call__ dispatches to the Controller method
33         # the request is routed to. This routing information is
34         # available in environ['pylons.routes_dict']
35         try:
36             return WSGIController.__call__(self, environ, start_response)
37         finally:
38             meta.Session.remove()
39
40 def update_sar(record, form_result):
41     """
42     Update a SQLAlchemy record with the results of a validated form submission
43     """
44     for key, value in form_result.items():
45         setattr(record, key, value)
46
47 def redirect_on_get(action):
48     """
49     Decorator for a controller action. If the action is called with a GET
50     method, 302 redirect to the action specified.
51     """
52
53     @decorator
54     def redirect_on_get_wrap(func, *args, **kwargs):
55         if request.method == 'GET':
56             controller = request.environ['pylons.routes_dict']['controller']
57             return h.redirect_to(controller=controller, action=action)
58         else:
59             return func(*args, **kwargs)
60     return redirect_on_get_wrap
61
62 def render(name, *args, **kwargs):
63     if any([x in request.user_agent for x in ('iPhone','webOS', 'Android')]):
64         if 'use_non_mobile' in request.params:
65             session['use_non_mobile'] = (request.params['use_non_mobile'] ==
66                                          'yes')
67         if session.get('use_non_mobile'):
68             c.mobile_client = True
69         else:
70             try:
71                 return render_mako('/mobile' + name, *args, **kwargs)
72             except TopLevelLookupException:
73                 # If a mobile template doesn't exist for this page, don't show
74                 # the 'use mobile interface' link.
75                 c.mobile_client = False
76     return render_mako(name, *args, **kwargs)
77
78 def get_users():
79     return meta.Session.query(model.User.id, model.User).\
80         order_by(model.User.resident.desc(), model.User.username)
81
82 __all__ = ['c', 'h', 'render', 'model', 'meta', '_', 'ungettext', 'N_',
83            'BaseController', 'update_sar', 'redirect_on_get', 'get_users']