Repository: wagtail/wagtail

Issue:
Redirects: The "Redirect from:" field does not strip leading/trailing whitespace
When editing a redirect record, if I put whitespace before or after the main content in the "Redirect from" field, the extra whitespace is never stripped from the value when it is saved to the database. This can create hard to debug errors in redirects that are created.

When I compare this to the "Redirect to any URL" field, trailing whitespace is correctly stripped and leading whitespace creates a validate error.



Discussion:
Looking at [wagtailredirects/models.py](https://github.com/torchbox/wagtail/blob/master/wagtail/wagtailredirects/models.py#L43), it looks like adding a line to the `normalise_path` method to strip out whitespace would fix this.

Try it please.

@strife25
Thanks for spotting. It looks like you're right, should be an easy fix.


Resolving patch:
diff --git a/wagtail/wagtailredirects/models.py b/wagtail/wagtailredirects/models.py
--- a/wagtail/wagtailredirects/models.py
+++ b/wagtail/wagtailredirects/models.py
@@ -41,6 +41,9 @@ def get_for_site(cls, site=None):
 
     @staticmethod
     def normalise_path(url):
+        # Strip whitespace
+        url = url.strip()
+
         # Parse url
         url_parsed = urlparse(url)
 
diff --git a/wagtail/wagtailredirects/views.py b/wagtail/wagtailredirects/views.py
--- 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_redirect', args=(theredirect.id,)), _('Edit'))


Test patch:
diff --git a/wagtail/wagtailredirects/tests.py b/wagtail/wagtailredirects/tests.py
--- a/wagtail/wagtailredirects/tests.py
+++ b/wagtail/wagtailredirects/tests.py
@@ -20,6 +20,8 @@ def test_path_normalisation(self):
         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 @@ def test_add(self):
         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({