Ticket #21473: 21473-master.patch
File 21473-master.patch, 5.6 KB (added by , 11 years ago) |
---|
-
django/contrib/auth/__init__.py
commit 9922ed46e2e81845006792310f3f1a3bf0d7b2a6 Author: Ludwik Trammer <ludwik@gmail.com> Date: Sat Nov 23 18:27:23 2013 +0100 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. diff --git a/django/contrib/auth/__init__.py b/django/contrib/auth/__init__.py index 4ef5c0b..83aa445 100644
a b def logout(request): 105 105 user = None 106 106 user_logged_out.send(sender=user.__class__, request=request, user=user) 107 107 108 # remember language choice saved to session 109 # for backwards compatibility django_language is also checked (remove in 1.8) 110 language = request.session.get('_language', request.session.get('django_language')) 111 108 112 request.session.flush() 113 114 if language is not None: 115 request.session['_language'] = language 116 109 117 if hasattr(request, 'user'): 110 118 from django.contrib.auth.models import AnonymousUser 111 119 request.user = AnonymousUser() -
django/contrib/auth/tests/test_views.py
diff --git a/django/contrib/auth/tests/test_views.py b/django/contrib/auth/tests/test_views.py index 060f008..4a1783b 100644
a b 1 from importlib import import_module 1 2 import itertools 2 3 import os 3 4 import re … … class LogoutTest(AuthViewsTestCase): 710 711 "%s should be allowed" % good_url) 711 712 self.confirm_logged_out() 712 713 714 def test_logout_preserve_language(self): 715 """Check that language stored in session is preserved after logout""" 716 # Create a new session with language 717 engine = import_module(settings.SESSION_ENGINE) 718 session = engine.SessionStore() 719 session['_language'] = 'pl' 720 session.save() 721 self.client.cookies[settings.SESSION_COOKIE_NAME] = session.session_key 722 723 self.client.get('/logout/') 724 self.assertEqual(self.client.session['_language'], 'pl') 725 713 726 714 727 @skipIfCustomUser 715 728 @override_settings( -
django/middleware/locale.py
diff --git a/django/middleware/locale.py b/django/middleware/locale.py index 87f904d..1f64387 100644
a b class LocaleMiddleware(object): 55 55 request.get_full_path()) 56 56 return self.response_redirect_class(language_url) 57 57 58 # Store language back into session if it is not present59 if hasattr(request, 'session') and '_language' not in request.session:60 # Backwards compatibility check on django_language (remove in 1.8);61 # revert to: `request.session.setdefault('_language', language)`.62 if 'django_language' in request.session:63 request.session['_language'] = request.session['django_language']64 del request.session['django_language']65 else:66 request.session['_language'] = language67 68 58 if not (self.is_language_prefix_patterns_used() 69 59 and language_from_path): 70 60 patch_vary_headers(response, ('Accept-Language',)) -
tests/i18n/tests.py
diff --git a/tests/i18n/tests.py b/tests/i18n/tests.py index 471f434..77b7d18 100644
a b class LocaleMiddlewareTests(TransRealMixin, TestCase): 1247 1247 'django.middleware.common.CommonMiddleware', 1248 1248 ), 1249 1249 ) 1250 def test_session_language(self): 1251 """ 1252 Check that language is stored in session if missing. 1253 """ 1254 # Create an empty session 1255 engine = import_module(settings.SESSION_ENGINE) 1256 session = engine.SessionStore() 1257 session.save() 1258 self.client.cookies[settings.SESSION_COOKIE_NAME] = session.session_key 1259 1260 # Clear the session data before request 1261 session.save() 1262 self.client.get('/en/simple/') 1263 self.assertEqual(self.client.session['_language'], 'en') 1264 1265 # Clear the session data before request 1266 session.save() 1267 self.client.get('/fr/simple/') 1268 self.assertEqual(self.client.session['_language'], 'fr') 1269 1270 # Check that language is not changed in session 1271 self.client.get('/en/simple/') 1272 self.assertEqual(self.client.session['_language'], 'fr') 1273 1274 @override_settings( 1275 MIDDLEWARE_CLASSES=( 1276 'django.contrib.sessions.middleware.SessionMiddleware', 1277 'django.middleware.locale.LocaleMiddleware', 1278 'django.middleware.common.CommonMiddleware', 1279 ), 1280 ) 1281 def test_backwards_session_language(self): 1282 """ 1283 Check that language is stored in session if missing. 1284 """ 1285 # Create session with old language key name 1286 engine = import_module(settings.SESSION_ENGINE) 1287 session = engine.SessionStore() 1288 session['django_language'] = 'en' 1289 session.save() 1290 self.client.cookies[settings.SESSION_COOKIE_NAME] = session.session_key 1291 1292 # request other language; should default to old language key value 1250 def test_language_not_saved_to_session(self): 1251 """Checks that current language is not automatically saved to 1252 session on every request.""" 1253 # Regression test for #21473 1293 1254 self.client.get('/fr/simple/') 1294 self.assert Equal(self.client.session['_language'], 'en')1255 self.assertNotIn('_language', self.client.session) 1295 1256 1296 1257 1297 1258 @override_settings(