Repository: django-cms/django-cms

Issue:
session language is not overwritten with LanguageCookieMiddleware

session language is not overwritten with LanguageCookieMiddleware



Discussion:
If you change the language in admin the new language gets saved in the session. The cookie middlware does not detect this and should overwrite it if you change language again in frontend

If you change the language in admin the new language gets saved in the session. The cookie middlware does not detect this and should overwrite it if you change language again in frontend


Resolving patch:
diff --git a/cms/middleware/language.py b/cms/middleware/language.py
--- a/cms/middleware/language.py
+++ b/cms/middleware/language.py
@@ -8,6 +8,11 @@
 class LanguageCookieMiddleware(object):
     def process_response(self, request, response):
         language = get_language()
+        if hasattr(request, 'session'):
+            session_language = request.session.get('django_language', None)
+            if session_language and not session_language == language:
+                request.session['django_language'] = language
+                request.session.save()
         if settings.LANGUAGE_COOKIE_NAME in request.COOKIES and \
                         request.COOKIES[settings.LANGUAGE_COOKIE_NAME] == language:
             return response


Test patch:
diff --git a/cms/tests/i18n.py b/cms/tests/i18n.py
--- a/cms/tests/i18n.py
+++ b/cms/tests/i18n.py
@@ -1,6 +1,8 @@
 from cms import api
 from cms.test_utils.testcases import SettingsOverrideTestCase
+from cms.test_utils.util.context_managers import SettingsOverride
 from cms.utils import i18n
+from django.utils.importlib import import_module
 
 
 class TestLanguages(SettingsOverrideTestCase):
@@ -306,3 +308,38 @@ def test_language_code(self):
         response = self.client.get('/en/')
         self.assertEqual(response.status_code, 302)
         self.assertRedirects(response, '/fr/')
+
+    def test_session_language(self):
+        with SettingsOverride(CMS_LANGUAGES={
+            1: [ {'code' : 'en',
+                  'name': 'English',
+                  'public': True},
+                 {'code': 'fr',
+                  'name': 'French',
+                  'public': True},
+                 ]}):
+            page = api.create_page("home", "nav_playground.html", "en", published=True)
+            api.create_title('fr', "home", page)
+            page.publish('fr')
+            page.publish('en')
+            response = self.client.get('/')
+            self.assertEqual(response.status_code, 302)
+            self.assertRedirects(response, '/en/')
+            from django.conf import settings
+            engine = import_module(settings.SESSION_ENGINE)
+            store = engine.SessionStore()
+            store.save()  # we need to make load() work, or the cookie is worthless
+            self.client.cookies[settings.SESSION_COOKIE_NAME] = store.session_key
+
+            #   ugly and long set of session
+            session = self.client.session
+            session['django_language'] = 'fr'
+            session.save()
+            response = self.client.get('/')
+            self.assertEqual(response.status_code, 302)
+            self.assertRedirects(response, '/fr/')
+            self.client.get('/en/')
+            self.assertEqual(self.client.session['django_language'], 'en')
+            response = self.client.get('/')
+            self.assertEqual(response.status_code, 302)
+            self.assertRedirects(response, '/en/')