Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #1 from djay/master

Support for query strings in plone.app.redirector. Closes https://dev.plone.org/ticket/12354 and https://dev.plone.org/ticket/9967.
  • Loading branch information...
commit 67859bf3637ee1ceea2da2fe346a98853045d778 2 parents 250436c + f412466
Laurence Rowe authored November 21, 2011
2  .gitignore
... ...
@@ -0,0 +1,2 @@
  1
+*.pyc
  2
+*.egg-info
6  CHANGES.txt
@@ -7,6 +7,12 @@ Changelog
7 7
 - Support parts of views e.g. mypage/@@myview/somepart
8 8
   [anthonygerrard]
9 9
 
  10
+- #12354 will redirect based on the query string as well as path if query_string
  11
+  stored. [djay]
  12
+
  13
+- #9967 will append the same query string after redirecting to be more tracker
  14
+  friendly. [djay]
  15
+
10 16
 1.1.2 - 2011-07-05
11 17
 ------------------
12 18
 
19  plone/app/redirector/browser.py
@@ -38,7 +38,20 @@ def attempt_redirect(self):
38 38
             return False
39 39
 
40 40
         old_path = '/'.join(old_path_elements)
41  
-        new_path = storage.get(old_path)
  41
+
  42
+        # First lets try with query string in cases or content migration
  43
+
  44
+        new_path = None
  45
+        
  46
+        query_string = self.request.QUERY_STRING
  47
+        if query_string:
  48
+            new_path = storage.get("%s?%s" % (old_path,query_string))
  49
+            # if we matched on the query_string we don't want to include it in redirect
  50
+            if new_path:
  51
+                query_string = ''
  52
+
  53
+        if not new_path:
  54
+            new_path = storage.get(old_path)
42 55
 
43 56
         if not new_path:
44 57
             new_path = self.find_redirect_if_view(old_path_elements, storage) 
@@ -50,6 +63,10 @@ def attempt_redirect(self):
50 63
             return False
51 64
 
52 65
         url = self.request.physicalPathToURL(new_path)
  66
+        
  67
+        # some analytics programs might use this info to track
  68
+        if query_string:
  69
+            url += "?"+query_string
53 70
         self.request.response.redirect(url, status=301, lock=1)
54 71
         return True
55 72
     
21  plone/app/redirector/tests/test_view.py
@@ -15,9 +15,10 @@ def afterSetUp(self):
15 15
         self.folder = self.portal.testfolder
16 16
         self.storage = getUtility(IRedirectionStorage)
17 17
 
18  
-    def view(self, context, actual_url):
  18
+    def view(self, context, actual_url, query_string=''):
19 19
         request = self.app.REQUEST
20 20
         request['ACTUAL_URL'] = actual_url
  21
+        request['QUERY_STRING'] = query_string
21 22
         return getMultiAdapter((context, request), name='plone_redirector_view')
22 23
 
23 24
     def test_attempt_redirect_with_known_url(self):
@@ -63,6 +64,24 @@ def test_attempt_redirect_with_quoted_url(self):
63 64
         self.assertEquals(301, self.app.REQUEST.response.getStatus())
64 65
         self.assertEquals(fu + '/bar/baz%20quux', self.app.REQUEST.response.getHeader('location'))
65 66
 
  67
+    def test_attempt_redirect_with_query_string(self):
  68
+        fp = '/'.join(self.folder.getPhysicalPath())
  69
+        fu = self.folder.absolute_url()
  70
+        self.storage.add(fp + '/foo?blah=blah', fp + '/bar')
  71
+        view = self.view(self.portal, fu + '/foo','blah=blah')
  72
+        self.assertEquals(True, view.attempt_redirect())
  73
+        self.assertEquals(301, self.app.REQUEST.response.getStatus())
  74
+        self.assertEquals(fu + '/bar', self.app.REQUEST.response.getHeader('location'))
  75
+
  76
+    def test_attempt_redirect_appending_query_string(self):
  77
+        fp = '/'.join(self.folder.getPhysicalPath())
  78
+        fu = self.folder.absolute_url()
  79
+        self.storage.add(fp + '/foo', fp + '/bar')
  80
+        view = self.view(self.portal, fu + '/foo', 'blah=blah')
  81
+        self.assertEquals(True, view.attempt_redirect())
  82
+        self.assertEquals(301, self.app.REQUEST.response.getStatus())
  83
+        self.assertEquals(fu + '/bar?blah=blah', self.app.REQUEST.response.getHeader('location'))
  84
+
66 85
     def test_find_first_parent_found_leaf(self):
67 86
         self.folder.invokeFactory('Folder', 'f1')
68 87
         fu = self.folder.absolute_url()

0 notes on commit 67859bf

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