Commits

imbolc committed 82aa65a

jsonp support in json-response

Comments (0)

Files changed (2)

pysi/decorators.py

     ''' Декоратор html-response '''
     return as_response(content_type='text/html', **kwargs)
 
-def as_json(**kwargs):
-    ''' Декоратор json_response '''
-    return as_response(dumper=json.dumps,
-        content_type='application/json', **kwargs)
+#~ def as_json(**kwargs):
+    #~ ''' Декоратор json_response '''
+    #~ return as_response(dumper=json.dumps,
+        #~ content_type='application/json', **kwargs)
+
+def as_json(**response_kwargs):
+    '''
+    Декоратор json-response
+    '''
+    def decorator(func):
+        def wrapper(rq, *args, **kwargs):
+            res = func(rq, *args, **kwargs)
+            if not isinstance(res, Response):
+                res = json.dumps(res)
+                if 'callback' in rq.GET:
+                    res = '%s(%s)' % (rq.GET['callback'], res)  # jsonp
+                    ct = 'application/x-javascript'
+                else:
+                    ct = 'application/json'
+                return Response(res, content_type=ct, **response_kwargs)
+            wrapper._pysi_urlname = get_urlname(func)
+        return wrapper
+    return decorator
 
 def to_template(fn, **response_kwargs):
     '''
 from setuptools import setup
 
 PACKAGE = 'pysi'
-VERSION = '0.5'
+VERSION = '0.5.1'
 
 if __name__ == '__main__':
     # Compile the list of packages available, because distutils doesn't have