Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.5.x] Fixed #19772 -- Handled APPEND_SLASH correctly in the redirec…

…ts app.

Backport of 64623a2.
  • Loading branch information...
commit 833ccd4b5b4b294e7ca8d32faa4461be7fafb13c 1 parent ad2311d
Aymeric Augustin authored February 07, 2013
20  django/contrib/redirects/middleware.py
... ...
@@ -1,3 +1,5 @@
  1
+from __future__ import unicode_literals
  2
+
1 3
 from django.contrib.redirects.models import Redirect
2 4
 from django.contrib.sites.models import get_current_site
3 5
 from django import http
@@ -7,17 +9,21 @@ class RedirectFallbackMiddleware(object):
7 9
     def process_response(self, request, response):
8 10
         if response.status_code != 404:
9 11
             return response # No need to check for a redirect for non-404 responses.
10  
-        path = request.get_full_path()
  12
+
  13
+        full_path = request.get_full_path()
11 14
         current_site = get_current_site(request)
  15
+
  16
+        r = None
12 17
         try:
13  
-            r = Redirect.objects.get(site__id__exact=current_site.id, old_path=path)
  18
+            r = Redirect.objects.get(site=current_site, old_path=full_path)
14 19
         except Redirect.DoesNotExist:
15  
-            r = None
16  
-        if r is None and settings.APPEND_SLASH:
17  
-            # Try removing the trailing slash.
  20
+            pass
  21
+        if settings.APPEND_SLASH and not request.path.endswith('/'):
  22
+            # Try appending a trailing slash.
  23
+            path_len = len(request.path)
  24
+            full_path = full_path[:path_len] + '/' + full_path[path_len:]
18 25
             try:
19  
-                r = Redirect.objects.get(site__id__exact=current_site.id,
20  
-                    old_path=path[:path.rfind('/')]+path[path.rfind('/')+1:])
  26
+                r = Redirect.objects.get(site=current_site, old_path=full_path)
21 27
             except Redirect.DoesNotExist:
22 28
                 pass
23 29
         if r is not None:
28  django/contrib/redirects/tests.py
@@ -8,10 +8,10 @@
8 8
 
9 9
 
10 10
 @override_settings(
11  
-    SITE_ID=1,
12  
-    APPEND_SLASH=True,
  11
+    APPEND_SLASH=False,
13 12
     MIDDLEWARE_CLASSES=list(settings.MIDDLEWARE_CLASSES) +
14 13
         ['django.contrib.redirects.middleware.RedirectFallbackMiddleware'],
  14
+    SITE_ID=1,
15 15
 )
16 16
 class RedirectTests(TestCase):
17 17
 
@@ -23,20 +23,32 @@ def test_model(self):
23 23
             site=self.site, old_path='/initial', new_path='/new_target')
24 24
         self.assertEqual(six.text_type(r1), "/initial ---> /new_target")
25 25
 
26  
-    def test_redirect_middleware(self):
27  
-        r1 = Redirect.objects.create(
  26
+    def test_redirect(self):
  27
+        Redirect.objects.create(
28 28
             site=self.site, old_path='/initial', new_path='/new_target')
29 29
         response = self.client.get('/initial')
30 30
         self.assertRedirects(response,
31 31
             '/new_target', status_code=301, target_status_code=404)
32  
-        # Works also with trailing slash
33  
-        response = self.client.get('/initial/')
  32
+
  33
+    @override_settings(APPEND_SLASH=True)
  34
+    def test_redirect_with_append_slash(self):
  35
+        Redirect.objects.create(
  36
+            site=self.site, old_path='/initial/', new_path='/new_target/')
  37
+        response = self.client.get('/initial')
34 38
         self.assertRedirects(response,
35  
-            '/new_target', status_code=301, target_status_code=404)
  39
+            '/new_target/', status_code=301, target_status_code=404)
  40
+
  41
+    @override_settings(APPEND_SLASH=True)
  42
+    def test_redirect_with_append_slash_and_query_string(self):
  43
+        Redirect.objects.create(
  44
+            site=self.site, old_path='/initial/?foo', new_path='/new_target/')
  45
+        response = self.client.get('/initial?foo')
  46
+        self.assertRedirects(response,
  47
+            '/new_target/', status_code=301, target_status_code=404)
36 48
 
37 49
     def test_response_gone(self):
38 50
         """When the redirect target is '', return a 410"""
39  
-        r1 = Redirect.objects.create(
  51
+        Redirect.objects.create(
40 52
             site=self.site, old_path='/initial', new_path='')
41 53
         response = self.client.get('/initial')
42 54
         self.assertEqual(response.status_code, 410)

0 notes on commit 833ccd4

Please sign in to comment.
Something went wrong with that request. Please try again.