]> asedeno.scripts.mit.edu Git - bluechips.git/blobdiff - bluechips/lib/base.py
Always list residents first, then sort by username.
[bluechips.git] / bluechips / lib / base.py
index 2a9786c6cad857e909ef8c731174e04c37c8418a..e485c5732139b9768f4ba9be38821565615763f3 100644 (file)
@@ -2,18 +2,20 @@
 
 Provides the BaseController class for subclassing.
 """
-from pylons import tmpl_context as c
+
+from decorator import decorator
+
+from pylons import request, session, tmpl_context as c
 from pylons.controllers import WSGIController
 from pylons.i18n import _, ungettext, N_
+from pylons.templating import render_mako
 
-from tw.mods.pylonshf import render, render_response, validate
+from mako.exceptions import TopLevelLookupException
 
 import bluechips.lib.helpers as h
 from bluechips import model
 from bluechips.model import meta
 
-from paste.request import construct_url
-from paste.httpexceptions import HTTPMovedPermanently
 
 class BaseController(WSGIController):
 
@@ -22,14 +24,6 @@ class BaseController(WSGIController):
         # WSGIController.__call__ dispatches to the Controller method
         # the request is routed to. This routing information is
         # available in environ['pylons.routes_dict']
-        if environ['pylons.routes_dict']['controller'] != 'error':
-            if environ['PATH_INFO'].endswith('/index'):
-                environ['PATH_INFO'] = environ['PATH_INFO'][:-5]
-                raise HTTPMovedPermanently(construct_url(environ))
-            if not environ['PATH_INFO'].endswith('/') and \
-                    environ['pylons.routes_dict']['action'] == 'index':
-                environ['PATH_INFO'] += '/'
-                raise HTTPMovedPermanently(construct_url(environ))
         try:
             return WSGIController.__call__(self, environ, start_response)
         finally:
@@ -42,6 +36,40 @@ def update_sar(record, form_result):
     for key, value in form_result.items():
         setattr(record, key, value)
 
-__all__ = ['c', 'h', 'render', 'render_response', 'validate',
-           'model', 'meta', '_', 'ungettext', 'N_', 'BaseController',
-           'update_sar']
+def redirect_on_get(action):
+    """
+    Decorator for a controller action. If the action is called with a GET
+    method, 302 redirect to the action specified.
+    """
+
+    @decorator
+    def redirect_on_get_wrap(func, *args, **kwargs):
+        if request.method == 'GET':
+            controller = request.environ['pylons.routes_dict']['controller']
+            return h.redirect_to(controller=controller, action=action)
+        else:
+            return func(*args, **kwargs)
+    return redirect_on_get_wrap
+
+def render(name, *args, **kwargs):
+    if any([x in request.user_agent for x in ('iPhone','webOS')]):
+        if 'use_non_mobile' in request.params:
+            session['use_non_mobile'] = (request.params['use_non_mobile'] ==
+                                         'yes')
+        if session.get('use_non_mobile'):
+            c.mobile_client = True
+        else:
+            try:
+                return render_mako('/mobile' + name, *args, **kwargs)
+            except TopLevelLookupException:
+                # If a mobile template doesn't exist for this page, don't show
+                # the 'use mobile interface' link.
+                c.mobile_client = False
+    return render_mako(name, *args, **kwargs)
+
+def get_users():
+    return meta.Session.query(model.User.id, model.User).\
+        order_by(model.User.resident.desc(), model.User.username)
+
+__all__ = ['c', 'h', 'render', 'model', 'meta', '_', 'ungettext', 'N_',
+           'BaseController', 'update_sar', 'redirect_on_get', 'get_users']