|
0
|
1 |
try: |
|
|
2 |
from functools import wraps |
|
|
3 |
except ImportError: |
|
29
|
4 |
from django.utils.functional import wraps # Python 2.4 fallback. |
|
0
|
5 |
|
|
|
6 |
from django.utils.cache import patch_vary_headers |
|
29
|
7 |
from django.utils.decorators import available_attrs |
|
0
|
8 |
|
|
|
9 |
def vary_on_headers(*headers): |
|
|
10 |
""" |
|
|
11 |
A view decorator that adds the specified headers to the Vary header of the |
|
|
12 |
response. Usage: |
|
|
13 |
|
|
|
14 |
@vary_on_headers('Cookie', 'Accept-language') |
|
|
15 |
def index(request): |
|
|
16 |
... |
|
|
17 |
|
|
|
18 |
Note that the header names are not case-sensitive. |
|
|
19 |
""" |
|
|
20 |
def decorator(func): |
|
|
21 |
def inner_func(*args, **kwargs): |
|
|
22 |
response = func(*args, **kwargs) |
|
|
23 |
patch_vary_headers(response, headers) |
|
|
24 |
return response |
|
29
|
25 |
return wraps(func, assigned=available_attrs(func))(inner_func) |
|
0
|
26 |
return decorator |
|
|
27 |
|
|
|
28 |
def vary_on_cookie(func): |
|
|
29 |
""" |
|
|
30 |
A view decorator that adds "Cookie" to the Vary header of a response. This |
|
|
31 |
indicates that a page's contents depends on cookies. Usage: |
|
|
32 |
|
|
|
33 |
@vary_on_cookie |
|
|
34 |
def index(request): |
|
|
35 |
... |
|
|
36 |
""" |
|
|
37 |
def inner_func(*args, **kwargs): |
|
|
38 |
response = func(*args, **kwargs) |
|
|
39 |
patch_vary_headers(response, ('Cookie',)) |
|
|
40 |
return response |
|
29
|
41 |
return wraps(func, assigned=available_attrs(func))(inner_func) |