X-Git-Url: https://asedeno.scripts.mit.edu/gitweb/?a=blobdiff_plain;f=bluechips%2Flib%2Fbase.py;h=586336c3a351ff1c25ce3f14f01624601cc8d03e;hb=01eb085c206c4ba88045fb88a2f1076622bd2585;hp=d94eeb664e40d3fda2561fda30b042f1f50e3893;hpb=3b08315d7a6dee99e751f16b9a5a050884bf54f8;p=bluechips.git diff --git a/bluechips/lib/base.py b/bluechips/lib/base.py index d94eeb6..586336c 100644 --- a/bluechips/lib/base.py +++ b/bluechips/lib/base.py @@ -2,19 +2,17 @@ Provides the BaseController class for subclassing. """ -from pylons import tmpl_context as c +from decorator import decorator + +from pylons import request, tmpl_context as c from pylons.controllers import WSGIController from pylons.i18n import _, ungettext, N_ from pylons.templating import render_mako as render -from tw.mods.pylonshf import validate, valid - 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): @@ -23,17 +21,7 @@ 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: - c.user = meta.Session.query(model.User).\ - filter_by(username=unicode(environ['REMOTE_USER'])).one() return WSGIController.__call__(self, environ, start_response) finally: meta.Session.remove() @@ -45,6 +33,21 @@ def update_sar(record, form_result): for key, value in form_result.items(): setattr(record, key, value) -__all__ = ['c', 'h', 'render', 'validate', 'valid', - '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 + + +__all__ = ['c', 'h', 'render', 'model', 'meta', '_', 'ungettext', 'N_', + 'BaseController', 'update_sar', 'redirect_on_get']