PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/django-1.4/django/views/decorators/cache.py

https://github.com/theosp/google_appengine
Python | 92 lines | 86 code | 4 blank | 2 comment | 2 complexity | 6834c9f3062cab97a7ebe1104f133957 MD5 | raw file
  1. from functools import wraps
  2. from django.utils.decorators import decorator_from_middleware_with_args, available_attrs
  3. from django.utils.cache import patch_cache_control, add_never_cache_headers
  4. from django.middleware.cache import CacheMiddleware
  5. def cache_page(*args, **kwargs):
  6. """
  7. Decorator for views that tries getting the page from the cache and
  8. populates the cache if the page isn't in the cache yet.
  9. The cache is keyed by the URL and some data from the headers.
  10. Additionally there is the key prefix that is used to distinguish different
  11. cache areas in a multi-site setup. You could use the
  12. sites.get_current().domain, for example, as that is unique across a Django
  13. project.
  14. Additionally, all headers from the response's Vary header will be taken
  15. into account on caching -- just like the middleware does.
  16. """
  17. # We need backwards compatibility with code which spells it this way:
  18. # def my_view(): pass
  19. # my_view = cache_page(my_view, 123)
  20. # and this way:
  21. # my_view = cache_page(123)(my_view)
  22. # and this:
  23. # my_view = cache_page(my_view, 123, key_prefix="foo")
  24. # and this:
  25. # my_view = cache_page(123, key_prefix="foo")(my_view)
  26. # and possibly this way (?):
  27. # my_view = cache_page(123, my_view)
  28. # and also this way:
  29. # my_view = cache_page(my_view)
  30. # and also this way:
  31. # my_view = cache_page()(my_view)
  32. # We also add some asserts to give better error messages in case people are
  33. # using other ways to call cache_page that no longer work.
  34. cache_alias = kwargs.pop('cache', None)
  35. key_prefix = kwargs.pop('key_prefix', None)
  36. assert not kwargs, "The only keyword arguments are cache and key_prefix"
  37. def warn():
  38. import warnings
  39. warnings.warn('The cache_page decorator must be called like: '
  40. 'cache_page(timeout, [cache=cache name], [key_prefix=key prefix]). '
  41. 'All other ways are deprecated.',
  42. PendingDeprecationWarning,
  43. stacklevel=3)
  44. if len(args) > 1:
  45. assert len(args) == 2, "cache_page accepts at most 2 arguments"
  46. warn()
  47. if callable(args[0]):
  48. return decorator_from_middleware_with_args(CacheMiddleware)(cache_timeout=args[1], cache_alias=cache_alias, key_prefix=key_prefix)(args[0])
  49. elif callable(args[1]):
  50. return decorator_from_middleware_with_args(CacheMiddleware)(cache_timeout=args[0], cache_alias=cache_alias, key_prefix=key_prefix)(args[1])
  51. else:
  52. assert False, "cache_page must be passed a view function if called with two arguments"
  53. elif len(args) == 1:
  54. if callable(args[0]):
  55. warn()
  56. return decorator_from_middleware_with_args(CacheMiddleware)(cache_alias=cache_alias, key_prefix=key_prefix)(args[0])
  57. else:
  58. # The One True Way
  59. return decorator_from_middleware_with_args(CacheMiddleware)(cache_timeout=args[0], cache_alias=cache_alias, key_prefix=key_prefix)
  60. else:
  61. warn()
  62. return decorator_from_middleware_with_args(CacheMiddleware)(cache_alias=cache_alias, key_prefix=key_prefix)
  63. def cache_control(**kwargs):
  64. def _cache_controller(viewfunc):
  65. @wraps(viewfunc, assigned=available_attrs(viewfunc))
  66. def _cache_controlled(request, *args, **kw):
  67. response = viewfunc(request, *args, **kw)
  68. patch_cache_control(response, **kwargs)
  69. return response
  70. return _cache_controlled
  71. return _cache_controller
  72. def never_cache(view_func):
  73. """
  74. Decorator that adds headers to a response so that it will
  75. never be cached.
  76. """
  77. @wraps(view_func, assigned=available_attrs(view_func))
  78. def _wrapped_view_func(request, *args, **kwargs):
  79. response = view_func(request, *args, **kwargs)
  80. add_never_cache_headers(response)
  81. return response
  82. return _wrapped_view_func