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 #15260 -- Ensured that CACHE_MIDDLEWARE_ANONYMOUS_ONLY …

…is effective with the cache_page decorator, not only the middleware. Thanks to brodie for report and draft patch.

Backport of r15559 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@15560 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit aae16079efbb03da3dc0b66c3e62ce022d293e63 1 parent 9ec9d2e
Carl Meyer authored February 17, 2011
13  django/middleware/cache.py
@@ -50,7 +50,7 @@
50 50
 
51 51
 from django.conf import settings
52 52
 from django.core.cache import cache
53  
-from django.utils.cache import get_cache_key, learn_cache_key, patch_response_headers, get_max_age, has_vary_header
  53
+from django.utils.cache import get_cache_key, learn_cache_key, patch_response_headers, get_max_age
54 54
 
55 55
 
56 56
 class UpdateCacheMiddleware(object):
@@ -67,10 +67,19 @@ def __init__(self):
67 67
         self.key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX
68 68
         self.cache_anonymous_only = getattr(settings, 'CACHE_MIDDLEWARE_ANONYMOUS_ONLY', False)
69 69
 
  70
+    def _session_accessed(self, request):
  71
+        try:
  72
+            return request.session.accessed
  73
+        except AttributeError:
  74
+            return False
  75
+
70 76
     def _should_update_cache(self, request, response):
71 77
         if not hasattr(request, '_cache_update_cache') or not request._cache_update_cache:
72 78
             return False
73  
-        if self.cache_anonymous_only and has_vary_header(response, 'Cookie'):
  79
+        # If the session has not been accessed otherwise, we don't want to
  80
+        # cause it to be accessed here. If it hasn't been accessed, then the
  81
+        # user's logged-in status has not affected the response anyway.
  82
+        if self.cache_anonymous_only and self._session_accessed(request):
74 83
             assert hasattr(request, 'user'), "The Django cache middleware with CACHE_MIDDLEWARE_ANONYMOUS_ONLY=True requires authentication middleware to be installed. Edit your MIDDLEWARE_CLASSES setting to insert 'django.contrib.auth.middleware.AuthenticationMiddleware' before the CacheMiddleware."
75 84
             if request.user.is_authenticated():
76 85
                 # Don't cache user-variable requests from authenticated users.
34  tests/regressiontests/cache/tests.py
@@ -19,6 +19,7 @@
19 19
 from django.utils import translation
20 20
 from django.utils.cache import patch_vary_headers, get_cache_key, learn_cache_key
21 21
 from django.utils.hashcompat import md5_constructor
  22
+from django.views.decorators.cache import cache_page
22 23
 from regressiontests.cache.models import Poll, expensive_calculation
23 24
 
24 25
 # functions/classes for complex data type tests
@@ -653,15 +654,18 @@ def hello_world_view(request, value):
653 654
 class CacheMiddlewareTest(unittest.TestCase):
654 655
 
655 656
     def setUp(self):
  657
+        from django.middleware import cache as cache_middleware_module
  658
+
  659
+        self.cache_middleware_module = cache_middleware_module
  660
+        self.orig_cache = self.cache_middleware_module.cache
656 661
         self.orig_cache_middleware_anonymous_only = getattr(settings, 'CACHE_MIDDLEWARE_ANONYMOUS_ONLY', False)
657  
-        self._orig_cache_backend = settings.CACHE_BACKEND
658 662
 
  663
+        cache_middleware_module.cache = get_cache("locmem://")
659 664
         settings.CACHE_MIDDLEWARE_ANONYMOUS_ONLY = False
660  
-        settings.CACHE_BACKEND = "locmem://"
661 665
 
662 666
     def tearDown(self):
  667
+        self.cache_middleware_module.cache = self.orig_cache
663 668
         settings.CACHE_MIDDLEWARE_ANONYMOUS_ONLY = self.orig_cache_middleware_anonymous_only
664  
-        settings.CACHE_BACKEND = self._orig_cache_backend
665 669
 
666 670
     def test_cache_middleware_anonymous_only_wont_cause_session_access(self):
667 671
         """ The cache middleware shouldn't cause a session access due to
@@ -694,6 +698,30 @@ def test_cache_middleware_anonymous_only_wont_cause_session_access(self):
694 698
 
695 699
         self.assertEqual(request.session.accessed, False)
696 700
 
  701
+    def test_cache_middleware_anonymous_only_with_cache_page(self):
  702
+        """CACHE_MIDDLEWARE_ANONYMOUS_ONLY should still be effective when used
  703
+        with the cache_page decorator: the response to a request from an
  704
+        authenticated user should not be cached."""
  705
+        settings.CACHE_MIDDLEWARE_ANONYMOUS_ONLY = True
  706
+
  707
+        request = HttpRequest()
  708
+        request.path = '/view/'
  709
+        request.method = 'GET'
  710
+
  711
+        class MockAuthenticatedUser(object):
  712
+            def is_authenticated(self):
  713
+                return True
  714
+
  715
+        class MockAccessedSession(object):
  716
+            accessed = True
  717
+
  718
+        request.user = MockAuthenticatedUser()
  719
+        request.session = MockAccessedSession()
  720
+
  721
+        response = cache_page(hello_world_view)(request, '1')
  722
+
  723
+        self.assertFalse("Cache-Control" in response)
  724
+
697 725
 
698 726
 if __name__ == '__main__':
699 727
     unittest.main()
5  tests/regressiontests/cache/urls.py
... ...
@@ -1,5 +0,0 @@
1  
-from django.conf.urls.defaults import patterns
2  
-
3  
-urlpatterns = patterns('regressiontests.cache.views',
4  
-    (r'^$', 'home'),
5  
-)
4  tests/regressiontests/cache/views.py
... ...
@@ -1,4 +0,0 @@
1  
-from django.http import HttpResponse
2  
-
3  
-def home(request):
4  
-    return HttpResponse('Hello World!')

0 notes on commit aae1607

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