Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add exemption for lang param in locale middleware.

There may be some URLs which have other uses for a
'lang' query parameter. This allows you to set such
URLs as exempt.
  • Loading branch information...
commit 7a1472a2ffbbb1639ed65e69a7a5f574f8c08abc 1 parent cbf37fd
@pmclanahan pmclanahan authored
Showing with 57 additions and 1 deletion.
  1. +10 −1 funfactory/middleware.py
  2. +47 −0 tests/test_middleware.py
View
11 funfactory/middleware.py
@@ -31,12 +31,21 @@ def __init__(self):
"loaded. Consider removing funfactory.middleware."
"LocaleURLMiddleware from your MIDDLEWARE_CLASSES setting.")
+ self.exempt_urls = getattr(settings, 'FF_EXEMPT_LANG_PARAM_URLS', ())
+
+ def _is_lang_change(self, request):
+ """Return True if the lang param is present and URL isn't exempt."""
+ if 'lang' not in request.GET:
+ return False
+
+ return not any(request.path.endswith(url) for url in self.exempt_urls)
+
def process_request(self, request):
prefixer = urlresolvers.Prefixer(request)
urlresolvers.set_url_prefix(prefixer)
full_path = prefixer.fix(prefixer.shortened_path)
- if 'lang' in request.GET:
+ if self._is_lang_change(request):
# Blank out the locale so that we can set a new one. Remove lang
# from the query params so we don't have an infinite loop.
prefixer.locale = ''
View
47 tests/test_middleware.py
@@ -0,0 +1,47 @@
+from django.test import TestCase, RequestFactory
+from django.test.utils import override_settings
+
+from funfactory.middleware import LocaleURLMiddleware
+
+
+class TestLocaleURLMiddleware(TestCase):
+ def setUp(self):
+ self.rf = RequestFactory()
+ self.middleware = LocaleURLMiddleware()
+
+ @override_settings(DEV_LANGUAGES=('de', 'fr'),
+ FF_EXEMPT_LANG_PARAM_URLS=())
+ def test_redirects_to_correct_language(self):
+ """Should redirect to lang prefixed url."""
+ path = '/the/dude/'
+ req = self.rf.get(path, HTTP_ACCEPT_LANGUAGE='de')
+ resp = LocaleURLMiddleware().process_request(req)
+ self.assertEqual(resp['Location'], '/de' + path)
+
+ @override_settings(DEV_LANGUAGES=('es', 'fr'),
+ LANGUAGE_CODE='en-US',
+ FF_EXEMPT_LANG_PARAM_URLS=())
+ def test_redirects_to_default_language(self):
+ """Should redirect to default lang if not in settings."""
+ path = '/the/dude/'
+ req = self.rf.get(path, HTTP_ACCEPT_LANGUAGE='de')
+ resp = LocaleURLMiddleware().process_request(req)
+ self.assertEqual(resp['Location'], '/en-US' + path)
+
+ @override_settings(DEV_LANGUAGES=('de', 'fr'),
+ FF_EXEMPT_LANG_PARAM_URLS=('/other/',))
+ def test_redirects_lang_param(self):
+ """Middleware should remove the lang param on redirect."""
+ path = '/fr/the/dude/'
+ req = self.rf.get(path, {'lang': 'de'})
+ resp = LocaleURLMiddleware().process_request(req)
+ self.assertEqual(resp['Location'], '/de/the/dude/')
+
+ @override_settings(DEV_LANGUAGES=('de', 'fr'),
+ FF_EXEMPT_LANG_PARAM_URLS=('/dude/',))
+ def test_no_redirect_lang_param(self):
+ """Middleware should not redirect when exempt."""
+ path = '/fr/the/dude/'
+ req = self.rf.get(path, {'lang': 'de'})
+ resp = LocaleURLMiddleware().process_request(req)
+ self.assertIs(resp, None) # no redirect
Please sign in to comment.
Something went wrong with that request. Please try again.