web/lib/django/views/decorators/vary.py
changeset 38 77b6da96e6f1
equal deleted inserted replaced
37:8d941af65caf 38:77b6da96e6f1
       
     1 try:
       
     2     from functools import wraps
       
     3 except ImportError:
       
     4     from django.utils.functional import wraps  # Python 2.4 fallback.
       
     5 
       
     6 from django.utils.cache import patch_vary_headers
       
     7 from django.utils.decorators import available_attrs
       
     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
       
    25         return wraps(func, assigned=available_attrs(func))(inner_func)
       
    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
       
    41     return wraps(func, assigned=available_attrs(func))(inner_func)