From c0fb9bd00b16bf384b726adb3c0dec34fc90e615 Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Thu, 3 Mar 2011 13:03:53 +0000 Subject: [PATCH] Fixed #13411 -- Made sure URL fragments are correctly handled by the next_redirect utility of the comments apps. Thanks, timesong, dpn and Julien Phalip. git-svn-id: http://code.djangoproject.com/svn/django/trunk@15720 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/comments/views/utils.py | 9 ++++++- .../comment_tests/tests/comment_view_tests.py | 24 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/django/contrib/comments/views/utils.py b/django/contrib/comments/views/utils.py index 8b729d2d95d..cc985e52d20 100644 --- a/django/contrib/comments/views/utils.py +++ b/django/contrib/comments/views/utils.py @@ -25,8 +25,15 @@ def next_redirect(data, default, default_view, **get_kwargs): if next is None: next = urlresolvers.reverse(default_view) if get_kwargs: + if '#' in next: + tmp = next.rsplit('#', 1) + next = tmp[0] + anchor = '#' + tmp[1] + else: + anchor = '' + joiner = ('?' in next) and '&' or '?' - next += joiner + urllib.urlencode(get_kwargs) + next += joiner + urllib.urlencode(get_kwargs) + anchor return HttpResponseRedirect(next) def confirmation_view(template, doc="Display a confirmation view."): diff --git a/tests/regressiontests/comment_tests/tests/comment_view_tests.py b/tests/regressiontests/comment_tests/tests/comment_view_tests.py index b8a62b42461..5e791966aec 100644 --- a/tests/regressiontests/comment_tests/tests/comment_view_tests.py +++ b/tests/regressiontests/comment_tests/tests/comment_view_tests.py @@ -256,3 +256,27 @@ class CommentViewTests(CommentTestCase): broken_location = location + u"\ufffd" response = self.client.get(broken_location) self.assertEqual(response.status_code, 200) + + def testCommentNextWithQueryStringAndAnchor(self): + """ + The `next` key needs to handle already having an anchor. Refs #13411. + """ + # With a query string also. + a = Article.objects.get(pk=1) + data = self.getValidData(a) + data["next"] = "/somewhere/else/?foo=bar#baz" + data["comment"] = "This is another comment" + response = self.client.post("/post/", data) + location = response["Location"] + match = re.search(r"^http://testserver/somewhere/else/\?foo=bar&c=\d+#baz$", location) + self.failUnless(match != None, "Unexpected redirect location: %s" % location) + + # Without a query string + a = Article.objects.get(pk=1) + data = self.getValidData(a) + data["next"] = "/somewhere/else/#baz" + data["comment"] = "This is another comment" + response = self.client.post("/post/", data) + location = response["Location"] + match = re.search(r"^http://testserver/somewhere/else/\?c=\d+#baz$", location) + self.failUnless(match != None, "Unexpected redirect location: %s" % location)