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