]> asedeno.scripts.mit.edu Git - bluechips.git/blobdiff - bluechips/lib/base.py
Fix division of currency
[bluechips.git] / bluechips / lib / base.py
index d33cfc2034b901e29bfa09a2b895270045c538a8..d04f98160d38c95a8e50171c704ae34378c37263 100644 (file)
@@ -2,11 +2,21 @@
 
 Provides the BaseController class for subclassing.
 """
+
+from decorator import decorator
+
+from pylons import request, session, tmpl_context as c
 from pylons.controllers import WSGIController
-from pylons.templating import render_mako as render
+from pylons.i18n import _, ungettext, N_
+from pylons.templating import render_mako
+
+from mako.exceptions import TopLevelLookupException
 
+import bluechips.lib.helpers as h
+from bluechips import model
 from bluechips.model import meta
 
+
 class BaseController(WSGIController):
 
     def __call__(self, environ, start_response):
@@ -18,4 +28,44 @@ class BaseController(WSGIController):
             return WSGIController.__call__(self, environ, start_response)
         finally:
             meta.Session.remove()
-        
\ No newline at end of file
+
+def update_sar(record, form_result):
+    """
+    Update a SQLAlchemy record with the results of a validated form submission
+    """
+    for key, value in form_result.items():
+        setattr(record, key, value)
+
+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)
+
+__all__ = ['c', 'h', 'render', 'model', 'meta', '_', 'ungettext', 'N_',
+           'BaseController', 'update_sar', 'redirect_on_get']