Skip to content

Commit

Permalink
handle lower case area names and redirect to canonical URL
Browse files Browse the repository at this point in the history
Again, in case someone lower cases the area name check for this and
redirect to the canonical URL
  • Loading branch information
struan committed Feb 15, 2024
1 parent 400645d commit cf05d21
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
7 changes: 7 additions & 0 deletions hub/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,13 @@ def test_area_page_lower_case_area_code(self):
self.assertEqual(response.status_code, 301)
self.assertEqual(response.headers["Location"], "/area/WMC/South%20Borsetshire")

def test_area_page_lower_case_area_name(self):
url = reverse("area", args=("wmc", "south borsetshire"))
response = self.client.get(url)

self.assertEqual(response.status_code, 301)
self.assertEqual(response.headers["Location"], "/area/WMC/South%20Borsetshire")

def test_area_page_logged_in(self):
DataSet.objects.update(featured=True)
url = reverse("area", args=("WMC", "South Borsetshire"))
Expand Down
15 changes: 11 additions & 4 deletions hub/views/area.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,29 @@ class BaseAreaView(TitleMixin, DetailView):

def get_object(self):
area_type_code = self.kwargs.get("area_type", "")
lower_case_code = False
bad_case = False

if area_type_code not in AreaType.VALID_AREA_TYPES:
area_type_code = area_type_code.upper()
if area_type_code in AreaType.VALID_AREA_TYPES:
lower_case_code = True
bad_case = True

try:
area = Area.objects.get(
area_type__code=area_type_code,
name=self.kwargs.get("name"),
)
except Area.DoesNotExist:
raise Http404("Area not found")
try:
area = Area.objects.get(
area_type__code=area_type_code,
name__iexact=self.kwargs.get("name"),
)
bad_case = True
except Area.DoesNotExist:
raise Http404("Area not found")

if lower_case_code:
if bad_case:
return redirect(area, permanent=True)

return area
Expand Down

0 comments on commit cf05d21

Please sign in to comment.