From 6de81d65f443a01961c23139ca5d7653ef012d35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vlastimil=20Z=C3=ADma?= Date: Mon, 25 Mar 2013 15:45:24 +0100 Subject: [PATCH] Fixed #14825 -- LocaleMiddleware keeps language * LocaleMiddleware stores language into session if it is not present there. --- django/middleware/locale.py | 4 ++++ docs/releases/1.6.txt | 4 ++++ tests/i18n/tests.py | 32 ++++++++++++++++++++++++++++++++ tests/i18n/urls.py | 3 ++- 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/django/middleware/locale.py b/django/middleware/locale.py index 25fbbaccdd..2e2204c859 100644 --- a/django/middleware/locale.py +++ b/django/middleware/locale.py @@ -53,6 +53,10 @@ class LocaleMiddleware(object): request.get_host(), language, request.get_full_path()) 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() and language_from_path): patch_vary_headers(response, ('Accept-Language',)) diff --git a/docs/releases/1.6.txt b/docs/releases/1.6.txt index 8d48381c06..35488b4110 100644 --- a/docs/releases/1.6.txt +++ b/docs/releases/1.6.txt @@ -266,6 +266,10 @@ Minor features ``False`` allows the field to reference proxy models. The default is ``True`` to retain the old behavior. +* The middleware :class:`~django.middleware.locale.LocaleMiddleware` now + stores active language in session if it is not present there. This + prevents loss of language settings after session flush, e.g. logout. + Backwards incompatible changes in 1.6 ===================================== diff --git a/tests/i18n/tests.py b/tests/i18n/tests.py index 777b9c4f75..019ed88cdb 100644 --- a/tests/i18n/tests.py +++ b/tests/i18n/tests.py @@ -1152,6 +1152,38 @@ class LocaleMiddlewareTests(TransRealMixin, TestCase): response = self.client.get('/en/streaming/') self.assertContains(response, "Yes/No") + @override_settings( + MIDDLEWARE_CLASSES=( + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.locale.LocaleMiddleware', + '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() + 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( USE_I18N=True, LANGUAGES=( diff --git a/tests/i18n/urls.py b/tests/i18n/urls.py index c118265dab..c7d01897f5 100644 --- a/tests/i18n/urls.py +++ b/tests/i18n/urls.py @@ -1,9 +1,10 @@ from __future__ import unicode_literals from django.conf.urls.i18n import i18n_patterns -from django.http import StreamingHttpResponse +from django.http import HttpResponse, StreamingHttpResponse from django.utils.translation import ugettext_lazy as _ urlpatterns = i18n_patterns('', + (r'^simple/$', lambda r: HttpResponse()), (r'^streaming/$', lambda r: StreamingHttpResponse([_("Yes"), "/", _("No")])), )