Ticket #21473: 21473-master.patch

File 21473-master.patch, 5.6 KB (added by Claude Paroz, 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):  
    105105        user = None
    106106    user_logged_out.send(sender=user.__class__, request=request, user=user)
    107107
     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
    108112    request.session.flush()
     113
     114    if language is not None:
     115        request.session['_language'] = language
     116
    109117    if hasattr(request, 'user'):
    110118        from django.contrib.auth.models import AnonymousUser
    111119        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  
     1from importlib import import_module
    12import itertools
    23import os
    34import re
    class LogoutTest(AuthViewsTestCase):  
    710711                            "%s should be allowed" % good_url)
    711712            self.confirm_logged_out()
    712713
     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
    713726
    714727@skipIfCustomUser
    715728@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):  
    5555                    request.get_full_path())
    5656                return self.response_redirect_class(language_url)
    5757
    58         # Store language back into session if it is not present
    59         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'] = language
    67 
    6858        if not (self.is_language_prefix_patterns_used()
    6959                and language_from_path):
    7060            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):  
    12471247            'django.middleware.common.CommonMiddleware',
    12481248        ),
    12491249    )
    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
    12931254        self.client.get('/fr/simple/')
    1294         self.assertEqual(self.client.session['_language'], 'en')
     1255        self.assertNotIn('_language', self.client.session)
    12951256
    12961257
    12971258@override_settings(
Back to Top