Skip to content

Commit

Permalink
Merge pull request #23 from rcpch/mbarton/remove-double-postcode-vali…
Browse files Browse the repository at this point in the history
…dation

Remove redundant postcode validation
  • Loading branch information
mbarton authored Jul 9, 2024
2 parents 49bb16d + 4272187 commit 14a0e25
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 102 deletions.
1 change: 0 additions & 1 deletion deprivation_scores/general_functions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
lsoa_for_postcode,
regions_for_postcode,
local_authority_district_code_for_postcode,
is_valid_postcode,
)
from .quantile import quantile_for_rank
11 changes: 0 additions & 11 deletions deprivation_scores/general_functions/postcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,3 @@ def local_authority_district_code_for_postcode(postcode):
serialised = response.json()
lad = serialised["result"]["codes"]["admin_district"]
return lad


def is_valid_postcode(postcode):

url = f"https://api.postcodes.io/postcodes/{postcode}/validate"
response = requests.get(url=url)
if response.status_code == 404:
print("Postcode validation failure. Could not validate postcode.")
return False
else:
return response.json()["result"]
177 changes: 87 additions & 90 deletions deprivation_scores/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
from .general_functions import (
lsoa_for_postcode,
regions_for_postcode,
is_valid_postcode,
)


Expand Down Expand Up @@ -353,45 +352,44 @@ def get(self, request):
"""
post_code = self.request.query_params.get("postcode", None)
if post_code:
if is_valid_postcode(postcode=post_code):
lsoa_object = lsoa_for_postcode(postcode=post_code)

if lsoa_object["lsoa"]:
lsoa_code = lsoa_object["lsoa"]
if lsoa_object["country"] == "England":
lsoa = LSOA.objects.filter(lsoa_code=lsoa_code).get()
imd = EnglishIndexMultipleDeprivation.objects.filter(
lsoa=lsoa
).get()
response = self.english_serializer_class(
instance=imd, context={"request": request}
)
elif lsoa_object["country"] == "Wales":
lsoa = LSOA.objects.filter(lsoa_code=lsoa_code).get()
imd = WelshIndexMultipleDeprivation.objects.filter(
lsoa=lsoa
).get()
response = self.welsh_serializer_class(
instance=imd, context={"request": request}
)
elif lsoa_object["country"] == "Scotland":
lsoa = DataZone.objects.filter(data_zone_code=lsoa_code).get()
imd = ScottishIndexMultipleDeprivation.objects.filter(
data_zone=lsoa
).get()
response = self.scottish_serializer_class(
instance=imd, context={"request": request}
)
elif lsoa_object["country"] == "Northern Ireland":
lsoa = SOA.objects.filter(soa_code=lsoa_code).get()
imd = NorthernIrelandIndexMultipleDeprivation.objects.filter(
soa=lsoa
).get()
response = self.northern_ireland_serializer_class(
instance=imd, context={"request": request}
)
else:
raise ParseError("No valid country supplied.", code=400)
lsoa_object = lsoa_for_postcode(postcode=post_code)

if lsoa_object["lsoa"]:
lsoa_code = lsoa_object["lsoa"]
if lsoa_object["country"] == "England":
lsoa = LSOA.objects.filter(lsoa_code=lsoa_code).get()
imd = EnglishIndexMultipleDeprivation.objects.filter(
lsoa=lsoa
).get()
response = self.english_serializer_class(
instance=imd, context={"request": request}
)
elif lsoa_object["country"] == "Wales":
lsoa = LSOA.objects.filter(lsoa_code=lsoa_code).get()
imd = WelshIndexMultipleDeprivation.objects.filter(
lsoa=lsoa
).get()
response = self.welsh_serializer_class(
instance=imd, context={"request": request}
)
elif lsoa_object["country"] == "Scotland":
lsoa = DataZone.objects.filter(data_zone_code=lsoa_code).get()
imd = ScottishIndexMultipleDeprivation.objects.filter(
data_zone=lsoa
).get()
response = self.scottish_serializer_class(
instance=imd, context={"request": request}
)
elif lsoa_object["country"] == "Northern Ireland":
lsoa = SOA.objects.filter(soa_code=lsoa_code).get()
imd = NorthernIrelandIndexMultipleDeprivation.objects.filter(
soa=lsoa
).get()
response = self.northern_ireland_serializer_class(
instance=imd, context={"request": request}
)
else:
raise ParseError("No valid country supplied.", code=400)
else:
# postcode not valid
raise ParseError("Invalid postcode supplied.", code=400)
Expand Down Expand Up @@ -468,56 +466,55 @@ def get(self, request):
post_code = self.request.query_params.get("postcode", None)
requested_quantile = self.request.query_params.get("quantile", None)
if post_code:
if is_valid_postcode(postcode=post_code):
lsoa_object = lsoa_for_postcode(postcode=post_code)
if lsoa_object["lsoa"]:
lsoa_code = lsoa_object["lsoa"]
if lsoa_object["country"] == "England":
lsoa = LSOA.objects.filter(lsoa_code=lsoa_code).get()
imd = EnglishIndexMultipleDeprivation.objects.filter(
lsoa=lsoa
).get()
data = quantile_for_rank(
rank=imd.imd_rank,
requested_quantile=requested_quantile,
country="england",
)
response = Response({"result": data})
elif lsoa_object["country"] == "Wales":
lsoa = LSOA.objects.filter(lsoa_code=lsoa_code).get()
imd = WelshIndexMultipleDeprivation.objects.filter(
lsoa=lsoa
).get()
data = quantile_for_rank(
rank=imd.imd_rank,
requested_quantile=requested_quantile,
country="wales",
)
response = Response({"result": data})
elif lsoa_object["country"] == "Scotland":
lsoa = DataZone.objects.filter(data_zone_code=lsoa_code).get()
imd = ScottishIndexMultipleDeprivation.objects.filter(
data_zone=lsoa
).get()
data = quantile_for_rank(
rank=imd.imd_rank,
requested_quantile=requested_quantile,
country="scotland",
)
response = Response({"result": data})
elif lsoa_object["country"] == "Northern Ireland":
lsoa = SOA.objects.filter(soa_code=lsoa_code).get()
imd = NorthernIrelandIndexMultipleDeprivation.objects.filter(
soa=lsoa
).get()
data = quantile_for_rank(
rank=imd.imd_rank,
requested_quantile=requested_quantile,
country="northern_ireland",
)
response = Response({"result": data})
else:
raise ParseError("No valid country supplied.", code=400)
lsoa_object = lsoa_for_postcode(postcode=post_code)
if lsoa_object["lsoa"]:
lsoa_code = lsoa_object["lsoa"]
if lsoa_object["country"] == "England":
lsoa = LSOA.objects.filter(lsoa_code=lsoa_code).get()
imd = EnglishIndexMultipleDeprivation.objects.filter(
lsoa=lsoa
).get()
data = quantile_for_rank(
rank=imd.imd_rank,
requested_quantile=requested_quantile,
country="england",
)
response = Response({"result": data})
elif lsoa_object["country"] == "Wales":
lsoa = LSOA.objects.filter(lsoa_code=lsoa_code).get()
imd = WelshIndexMultipleDeprivation.objects.filter(
lsoa=lsoa
).get()
data = quantile_for_rank(
rank=imd.imd_rank,
requested_quantile=requested_quantile,
country="wales",
)
response = Response({"result": data})
elif lsoa_object["country"] == "Scotland":
lsoa = DataZone.objects.filter(data_zone_code=lsoa_code).get()
imd = ScottishIndexMultipleDeprivation.objects.filter(
data_zone=lsoa
).get()
data = quantile_for_rank(
rank=imd.imd_rank,
requested_quantile=requested_quantile,
country="scotland",
)
response = Response({"result": data})
elif lsoa_object["country"] == "Northern Ireland":
lsoa = SOA.objects.filter(soa_code=lsoa_code).get()
imd = NorthernIrelandIndexMultipleDeprivation.objects.filter(
soa=lsoa
).get()
data = quantile_for_rank(
rank=imd.imd_rank,
requested_quantile=requested_quantile,
country="northern_ireland",
)
response = Response({"result": data})
else:
raise ParseError("No valid country supplied.", code=400)
else:
# postcode not valid
raise ParseError("Invalid postcode supplied.", code=400)
Expand Down

0 comments on commit 14a0e25

Please sign in to comment.