app/django/views/decorators/vary.py
author Pawel Solyga <Pawel.Solyga@gmail.com>
Fri, 22 Aug 2008 13:44:50 +0000
changeset 99 8c38b546a3cf
parent 54 03e267d67478
permissions -rw-r--r--
Added public view support (not using controller yet) Changed Google Account variable from user to id in views and templates (no more confusions with Melange user). Added templates_helpers for makeSimblingTemplate function. Addded new template tag readonly_field_as_table_row used in public views Fixed one typo in roles.py

try:
    from functools import wraps
except ImportError:
    from django.utils.functional import wraps  # Python 2.3, 2.4 fallback.

from django.utils.cache import patch_vary_headers

def vary_on_headers(*headers):
    """
    A view decorator that adds the specified headers to the Vary header of the
    response. Usage:

       @vary_on_headers('Cookie', 'Accept-language')
       def index(request):
           ...

    Note that the header names are not case-sensitive.
    """
    def decorator(func):
        def inner_func(*args, **kwargs):
            response = func(*args, **kwargs)
            patch_vary_headers(response, headers)
            return response
        return wraps(func)(inner_func)
    return decorator

def vary_on_cookie(func):
    """
    A view decorator that adds "Cookie" to the Vary header of a response. This
    indicates that a page's contents depends on cookies. Usage:

        @vary_on_cookie
        def index(request):
            ...
    """
    def inner_func(*args, **kwargs):
        response = func(*args, **kwargs)
        patch_vary_headers(response, ('Cookie',))
        return response
    return wraps(func)(inner_func)