Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.2.X] Fixed #12019 - backwards compatibility issues with cache_page…

… decorator.

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

Backport of [13864] from trunk

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@13865 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 804719ff23a8fc80b871900663b99085021d9b1b 1 parent 80b9233
Luke Plant authored September 21, 2010
13  django/views/decorators/cache.py
@@ -33,6 +33,10 @@ def cache_page(*args, **kwargs):
33 33
     #   my_view = cache_page(123, key_prefix="foo")(my_view)
34 34
     # and possibly this way (?):
35 35
     #   my_view = cache_page(123, my_view)
  36
+    # and also this way:
  37
+    #   my_view = cache_page(my_view)
  38
+    # and also this way:
  39
+    #   my_view = cache_page()(my_view)
36 40
 
37 41
     # We also add some asserts to give better error messages in case people are
38 42
     # using other ways to call cache_page that no longer work.
@@ -45,9 +49,14 @@ def cache_page(*args, **kwargs):
45 49
         elif callable(args[1]):
46 50
             return decorator_from_middleware_with_args(CacheMiddleware)(cache_timeout=args[0], key_prefix=key_prefix)(args[1])
47 51
         else:
48  
-            assert False, "cache_page must be passed either a single argument (timeout) or a view function and a timeout"
  52
+            assert False, "cache_page must be passed a view function if called with two arguments"
  53
+    elif len(args) == 1:
  54
+        if callable(args[0]):
  55
+            return decorator_from_middleware_with_args(CacheMiddleware)(key_prefix=key_prefix)(args[0])
  56
+        else:
  57
+            return decorator_from_middleware_with_args(CacheMiddleware)(cache_timeout=args[0], key_prefix=key_prefix)
49 58
     else:
50  
-        return decorator_from_middleware_with_args(CacheMiddleware)(cache_timeout=args[0], key_prefix=key_prefix)
  59
+        return decorator_from_middleware_with_args(CacheMiddleware)(key_prefix=key_prefix)
51 60
 
52 61
 
53 62
 def cache_control(**kwargs):
4  tests/regressiontests/decorators/tests.py
@@ -112,6 +112,10 @@ def my_view(request):
112 112
         self.assertEqual(my_view_cached(HttpRequest()), "response")
113 113
         my_view_cached2 = cache_page(my_view, 123, key_prefix="test")
114 114
         self.assertEqual(my_view_cached2(HttpRequest()), "response")
  115
+        my_view_cached3 = cache_page(my_view)
  116
+        self.assertEqual(my_view_cached3(HttpRequest()), "response")
  117
+        my_view_cached4 = cache_page()(my_view)
  118
+        self.assertEqual(my_view_cached4(HttpRequest()), "response")
115 119
 
116 120
 
117 121
 # For testing method_decorator, a decorator that assumes a single argument.

0 notes on commit 804719f

Please sign in to comment.
Something went wrong with that request. Please try again.