PageRenderTime 23ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Django-1.4.3/django/views/decorators/csrf.py

https://bitbucket.org/ducopdep/tiny_blog
Python | 79 lines | 77 code | 2 blank | 0 comment | 1 complexity | bec1a93acf4f79d361610a745360ec5e MD5 | raw file
Possible License(s): BSD-3-Clause
  1. import warnings
  2. from django.middleware.csrf import CsrfViewMiddleware, get_token
  3. from django.utils.decorators import decorator_from_middleware, available_attrs
  4. from functools import wraps
  5. csrf_protect = decorator_from_middleware(CsrfViewMiddleware)
  6. csrf_protect.__name__ = "csrf_protect"
  7. csrf_protect.__doc__ = """
  8. This decorator adds CSRF protection in exactly the same way as
  9. CsrfViewMiddleware, but it can be used on a per view basis. Using both, or
  10. using the decorator multiple times, is harmless and efficient.
  11. """
  12. class _EnsureCsrfToken(CsrfViewMiddleware):
  13. # We need this to behave just like the CsrfViewMiddleware, but not reject
  14. # requests.
  15. def _reject(self, request, reason):
  16. return None
  17. requires_csrf_token = decorator_from_middleware(_EnsureCsrfToken)
  18. requires_csrf_token.__name__ = 'requires_csrf_token'
  19. requires_csrf_token.__doc__ = """
  20. Use this decorator on views that need a correct csrf_token available to
  21. RequestContext, but without the CSRF protection that csrf_protect
  22. enforces.
  23. """
  24. class _EnsureCsrfCookie(CsrfViewMiddleware):
  25. def _reject(self, request, reason):
  26. return None
  27. def process_view(self, request, callback, callback_args, callback_kwargs):
  28. retval = super(_EnsureCsrfCookie, self).process_view(request, callback, callback_args, callback_kwargs)
  29. # Forces process_response to send the cookie
  30. get_token(request)
  31. return retval
  32. ensure_csrf_cookie = decorator_from_middleware(_EnsureCsrfCookie)
  33. ensure_csrf_cookie.__name__ = 'ensure_csrf_cookie'
  34. ensure_csrf_cookie.__doc__ = """
  35. Use this decorator to ensure that a view sets a CSRF cookie, whether or not it
  36. uses the csrf_token template tag, or the CsrfViewMiddleware is used.
  37. """
  38. def csrf_response_exempt(view_func):
  39. """
  40. Modifies a view function so that its response is exempt
  41. from the post-processing of the CSRF middleware.
  42. """
  43. warnings.warn("csrf_response_exempt is deprecated. It no longer performs a "
  44. "function, and calls to it can be removed.",
  45. PendingDeprecationWarning)
  46. return view_func
  47. def csrf_view_exempt(view_func):
  48. """
  49. Marks a view function as being exempt from CSRF view protection.
  50. """
  51. warnings.warn("csrf_view_exempt is deprecated. Use csrf_exempt instead.",
  52. PendingDeprecationWarning)
  53. return csrf_exempt(view_func)
  54. def csrf_exempt(view_func):
  55. """
  56. Marks a view function as being exempt from the CSRF view protection.
  57. """
  58. # We could just do view_func.csrf_exempt = True, but decorators
  59. # are nicer if they don't have side-effects, so we return a new
  60. # function.
  61. def wrapped_view(*args, **kwargs):
  62. return view_func(*args, **kwargs)
  63. wrapped_view.csrf_exempt = True
  64. return wraps(view_func, assigned=available_attrs(view_func))(wrapped_view)