# 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):
|
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 | # 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)): |
74 | 86 | 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 | 87 | if request.user.is_authenticated(): |
76 | 88 | # Don't cache user-variable requests from authenticated users. |
diff --git a/tests/regressiontests/cache/tests.py b/tests/regressiontests/cache/tests.py
a
|
b
|
import unittest
|
10 | 10 | import warnings |
11 | 11 | |
12 | 12 | from django.conf import settings |
| 13 | from django.contrib.auth.models import User |
13 | 14 | from django.core import management |
14 | 15 | from django.core.cache import get_cache |
15 | 16 | from django.core.cache.backends.base import CacheKeyWarning |
… |
… |
class CacheMiddlewareAnonymousOnlyTests(
|
670 | 671 | response = self.client.get('/') |
671 | 672 | self.failIf('Cookie' in response.get('Vary', '')) |
672 | 673 | |
| 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) |
673 | 693 | |
674 | 694 | if __name__ == '__main__': |
675 | 695 | unittest.main() |
diff --git a/tests/regressiontests/cache/urls.py b/tests/regressiontests/cache/urls.py
a
|
b
|
from django.conf.urls.defaults import pa
|
2 | 2 | |
3 | 3 | urlpatterns = patterns('regressiontests.cache.views', |
4 | 4 | (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'), |
5 | 9 | ) |
diff --git a/tests/regressiontests/cache/views.py b/tests/regressiontests/cache/views.py
a
|
b
|
|
1 | 1 | from django.http import HttpResponse |
| 2 | from django.views.decorators.cache import cache_page |
| 3 | from django.views.decorators.vary import vary_on_cookie |
2 | 4 | |
3 | 5 | def home(request): |
4 | 6 | return HttpResponse('Hello World!') |
| 7 | |
| 8 | def user(request): |
| 9 | return HttpResponse(request.user.username) |
| 10 | |
| 11 | user_cache_page = cache_page(60 * 15)(user) |
| 12 | user_vary_on_cookie_cache_page = vary_on_cookie(user_cache_page) |
| 13 | user_cache_page_vary_on_cookie = cache_page(60 * 15)(vary_on_cookie(user)) |