Ticket #13283: 13283_r12936.diff

File 13283_r12936.diff, 3.6 KB (added by carljm, 5 years ago)

fix via patching request.session.accessed

  • django/middleware/cache.py

    diff --git a/django/middleware/cache.py b/django/middleware/cache.py
    a b  
    113113        version if available.
    114114        """
    115115        if self.cache_anonymous_only:
     116            # do all of our request.user access here, so we can reset
     117            # request.session.accessed afterwards (to avoid
     118            # SessionMiddleware setting Vary: Cookie)
     119            assert hasattr(request, 'session'), "The Django cache middleware with CACHE_MIDDLEWARE_ANONYMOUS_ONLY=True requires both session middleware and authentication middleware to be installed. Edit your MIDDLEWARE_CLASSES setting to insert 'django.contrib.sessions.middleware.SessionMiddleware' and 'django.contrib.auth.middleware.AuthenticationMiddleware' before the CacheMiddleware."
     120            session_accessed = request.session.accessed
    116121            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."
     122            user_is_authenticated = request.user.is_authenticated()
     123            request.session.accessed = session_accessed
    117124
    118125        if not request.method in ('GET', 'HEAD') or request.GET:
    119126            request._cache_update_cache = False
    120127            return None # Don't bother checking the cache.
    121128
    122         if self.cache_anonymous_only and request.user.is_authenticated():
     129        if self.cache_anonymous_only and user_is_authenticated:
    123130            request._cache_update_cache = False
    124131            return None # Don't cache requests from authenticated users.
    125132
  • tests/regressiontests/cache/tests.py

    diff --git a/tests/regressiontests/cache/tests.py b/tests/regressiontests/cache/tests.py
    a b  
    1818from django.utils import translation
    1919from django.utils.cache import patch_vary_headers, get_cache_key, learn_cache_key
    2020from django.utils.hashcompat import md5_constructor
     21from django.test import TestCase
    2122from regressiontests.cache.models import Poll, expensive_calculation
    2223
    2324# functions/classes for complex data type tests
     
    568569        get_cache_data = FetchFromCacheMiddleware().process_request(request)
    569570        self.assertEqual(get_cache_data.content, es_message)
    570571
     572class CacheMiddlewareTests(TestCase):
     573    urls = 'regressiontests.cache.test_urls'
     574   
     575    def setUp(self):
     576        self._orig_cache_middleware_anonymous_only = getattr(settings,
     577                                                             'CACHE_MIDDLEWARE_ANONYMOUS_ONLY',
     578                                                             False)
     579        self._orig_middleware_classes = settings.MIDDLEWARE_CLASSES
     580
     581        settings.MIDDLEWARE_CLASSES = list(settings.MIDDLEWARE_CLASSES)
     582        settings.MIDDLEWARE_CLASSES.insert(0, 'django.middleware.cache.UpdateCacheMiddleware')
     583        settings.MIDDLEWARE_CLASSES += ['django.middleware.cache.FetchFromCacheMiddleware']
     584
     585    def tearDown(self):
     586        settings.CACHE_MIDDLEWARE_ANONYMOUS_ONLY = self._orig_cache_middleware_anonymous_only
     587        settings.MIDDLEWARE_CLASSES = self._orig_middleware_classes
     588   
     589    def test_cache_middleware_anonymous_only_does_not_cause_vary_cookie(self):
     590        settings.CACHE_MIDDLEWARE_ANONYMOUS_ONLY = True
     591
     592        response = self.client.get('/')
     593
     594        self.failIf('Cookie' in response.get('Vary', ''))
     595       
     596       
     597       
    571598if __name__ == '__main__':
    572599    unittest.main()
Back to Top