From 9922ed46e2e81845006792310f3f1a3bf0d7b2a6 Mon Sep 17 00:00:00 2001 From: Ludwik Trammer Date: Sat, 23 Nov 2013 18:27:23 +0100 Subject: [PATCH] Fixed #21473 -- Limited language preservation to logout Current language is no longer saved to session by LocaleMiddleware on every response (the behavior introduced in #14825). Instead language stored in session is reintroduced into new session after logout. Forward port of c558a43fd6 to master. --- django/contrib/auth/__init__.py | 8 ++++ django/contrib/auth/tests/test_views.py | 13 +++++++ django/middleware/locale.py | 10 ----- tests/i18n/tests.py | 49 +++---------------------- 4 files changed, 26 insertions(+), 54 deletions(-) diff --git a/django/contrib/auth/__init__.py b/django/contrib/auth/__init__.py index 4ef5c0b2cd..83aa445bac 100644 --- a/django/contrib/auth/__init__.py +++ b/django/contrib/auth/__init__.py @@ -105,7 +105,15 @@ def logout(request): user = None user_logged_out.send(sender=user.__class__, request=request, user=user) + # remember language choice saved to session + # for backwards compatibility django_language is also checked (remove in 1.8) + language = request.session.get('_language', request.session.get('django_language')) + request.session.flush() + + if language is not None: + request.session['_language'] = language + if hasattr(request, 'user'): from django.contrib.auth.models import AnonymousUser request.user = AnonymousUser() diff --git a/django/contrib/auth/tests/test_views.py b/django/contrib/auth/tests/test_views.py index 060f0086ad..4a1783b14d 100644 --- a/django/contrib/auth/tests/test_views.py +++ b/django/contrib/auth/tests/test_views.py @@ -1,3 +1,4 @@ +from importlib import import_module import itertools import os import re @@ -710,6 +711,18 @@ class LogoutTest(AuthViewsTestCase): "%s should be allowed" % good_url) self.confirm_logged_out() + def test_logout_preserve_language(self): + """Check that language stored in session is preserved after logout""" + # Create a new session with language + engine = import_module(settings.SESSION_ENGINE) + session = engine.SessionStore() + session['_language'] = 'pl' + session.save() + self.client.cookies[settings.SESSION_COOKIE_NAME] = session.session_key + + self.client.get('/logout/') + self.assertEqual(self.client.session['_language'], 'pl') + @skipIfCustomUser @override_settings( diff --git a/django/middleware/locale.py b/django/middleware/locale.py index 87f904d7dd..1f64387d78 100644 --- a/django/middleware/locale.py +++ b/django/middleware/locale.py @@ -55,16 +55,6 @@ class LocaleMiddleware(object): request.get_full_path()) return self.response_redirect_class(language_url) - # Store language back into session if it is not present - if hasattr(request, 'session') and '_language' not in request.session: - # Backwards compatibility check on django_language (remove in 1.8); - # revert to: `request.session.setdefault('_language', language)`. - if 'django_language' in request.session: - request.session['_language'] = request.session['django_language'] - del request.session['django_language'] - else: - request.session['_language'] = language - if not (self.is_language_prefix_patterns_used() and language_from_path): patch_vary_headers(response, ('Accept-Language',)) diff --git a/tests/i18n/tests.py b/tests/i18n/tests.py index 471f434467..77b7d18455 100644 --- a/tests/i18n/tests.py +++ b/tests/i18n/tests.py @@ -1247,51 +1247,12 @@ class LocaleMiddlewareTests(TransRealMixin, TestCase): 'django.middleware.common.CommonMiddleware', ), ) - def test_session_language(self): - """ - Check that language is stored in session if missing. - """ - # Create an empty session - engine = import_module(settings.SESSION_ENGINE) - session = engine.SessionStore() - session.save() - self.client.cookies[settings.SESSION_COOKIE_NAME] = session.session_key - - # Clear the session data before request - session.save() - self.client.get('/en/simple/') - self.assertEqual(self.client.session['_language'], 'en') - - # Clear the session data before request - session.save() + def test_language_not_saved_to_session(self): + """Checks that current language is not automatically saved to + session on every request.""" + # Regression test for #21473 self.client.get('/fr/simple/') - self.assertEqual(self.client.session['_language'], 'fr') - - # Check that language is not changed in session - self.client.get('/en/simple/') - self.assertEqual(self.client.session['_language'], 'fr') - - @override_settings( - MIDDLEWARE_CLASSES=( - 'django.contrib.sessions.middleware.SessionMiddleware', - 'django.middleware.locale.LocaleMiddleware', - 'django.middleware.common.CommonMiddleware', - ), - ) - def test_backwards_session_language(self): - """ - Check that language is stored in session if missing. - """ - # Create session with old language key name - engine = import_module(settings.SESSION_ENGINE) - session = engine.SessionStore() - session['django_language'] = 'en' - session.save() - self.client.cookies[settings.SESSION_COOKIE_NAME] = session.session_key - - # request other language; should default to old language key value - self.client.get('/fr/simple/') - self.assertEqual(self.client.session['_language'], 'en') + self.assertNotIn('_language', self.client.session) @override_settings(