Skip to content

Commit

Permalink
Use standard area_from_code for legacy code lookup
Browse files Browse the repository at this point in the history
This should mean no unhandled server errors if multiple results
returned, and both endpoints should act the same.
  • Loading branch information
dracos committed Dec 21, 2023
1 parent ce3f322 commit e0bb011
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 36 deletions.
21 changes: 17 additions & 4 deletions mapit/views/areas.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from django.db.models import Q
from django.db.models.query import QuerySet
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import resolve, reverse
from django.urls import resolve, reverse, NoReverseMatch
from django.conf import settings
from django.shortcuts import redirect, render
from django.views.decorators.csrf import csrf_exempt
Expand Down Expand Up @@ -352,9 +352,10 @@ def areas_geometry(request, area_ids):


@ratelimit
def area_from_code(request, code_type, code_value, format=''):
q = query_args(request, format)
def area_from_code(request, code_type, code_value, format='', area_type=None):
q = query_args(request, format, area_type)
q &= Q(codes__type__code=code_type, codes__code=code_value)

try:
area = Area.objects.get(q)
except Area.DoesNotExist:
Expand All @@ -363,10 +364,22 @@ def area_from_code(request, code_type, code_value, format=''):
except Area.MultipleObjectsReturned:
message = _('There were multiple areas that matched code {0} = {1}.').format(code_type, code_value)
raise ViewException(format, message, 500)

area_kwargs = {'area_id': area.id}
if format:
area_kwargs['format'] = format
return HttpResponseRedirect(reverse('area', kwargs=area_kwargs))

# We're called either by area or area_polygon
try:
redirect_path = reverse('area', kwargs=area_kwargs)
except NoReverseMatch:
redirect_path = reverse('area_polygon', kwargs=area_kwargs)

# If there was a query string, make sure it's passed on in the
# redirect:
if request.META['QUERY_STRING']:
redirect_path += "?" + request.META['QUERY_STRING']

Check warning on line 381 in mapit/views/areas.py

View check run for this annotation

Codecov / codecov/patch

mapit/views/areas.py#L381

Added line #L381 was not covered by tests
return HttpResponseRedirect(redirect_path)


@ratelimit
Expand Down
42 changes: 10 additions & 32 deletions mapit_gb/countries.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,28 @@
import re

from django.urls import reverse, NoReverseMatch
from django.http import HttpResponseRedirect
from ukpostcodeutils import validation

from mapit.shortcuts import get_object_or_404


def area_code_lookup(request, area_code, format):
from mapit.models import Area, CodeType, Generation
from mapit.views.areas import area_from_code

area_code_type = None
if re.match(r'\d\d([A-Z]{2}|[A-Z]{4}|[A-Z]{2}\d\d\d|[A-Z]|[A-Z]\d\d)$', area_code):
area_code_type = CodeType.objects.get(code='ons')
area_code_type = 'ons'
elif re.match(r'[ESW]0[12]\d{6}$', area_code): # LSOA/MSOA have ONS code type
area_code_type = CodeType.objects.get(code='ons')
area_code_type = 'ons'
elif re.match(r'[ENSW]\d{8}$', area_code):
area_code_type = CodeType.objects.get(code='gss')
area_code_type = 'gss'
if not area_code_type:
return None

args = {'format': format, 'codes__type': area_code_type, 'codes__code': area_code}
area_type = None
if re.match('[EW]01', area_code):
args['type__code'] = 'OLF'
area_type = 'OLF'
elif re.match('[EW]02', area_code):
args['type__code'] = 'OMF'

try:
area = get_object_or_404(Area, **args)
except Area.MultipleObjectsReturned:
generation = Generation.objects.current()
args['generation_low__lte'] = generation
args['generation_high__gte'] = generation
area = get_object_or_404(Area, **args)
area_kwargs = {'area_id': area.id}
if format:
area_kwargs['format'] = format
# We're called either by area or area_polygon
try:
redirect_path = reverse('area', kwargs=area_kwargs)
except NoReverseMatch:
redirect_path = reverse('area_polygon', kwargs=area_kwargs)
# If there was a query string, make sure it's passed on in the
# redirect:
if request.META['QUERY_STRING']:
redirect_path += "?" + request.META['QUERY_STRING']
return HttpResponseRedirect(redirect_path)
area_type = 'OMF'

return area_from_code(request, area_code_type, area_code, format, area_type)


def canonical_postcode(pc):
Expand Down

0 comments on commit e0bb011

Please sign in to comment.