Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #12019 - backwards compatibility issues with cache_page decorator.

Thanks to rokclimb15 for the report, and j4mie/rokclimb15 for the patches.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@13864 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 9d6eeeec39f834ad480778ba7aa4ece01c6f7aff 1 parent 11aee35
Luke Plant spookylukey authored
13 django/views/decorators/cache.py
View
@@ -33,6 +33,10 @@ def cache_page(*args, **kwargs):
# 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.
@@ -45,9 +49,14 @@ def cache_page(*args, **kwargs):
elif callable(args[1]):
return decorator_from_middleware_with_args(CacheMiddleware)(cache_timeout=args[0], key_prefix=key_prefix)(args[1])
else:
- assert False, "cache_page must be passed either a single argument (timeout) or a view function and a timeout"
+ assert False, "cache_page must be passed a view function if called with two arguments"
+ elif len(args) == 1:
+ if callable(args[0]):
+ return decorator_from_middleware_with_args(CacheMiddleware)(key_prefix=key_prefix)(args[0])
+ else:
+ return decorator_from_middleware_with_args(CacheMiddleware)(cache_timeout=args[0], key_prefix=key_prefix)
else:
- return decorator_from_middleware_with_args(CacheMiddleware)(cache_timeout=args[0], key_prefix=key_prefix)
+ return decorator_from_middleware_with_args(CacheMiddleware)(key_prefix=key_prefix)
def cache_control(**kwargs):
4 tests/regressiontests/decorators/tests.py
View
@@ -112,6 +112,10 @@ def my_view(request):
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")
# For testing method_decorator, a decorator that assumes a single argument.
Please sign in to comment.
Something went wrong with that request. Please try again.