Alex Marandon avatar Alex Marandon committed 2750a84

Set locale session key in change_locale view

Comments (0)

Files changed (4)

localeurl/settings.py

 
 USE_ACCEPT_LANGUAGE = getattr(settings, 'LOCALEURL_USE_ACCEPT_LANGUAGE', False)
 
+USE_SESSION = getattr(settings, 'LOCALEURL_USE_SESSION', False)
+
 LOCALE_REDIRECT_PERMANENT = getattr(settings, 'LOCALE_REDIRECT_PERMANENT', True)

localeurl/tests/tests.py

     def test_kwargs_none(self):
         url = reverse("dummy1", args=["test"], kwargs=None)
         self.assertEqual(url, "/en/dummy/test")
+
+
+class ViewsTestCase(LocaleurlTestCase):
+
+    urls = 'localeurl.urls'
+
+    def setUp(self):
+        super(LocaleurlTestCase, self).setUp()
+        self.settings_manager = test_utils.TestSettingsManager()
+
+    def test_change_locale_check_session_enabled(self):
+        self.settings_manager.set(LOCALEURL_USE_SESSION=True)
+        reload(localeurl_settings)
+        self.client.post('/change/', data={'locale': 'de', 'next': '/foo'})
+        self.assertEqual("de", self.client.session['locale'])
+
+    def test_change_locale_check_session_disabled(self):
+        self.settings_manager.set(LOCALEURL_USE_SESSION=False)
+        reload(localeurl_settings)
+        self.client.post('/change/', data={'locale': 'de', 'next': '/foo'})
+        self.assertNotEqual("de", self.client.session.get('locale'))

localeurl/views.py

 from django import http
 from django.utils.translation import check_for_language
 from localeurl import utils
+from localeurl import settings as localeurl_settings
 
 def change_locale(request):
     """
     if request.method == 'POST':
         locale = request.POST.get('locale', None)
         if locale and check_for_language(locale):
+            if localeurl_settings.USE_SESSION:
+                request.session['locale'] = locale
             path = utils.locale_path(path, locale)
 
     response = http.HttpResponseRedirect(path)
             'localeurl',
             'localeurl.tests',
             'django.contrib.sites', # for sitemap test
+            'django.contrib.sessions', # for USE_SESSION
             ),
         ROOT_URLCONF='localeurl.tests.test_urls',
         SITE_ID=1,
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.