Ticket #15260: cache-page-fix.patch

File cache-page-fix.patch, 4.7 KB (added by brodie, 4 years ago)
  • django/middleware/cache.py

    # HG changeset patch
    # User Brodie Rao <brodie@bitheap.org>
    # Date 1297307788 28800
    # Branch releases/1.2.X
    # Node ID a16a219713d315962c56ddf13ee4f03fad5decf9
    # Parent  61c0665bc6c15ed9db42a7ca5da2678efa551934
    Fixed @cache_page not properly checking if the current request is by a logged in user
    
    diff --git a/django/middleware/cache.py b/django/middleware/cache.py
    a b class UpdateCacheMiddleware(object): 
    6767        self.key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX
    6868        self.cache_anonymous_only = getattr(settings, 'CACHE_MIDDLEWARE_ANONYMOUS_ONLY', False)
    6969
     70    def _session_accessed(self, request):
     71        try:
     72            return request.session.accessed
     73        except AttributeError:
     74            return False
     75
    7076    def _should_update_cache(self, request, response):
    7177        if not hasattr(request, '_cache_update_cache') or not request._cache_update_cache:
    7278            return False
    73         if self.cache_anonymous_only and has_vary_header(response, 'Cookie'):
     79        # In most situations, we shouldn't need to directly check if
     80        # the session has been accessed; checking for Vary: Cookie is
     81        # enough. But when the @cache_page decorator is used, this
     82        # check is done before the Vary header is set, so we need to
     83        # check request.session.accessed ourselves.
     84        if self.cache_anonymous_only and (has_vary_header(response, 'Cookie')
     85                                          or self._session_accessed(request)):
    7486            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."
    7587            if request.user.is_authenticated():
    7688                # Don't cache user-variable requests from authenticated users.
  • tests/regressiontests/cache/tests.py

    diff --git a/tests/regressiontests/cache/tests.py b/tests/regressiontests/cache/tests.py
    a b import unittest 
    1010import warnings
    1111
    1212from django.conf import settings
     13from django.contrib.auth.models import User
    1314from django.core import management
    1415from django.core.cache import get_cache
    1516from django.core.cache.backends.base import CacheKeyWarning
    class CacheMiddlewareAnonymousOnlyTests( 
    670671        response = self.client.get('/')
    671672        self.failIf('Cookie' in response.get('Vary', ''))
    672673
     674    def test_cache_middleware_anonymous_only_while_logged_in(self):
     675        settings.CACHE_MIDDLEWARE_ANONYMOUS_ONLY = True
     676
     677        response = self.client.get('/')
     678        self.assertEqual(response['Cache-Control'], 'max-age=600')
     679
     680        User.objects.create_user(username='user', email='foo@bar.com',
     681                                 password='password')
     682        login = self.client.login(username='user', password='password')
     683        self.assertTrue(login)
     684
     685        response = self.client.get('/user')
     686        self.failIf('Cache-Control' in response)
     687        response = self.client.get('/user-cache-page')
     688        self.failIf('Cache-Control' in response)
     689        response = self.client.get('/user-vary-on-cookie-cache-page')
     690        self.failIf('Cache-Control' in response)
     691        response = self.client.get('/user-cache-page-vary-on-cookie')
     692        self.failIf('Cache-Control' in response)
    673693
    674694if __name__ == '__main__':
    675695    unittest.main()
  • tests/regressiontests/cache/urls.py

    diff --git a/tests/regressiontests/cache/urls.py b/tests/regressiontests/cache/urls.py
    a b from django.conf.urls.defaults import pa 
    22
    33urlpatterns = patterns('regressiontests.cache.views',
    44    (r'^$', 'home'),
     5    (r'^user$', 'user'),
     6    (r'^user-cache-page$', 'user_cache_page'),
     7    (r'^user-vary-on-cookie-cache-page$', 'user_vary_on_cookie_cache_page'),
     8    (r'^user-cache-page-vary-on-cookie$', 'user_cache_page_vary_on_cookie'),
    59)
  • tests/regressiontests/cache/views.py

    diff --git a/tests/regressiontests/cache/views.py b/tests/regressiontests/cache/views.py
    a b  
    11from django.http import HttpResponse
     2from django.views.decorators.cache import cache_page
     3from django.views.decorators.vary import vary_on_cookie
    24
    35def home(request):
    46    return HttpResponse('Hello World!')
     7
     8def user(request):
     9    return HttpResponse(request.user.username)
     10
     11user_cache_page = cache_page(60 * 15)(user)
     12user_vary_on_cookie_cache_page = vary_on_cookie(user_cache_page)
     13user_cache_page_vary_on_cookie = cache_page(60 * 15)(vary_on_cookie(user))
Back to Top