Skip to content

Instantly share code, notes, and snippets.

@fredj
Created May 28, 2010 14:56
Show Gist options
  • Save fredj/417252 to your computer and use it in GitHub Desktop.
Save fredj/417252 to your computer and use it in GitHub Desktop.
from pylons.decorators.util import get_pylons
from decorator import decorator
from geojson import FeatureCollection, dumps
@decorator
def geojsonify(func, *args, **kwargs):
pylons = get_pylons(args)
pylons.response.content_type = 'application/json'
data = func(*args, **kwargs)
if isinstance(data, list): # sucks !!
return dumps(FeatureCollection(data))
else:
return dumps(data)
from pylons.decorators.util import get_pylons
from decorator import decorator
@decorator
def jsonp(func, *args, **kwargs):
pylons = get_pylons(args)
fn_name = pylons.request.params.get('jsonp') or \
pylons.request.params.get('callback') or \
pylons.request.params.get('cb')
body = func(*args, **kwargs)
pylons.response.charset = 'utf8'
if fn_name is not None:
response.content_type = 'application/javascript'
return "%s(%s);"%(fn_name, body)
else:
response.content_type = 'application/json'
return body
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment