diff --git a/wagtail/wagtailredirects/models.py b/wagtail/wagtailredirects/models.py index 88d40c23e..48a43346a 100644 --- a/wagtail/wagtailredirects/models.py +++ b/wagtail/wagtailredirects/models.py @@ -40,6 +40,9 @@ class Redirect(models.Model): @staticmethod def normalise_path(url): + # Strip whitespace + url = url.strip() + # Parse url url_parsed = urlparse(url) diff --git a/wagtail/wagtailredirects/tests.py b/wagtail/wagtailredirects/tests.py index 884ceb681..402100dec 100644 --- a/wagtail/wagtailredirects/tests.py +++ b/wagtail/wagtailredirects/tests.py @@ -20,6 +20,8 @@ class TestRedirects(TestCase): self.assertEqual(path, normalise_path('Hello/world.html/?foo=Bar&Baz=quux2')) # Trailing slashes are ignored self.assertEqual(path, normalise_path('/Hello/world.html?foo=Bar&Baz=quux2#cool')) # Fragments are ignored self.assertEqual(path, normalise_path('/Hello/world.html?Baz=quux2&foo=Bar')) # Order of query string parameters are ignored + self.assertEqual(path, normalise_path(' /Hello/world.html?foo=Bar&Baz=quux2')) # Leading whitespace + self.assertEqual(path, normalise_path('/Hello/world.html?foo=Bar&Baz=quux2 ')) # Trailing whitespace # Test against different paths self.assertNotEqual(path, normalise_path('/hello/world.html?foo=Bar&Baz=quux2')) # 'hello' is lowercase @@ -119,6 +121,7 @@ class TestRedirectsAddView(TestCase, WagtailTestUtils): redirects = models.Redirect.objects.filter(old_path='/test') self.assertEqual(redirects.count(), 1) self.assertEqual(redirects.first().redirect_link, 'http://www.test.com/') + self.assertEqual(redirects.first().site, None) def test_add_validation_error(self): response = self.post({ diff --git a/wagtail/wagtailredirects/views.py b/wagtail/wagtailredirects/views.py index 1f34197fb..c07a7a28a 100644 --- a/wagtail/wagtailredirects/views.py +++ b/wagtail/wagtailredirects/views.py @@ -108,9 +108,7 @@ def add(request): if request.POST: form = form_class(request.POST, request.FILES) if form.is_valid(): - theredirect = form.save(commit=False) - theredirect.site = request.site - theredirect.save() + theredirect = form.save() messages.success(request, _("Redirect '{0}' added.").format(theredirect.title), buttons=[ messages.button(reverse('wagtailredirects:edit', args=(theredirect.id,)), _('Edit'))