Decorator for simplifying controller methods that return json (just return

and object that simplejson can understand and it will be jsonified)
This commit is contained in:
James Taylor
2007-10-09 23:23:08 +00:00
parent 2471f3222f
commit 7831ea5a2a
2 changed files with 13 additions and 1 deletions
+1 -1
View File
@@ -2,5 +2,5 @@
The Galaxy web application.
"""
from framework import expose, url_for, FormBuilder
from framework import expose, json, url_for, FormBuilder
+12
View File
@@ -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 ):