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.
This commit is contained in:
Ludwik Trammer 2013-11-23 18:27:23 +01:00 committed by Claude Paroz
parent 9c5f59f489
commit 9922ed46e2
4 changed files with 26 additions and 54 deletions

View File

@ -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()

View File

@ -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(

View File

@ -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',))

View File

@ -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(