From 6ac093f5f73d5c8f8e7ab85a3dfe76af8e5148c2 Mon Sep 17 00:00:00 2001 From: Struan Donald Date: Wed, 21 Feb 2024 09:38:14 +0000 Subject: [PATCH] strip spaces from start/end of search term Fixes #477 --- hub/tests/test_views.py | 15 +++++++++++++++ hub/views/area.py | 3 +++ 2 files changed, 18 insertions(+) diff --git a/hub/tests/test_views.py b/hub/tests/test_views.py index aa3d5b2c9..96d93c9a0 100644 --- a/hub/tests/test_views.py +++ b/hub/tests/test_views.py @@ -355,6 +355,21 @@ def test_area_name_lookup(self): context = response.context self.assertEqual(context["area"].name, "South Borsetshire") + def test_search_with_spaces(self): + url = reverse("area_search") + response = self.client.get(url, {"search": " South Borsetshire"}, follow=True) + + self.assertRedirects(response, "/area/WMC/South%20Borsetshire") + self.assertTemplateUsed(response, "hub/area.html") + + response = self.client.get(url, {"search": "South Borsetshire "}, follow=True) + + self.assertRedirects(response, "/area/WMC/South%20Borsetshire") + self.assertTemplateUsed(response, "hub/area.html") + + context = response.context + self.assertEqual(context["area"].name, "South Borsetshire") + def test_mp_name_lookup(self): url = reverse("area_search") response = self.client.get(url, {"search": "James Madeupname"}, follow=True) diff --git a/hub/views/area.py b/hub/views/area.py index ff6561901..2f4e79f41 100644 --- a/hub/views/area.py +++ b/hub/views/area.py @@ -407,6 +407,9 @@ def get_context_data(self, **kwargs): if search is None and lat is None and lon is None: return context + if search is not None: + search = search.strip() + context["search"] = search if lon and lat: