From a5a89b5a432df1f8c9003dd3b3b8b93675746da3 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Sun, 9 Oct 2005 00:37:56 +0000 Subject: [PATCH] Moved vary decorators from django.utils.cache to django.views.decorators.vary git-svn-id: http://code.djangoproject.com/svn/django/trunk@809 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/utils/cache.py | 42 ++++----------------------------- django/views/decorators/vary.py | 35 +++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 38 deletions(-) create mode 100644 django/views/decorators/vary.py diff --git a/django/utils/cache.py b/django/utils/cache.py index 26b60c4040..fcd0825a22 100644 --- a/django/utils/cache.py +++ b/django/utils/cache.py @@ -1,8 +1,8 @@ """ -This module contains helper functions and decorators for controlling caching. -It does so by managing the "Vary" header of responses. It includes functions -to patch the header of response objects directly and decorators that change -functions to do that header-patching themselves. +This module contains helper functions for controlling caching. It does so by +managing the "Vary" header of responses. It includes functions to patch the +header of response objects directly and decorators that change functions to do +that header-patching themselves. For information on the Vary header, see: @@ -64,40 +64,6 @@ def patch_vary_headers(response, newheaders): vary.append(newheader) response['Vary'] = ', '.join(vary) -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 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 inner_func - def _generate_cache_key(request, headerlist, key_prefix): "Returns a cache key from the headers given in the header list." ctx = md5.new() diff --git a/django/views/decorators/vary.py b/django/views/decorators/vary.py new file mode 100644 index 0000000000..9b49c45cf2 --- /dev/null +++ b/django/views/decorators/vary.py @@ -0,0 +1,35 @@ +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 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 inner_func