m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
2.0 KiB

6 months ago
  1. from datetime import timedelta
  2. from flask import make_response, request, current_app
  3. from functools import update_wrapper
  4. def crossdomain(origin=None, methods=None, headers=None, expose_headers=None,
  5. max_age=21600, attach_to_all=True,
  6. automatic_options=True, credentials=False):
  7. """
  8. http://flask.pocoo.org/snippets/56/
  9. """
  10. if methods is not None:
  11. methods = ', '.join(sorted(x.upper() for x in methods))
  12. if headers is not None and not isinstance(headers, str):
  13. headers = ', '.join(x.upper() for x in headers)
  14. if expose_headers is not None and not isinstance(expose_headers, str):
  15. expose_headers = ', '.join(x.upper() for x in expose_headers)
  16. if not isinstance(origin, str):
  17. origin = ', '.join(origin)
  18. if isinstance(max_age, timedelta):
  19. max_age = max_age.total_seconds()
  20. def get_methods():
  21. if methods is not None:
  22. return methods
  23. options_resp = current_app.make_default_options_response()
  24. return options_resp.headers['allow']
  25. def decorator(f):
  26. def wrapped_function(*args, **kwargs):
  27. if automatic_options and request.method == 'OPTIONS':
  28. resp = current_app.make_default_options_response()
  29. else:
  30. resp = make_response(f(*args, **kwargs))
  31. if not attach_to_all and request.method != 'OPTIONS':
  32. return resp
  33. h = resp.headers
  34. h['Access-Control-Allow-Origin'] = origin
  35. h['Access-Control-Allow-Methods'] = get_methods()
  36. h['Access-Control-Max-Age'] = str(max_age)
  37. if credentials:
  38. h['Access-Control-Allow-Credentials'] = 'true'
  39. if headers is not None:
  40. h['Access-Control-Allow-Headers'] = headers
  41. if expose_headers is not None:
  42. h['Access-Control-Expose-Headers'] = expose_headers
  43. return resp
  44. f.provide_automatic_options = False
  45. return update_wrapper(wrapped_function, f)
  46. return decorator