2013-04-29 11:05:09 +02:00
|
|
|
import json
|
|
|
|
from functools import wraps
|
|
|
|
from flask import redirect, request, current_app
|
|
|
|
import re
|
|
|
|
|
|
|
|
jsonp_validator = re.compile("^[a-zA-Z0-9_\-.]{1,128}$")
|
|
|
|
|
2018-11-30 01:28:24 +01:00
|
|
|
|
2013-04-29 11:05:09 +02:00
|
|
|
def jsonp(f):
|
2018-11-30 01:28:24 +01:00
|
|
|
"""Wraps JSONified output for JSONP"""
|
|
|
|
@wraps(f)
|
|
|
|
def decorated_function(*args, **kwargs):
|
|
|
|
callback = request.args.get('callback', False)
|
|
|
|
if callback and jsonp_validator.match(callback):
|
|
|
|
content = str(callback) + '(' + str(f(*args, **kwargs).data) + ')'
|
|
|
|
return current_app.response_class(
|
|
|
|
content, mimetype='application/javascript')
|
|
|
|
else:
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
return decorated_function
|