Anonymous avatar Anonymous committed 9736e3a

Refactor configurable redirects, add tests.

* Refactor configurable redirects to use status code instead of class.
* Move status code setting from django settings to localeurl_settings.
* Rename redirect setting.
* Remove unused import.

Comments (0)

Files changed (3)

localeurl/middleware.py

 from django.conf import settings
 import django.core.exceptions
-from django.http import HttpResponsePermanentRedirect
+from django.http import HttpResponsePermanentRedirect, HttpResponseRedirect
 from django.utils import translation
 from django.utils.encoding import iri_to_uri
 from django.utils.translation.trans_real import parse_accept_lang_header
 from localeurl import settings as localeurl_settings
 # Importing models ensures that reverse() is patched soon enough. Refs #5.
-from localeurl import models, utils
+from localeurl import utils
 
 # Make sure the default language is in the list of supported languages
 assert utils.supported_language(settings.LANGUAGE_CODE) is not None, \
                 locale_path = "%s?%s" % (locale_path,
                         request.META['QUERY_STRING'])
             locale_url = utils.add_script_prefix(locale_path)
-            redirect_class = getattr(settings,
-                                     "LANGUAGE_REDIRECT_CLASS",
-                                     HttpResponsePermanentRedirect)
+            redirect_class = {
+                301: HttpResponsePermanentRedirect,
+                302: HttpResponseRedirect,
+            }.get(getattr(localeurl_settings, "LOCALE_REDIRECT_CODE"),
+                  HttpResponsePermanentRedirect)
             # @@@ iri_to_uri for Django 1.0; 1.1+ do it in HttpResp...Redirect
             return redirect_class(iri_to_uri(locale_url))
         request.path_info = path

localeurl/settings.py

 PREFIX_DEFAULT_LOCALE = getattr(settings, 'PREFIX_DEFAULT_LOCALE', True)
 
 USE_ACCEPT_LANGUAGE = getattr(settings, 'LOCALEURL_USE_ACCEPT_LANGUAGE', False)
+
+LOCALE_REDIRECT_CODE = getattr(settings, 'LOCALE_REDIRECT_CODE', 301)

localeurl/tests/tests.py

         urlresolvers.set_script_prefix(previous_script_prefix)
 
 
+    def test_redirect_statuscode_302(self):
+        self.settings_manager.set(LOCALE_REDIRECT_CODE=302)
+        reload(localeurl_settings)
+
+        r1 = self.request_factory.get('/nl-be/test/independent/?foo=bar')
+        r2 = self.middleware.process_request(r1)
+        self.assertEqual(302, r2.status_code)
+
+
+    def test_redirect_statuscode_not_supported(self):
+        self.settings_manager.set(LOCALE_REDIRECT_CODE=408)
+        reload(localeurl_settings)
+
+        r1 = self.request_factory.get('/nl-be/test/independent/?foo=bar')
+        r2 = self.middleware.process_request(r1)
+        self.assertEqual(301, r2.status_code)
+
+
     def test_check_accept_lang(self):
         self.settings_manager.set(LOCALEURL_USE_ACCEPT_LANGUAGE=True)
         reload(localeurl_settings)
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.