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