mirror of https://github.com/django/django.git
[1.6.x] 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.
This commit is contained in:
parent
d32637d81b
commit
c558a43fd6
|
@ -101,7 +101,14 @@ def logout(request):
|
||||||
user = None
|
user = None
|
||||||
user_logged_out.send(sender=user.__class__, request=request, user=user)
|
user_logged_out.send(sender=user.__class__, request=request, user=user)
|
||||||
|
|
||||||
|
# remember language choice saved to session
|
||||||
|
language = request.session.get('django_language')
|
||||||
|
|
||||||
request.session.flush()
|
request.session.flush()
|
||||||
|
|
||||||
|
if language is not None:
|
||||||
|
request.session['django_language'] = language
|
||||||
|
|
||||||
if hasattr(request, 'user'):
|
if hasattr(request, 'user'):
|
||||||
from django.contrib.auth.models import AnonymousUser
|
from django.contrib.auth.models import AnonymousUser
|
||||||
request.user = AnonymousUser()
|
request.user = AnonymousUser()
|
||||||
|
|
|
@ -12,6 +12,7 @@ from django.http import QueryDict, HttpRequest
|
||||||
from django.utils.encoding import force_text
|
from django.utils.encoding import force_text
|
||||||
from django.utils.http import int_to_base36, urlsafe_base64_decode, urlquote
|
from django.utils.http import int_to_base36, urlsafe_base64_decode, urlquote
|
||||||
from django.utils.six.moves.urllib.parse import urlparse, ParseResult
|
from django.utils.six.moves.urllib.parse import urlparse, ParseResult
|
||||||
|
from django.utils.importlib import import_module
|
||||||
from django.utils._os import upath
|
from django.utils._os import upath
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from django.test.utils import override_settings, patch_logger
|
from django.test.utils import override_settings, patch_logger
|
||||||
|
@ -696,6 +697,19 @@ class LogoutTest(AuthViewsTestCase):
|
||||||
"%s should be allowed" % good_url)
|
"%s should be allowed" % good_url)
|
||||||
self.confirm_logged_out()
|
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['django_language'] = 'pl'
|
||||||
|
session.save()
|
||||||
|
self.client.cookies[settings.SESSION_COOKIE_NAME] = session.session_key
|
||||||
|
|
||||||
|
self.client.get('/logout/')
|
||||||
|
self.assertEqual(self.client.session['django_language'], 'pl')
|
||||||
|
|
||||||
|
|
||||||
@skipIfCustomUser
|
@skipIfCustomUser
|
||||||
@override_settings(
|
@override_settings(
|
||||||
PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',),
|
PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',),
|
||||||
|
|
|
@ -53,10 +53,6 @@ class LocaleMiddleware(object):
|
||||||
request.get_host(), language, request.get_full_path())
|
request.get_host(), language, request.get_full_path())
|
||||||
return HttpResponseRedirect(language_url)
|
return HttpResponseRedirect(language_url)
|
||||||
|
|
||||||
# Store language back into session if it is not present
|
|
||||||
if hasattr(request, 'session'):
|
|
||||||
request.session.setdefault('django_language', language)
|
|
||||||
|
|
||||||
if not (self.is_language_prefix_patterns_used()
|
if not (self.is_language_prefix_patterns_used()
|
||||||
and language_from_path):
|
and language_from_path):
|
||||||
patch_vary_headers(response, ('Accept-Language',))
|
patch_vary_headers(response, ('Accept-Language',))
|
||||||
|
|
|
@ -1191,29 +1191,12 @@ class LocaleMiddlewareTests(TransRealMixin, TestCase):
|
||||||
'django.middleware.common.CommonMiddleware',
|
'django.middleware.common.CommonMiddleware',
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
def test_session_language(self):
|
def test_language_not_saved_to_session(self):
|
||||||
"""
|
"""Checks that current language is not automatically saved to
|
||||||
Check that language is stored in session if missing.
|
session on every request."""
|
||||||
"""
|
# Regression test for #21473
|
||||||
# Create an empty session
|
self.client.get('/fr/simple/')
|
||||||
engine = import_module(settings.SESSION_ENGINE)
|
self.assertNotIn('django_language', self.client.session)
|
||||||
session = engine.SessionStore()
|
|
||||||
session.save()
|
|
||||||
self.client.cookies[settings.SESSION_COOKIE_NAME] = session.session_key
|
|
||||||
|
|
||||||
# Clear the session data before request
|
|
||||||
session.save()
|
|
||||||
response = self.client.get('/en/simple/')
|
|
||||||
self.assertEqual(self.client.session['django_language'], 'en')
|
|
||||||
|
|
||||||
# Clear the session data before request
|
|
||||||
session.save()
|
|
||||||
response = self.client.get('/fr/simple/')
|
|
||||||
self.assertEqual(self.client.session['django_language'], 'fr')
|
|
||||||
|
|
||||||
# Check that language is not changed in session
|
|
||||||
response = self.client.get('/en/simple/')
|
|
||||||
self.assertEqual(self.client.session['django_language'], 'fr')
|
|
||||||
|
|
||||||
|
|
||||||
@override_settings(
|
@override_settings(
|
||||||
|
|
Loading…
Reference in New Issue