1. Carl Meyer
  2. django-localeurl

Commits

Carl Meyer  committed 7f00b6c

Corrected infinite redirect with script prefix introduced in fix for #10.

  • Participants
  • Parent commits 871bcbc
  • Branches default

Comments (0)

Files changed (3)

File localeurl/middleware.py

View file
                         request.META.get('HTTP_ACCEPT_LANGUAGE', ''))])
             if accept_langs:
                 locale = accept_langs[0]
-        locale_url = utils.locale_url(path, locale)
-        if locale_url != request.path_info:
+        locale_path = utils.locale_path(path, locale)
+        if locale_path != request.path_info:
             if request.META.get("QUERY_STRING", ""):
-                locale_url = "%s?%s" % (locale_url,
+                locale_path = "%s?%s" % (locale_path,
                         request.META['QUERY_STRING'])
+            locale_url = utils.add_script_prefix(locale_path)
             # @@@ iri_to_uri for Django 1.0; 1.1+ do it in HttpResp...Redirect
             return HttpResponsePermanentRedirect(iri_to_uri(locale_url))
         request.path_info = path

File localeurl/tests/tests.py

View file
         self.assertEqual(301, r2.status_code)
         self.assertEqual('/prefix/test/independent/', r2['Location'])
 
+        # no infinite redirects
+        r3 = self.request_factory.get('/test/independent/')
+        r4 = self.middleware.process_request(r3)
+        self.assertEqual(None, r4)
+
         urlresolvers.set_script_prefix(previous_script_prefix)
 
 

File localeurl/utils.py

View file
     the locale is empty settings.LANGUAGE_CODE is used.
     """
     path = locale_path(path, locale)
-    return ''.join([urlresolvers.get_script_prefix(), path[1:]])
+    return add_script_prefix(path)
 
 def strip_script_prefix(url):
     """
             "URL must start with SCRIPT_PREFIX: %s" % url
     pos = len(urlresolvers.get_script_prefix()) - 1
     return url[:pos], url[pos:]
+
+def add_script_prefix(path):
+    """
+    Prepends the SCRIPT_PREFIX to a path.
+
+    """
+    return ''.join([urlresolvers.get_script_prefix(), path[1:]])