Removed legacy ways of calling cache_page.

This commit is contained in:
Aymeric Augustin 2012-12-24 23:43:34 +01:00
parent 9f9a4cdecd
commit 2ecf56ea3f
3 changed files with 13 additions and 91 deletions

View File

@ -18,54 +18,17 @@ def cache_page(*args, **kwargs):
Additionally, all headers from the response's Vary header will be taken
into account on caching -- just like the middleware does.
"""
# We need backwards compatibility with code which spells it this way:
# def my_view(): pass
# my_view = cache_page(my_view, 123)
# and this way:
# my_view = cache_page(123)(my_view)
# and this:
# my_view = cache_page(my_view, 123, key_prefix="foo")
# and this:
# my_view = cache_page(123, key_prefix="foo")(my_view)
# and possibly this way (?):
# my_view = cache_page(123, my_view)
# and also this way:
# my_view = cache_page(my_view)
# and also this way:
# my_view = cache_page()(my_view)
# We also add some asserts to give better error messages in case people are
# using other ways to call cache_page that no longer work.
if len(args) != 1 or callable(args[0]):
raise TypeError("cache_page has a single mandatory positional argument: timeout")
cache_timeout = args[0]
cache_alias = kwargs.pop('cache', None)
key_prefix = kwargs.pop('key_prefix', None)
assert not kwargs, "The only keyword arguments are cache and key_prefix"
def warn():
import warnings
warnings.warn('The cache_page decorator must be called like: '
'cache_page(timeout, [cache=cache name], [key_prefix=key prefix]). '
'All other ways are deprecated.',
DeprecationWarning,
stacklevel=2)
if kwargs:
raise TypeError("cache_page has two optional keyword arguments: cache and key_prefix")
if len(args) > 1:
assert len(args) == 2, "cache_page accepts at most 2 arguments"
warn()
if callable(args[0]):
return decorator_from_middleware_with_args(CacheMiddleware)(cache_timeout=args[1], cache_alias=cache_alias, key_prefix=key_prefix)(args[0])
elif callable(args[1]):
return decorator_from_middleware_with_args(CacheMiddleware)(cache_timeout=args[0], cache_alias=cache_alias, key_prefix=key_prefix)(args[1])
else:
assert False, "cache_page must be passed a view function if called with two arguments"
elif len(args) == 1:
if callable(args[0]):
warn()
return decorator_from_middleware_with_args(CacheMiddleware)(cache_alias=cache_alias, key_prefix=key_prefix)(args[0])
else:
# The One True Way
return decorator_from_middleware_with_args(CacheMiddleware)(cache_timeout=args[0], cache_alias=cache_alias, key_prefix=key_prefix)
else:
warn()
return decorator_from_middleware_with_args(CacheMiddleware)(cache_alias=cache_alias, key_prefix=key_prefix)
return decorator_from_middleware_with_args(CacheMiddleware)(cache_timeout=cache_timeout, cache_alias=cache_alias, key_prefix=key_prefix)
def cache_control(**kwargs):

View File

@ -1478,19 +1478,12 @@ def hello_world_view(request, value):
)
class CacheMiddlewareTest(TestCase):
# The following tests will need to be modified in Django 1.6 to not use
# deprecated ways of using the cache_page decorator that will be removed in
# such version
def setUp(self):
self.factory = RequestFactory()
self.default_cache = get_cache('default')
self.other_cache = get_cache('other')
self.save_warnings_state()
warnings.filterwarnings('ignore', category=DeprecationWarning,
module='django.views.decorators.cache')
def tearDown(self):
self.restore_warnings_state()
self.default_cache.clear()
self.other_cache.clear()
@ -1603,21 +1596,20 @@ class CacheMiddlewareTest(TestCase):
request.user = MockAuthenticatedUser()
request.session = MockAccessedSession()
response = cache_page(hello_world_view)(request, '1')
response = cache_page(60)(hello_world_view)(request, '1')
self.assertFalse("Cache-Control" in response)
def test_view_decorator(self):
# decorate the same view with different cache decorators
default_view = cache_page(hello_world_view)
default_with_prefix_view = cache_page(key_prefix='prefix1')(hello_world_view)
default_view = cache_page(3)(hello_world_view)
default_with_prefix_view = cache_page(3, key_prefix='prefix1')(hello_world_view)
explicit_default_view = cache_page(cache='default')(hello_world_view)
explicit_default_with_prefix_view = cache_page(cache='default', key_prefix='prefix1')(hello_world_view)
explicit_default_view = cache_page(3, cache='default')(hello_world_view)
explicit_default_with_prefix_view = cache_page(3, cache='default', key_prefix='prefix1')(hello_world_view)
other_view = cache_page(cache='other')(hello_world_view)
other_with_prefix_view = cache_page(cache='other', key_prefix='prefix2')(hello_world_view)
other_with_timeout_view = cache_page(3, cache='other', key_prefix='prefix3')(hello_world_view)
other_view = cache_page(1, cache='other')(hello_world_view)
other_with_prefix_view = cache_page(1, cache='other', key_prefix='prefix2')(hello_world_view)
request = self.factory.get('/view/')
@ -1657,10 +1649,6 @@ class CacheMiddlewareTest(TestCase):
response = other_with_prefix_view(request, '9')
self.assertEqual(response.content, b'Hello World 9')
# Request from the alternate cache with a new prefix and a custom timeout
response = other_with_timeout_view(request, '10')
self.assertEqual(response.content, b'Hello World 10')
# But if we wait a couple of seconds...
time.sleep(2)
@ -1689,18 +1677,6 @@ class CacheMiddlewareTest(TestCase):
response = other_with_prefix_view(request, '16')
self.assertEqual(response.content, b'Hello World 16')
# ... but a view with a custom timeout will still hit
response = other_with_timeout_view(request, '17')
self.assertEqual(response.content, b'Hello World 10')
# And if we wait a few more seconds
time.sleep(2)
# the custom timeout cache will miss
response = other_with_timeout_view(request, '18')
self.assertEqual(response.content, b'Hello World 18')
@override_settings(
CACHE_MIDDLEWARE_KEY_PREFIX='settingsprefix',
CACHE_MIDDLEWARE_SECONDS=1,

View File

@ -1,4 +1,3 @@
import warnings
from functools import wraps
from django.contrib.admin.views.decorators import staff_member_required
@ -116,22 +115,6 @@ class DecoratorsTest(TestCase):
my_view_cached2 = cache_page(123, key_prefix="test")(my_view)
self.assertEqual(my_view_cached2(HttpRequest()), "response")
def test_cache_page_old_style(self):
"""
Test that we can call cache_page the old way
"""
def my_view(request):
return "response"
with warnings.catch_warnings(record=True):
my_view_cached = cache_page(my_view, 123)
self.assertEqual(my_view_cached(HttpRequest()), "response")
my_view_cached2 = cache_page(my_view, 123, key_prefix="test")
self.assertEqual(my_view_cached2(HttpRequest()), "response")
my_view_cached3 = cache_page(my_view)
self.assertEqual(my_view_cached3(HttpRequest()), "response")
my_view_cached4 = cache_page()(my_view)
self.assertEqual(my_view_cached4(HttpRequest()), "response")
def test_require_safe_accepts_only_safe_methods(self):
"""
Test for the require_safe decorator.