1 """The base Controller API
3 Provides the BaseController class for subclassing.
6 # Monkey-patch around a webhelpers/Pylons incompatibility
8 import webhelpers.pylonslib.secure_form
10 webhelpers.html.secure_form = webhelpers.pylonslib.secure_form
14 from decorator import decorator
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
21 from mako.exceptions import TopLevelLookupException
23 import bluechips.lib.helpers as h
24 from bluechips import model
25 from bluechips.model import meta
28 class BaseController(WSGIController):
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']
36 return WSGIController.__call__(self, environ, start_response)
40 def update_sar(record, form_result):
42 Update a SQLAlchemy record with the results of a validated form submission
44 for key, value in form_result.items():
45 setattr(record, key, value)
47 def redirect_on_get(action):
49 Decorator for a controller action. If the action is called with a GET
50 method, 302 redirect to the action specified.
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)
59 return func(*args, **kwargs)
60 return redirect_on_get_wrap
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'] ==
67 if session.get('use_non_mobile'):
68 c.mobile_client = True
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)
79 return meta.Session.query(model.User.id, model.User).\
80 order_by(model.User.resident.desc(), model.User.username)
82 __all__ = ['c', 'h', 'render', 'model', 'meta', '_', 'ungettext', 'N_',
83 'BaseController', 'update_sar', 'redirect_on_get', 'get_users']