From 7831ea5a2a02ab3b735f6090834ce2e4a8f222f4 Mon Sep 17 00:00:00 2001 From: James Taylor Date: Tue, 9 Oct 2007 23:23:08 +0000 Subject: [PATCH] Decorator for simplifying controller methods that return json (just return and object that simplejson can understand and it will be jsonified) --- lib/galaxy/web/__init__.py | 2 +- lib/galaxy/web/framework/__init__.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/galaxy/web/__init__.py b/lib/galaxy/web/__init__.py index f84f21b96e0..a4c45b928fc 100644 --- a/lib/galaxy/web/__init__.py +++ b/lib/galaxy/web/__init__.py @@ -2,5 +2,5 @@ The Galaxy web application. """ -from framework import expose, url_for, FormBuilder +from framework import expose, json, url_for, FormBuilder diff --git a/lib/galaxy/web/framework/__init__.py b/lib/galaxy/web/framework/__init__.py index a54e1f20550..c2a7b1a4919 100644 --- a/lib/galaxy/web/framework/__init__.py +++ b/lib/galaxy/web/framework/__init__.py @@ -21,6 +21,9 @@ pkg_resources.require( "Mako" ) import mako.template import mako.lookup +pkg_resources.require( "simplejson" ) +import simplejson + url_for = base.routes.url_for def expose( func ): @@ -30,6 +33,15 @@ def expose( func ): func.exposed = True return func +def json( func ): + def decorator( self, trans, *args, **kwargs ): + trans.response.set_content_type( "text/javascript" ) + return simplejson.dumps( func( self, trans, *args, **kwargs ) ) + if not hasattr(func, '_orig'): + decorator._orig = func + decorator.exposed = True + return decorator + NOT_SET = object() class WebApplication( base.WebApplication ):