From 5f0dec2b63c58d23e8ee8939e3f374de62d4d9a9 Mon Sep 17 00:00:00 2001 From: Michael Barton Date: Mon, 8 Jul 2024 20:25:45 +0100 Subject: [PATCH 1/2] Remove redundant call to postcodes.io validate /validate does the same underlying database lookup at their end as the normal API so we don't need the additional call --- deprivation_scores/views.py | 77 ++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 39 deletions(-) diff --git a/deprivation_scores/views.py b/deprivation_scores/views.py index 28373b4..e430d08 100644 --- a/deprivation_scores/views.py +++ b/deprivation_scores/views.py @@ -353,45 +353,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) From 42721870ab79cc50bf904e16510c8da52c1f6b01 Mon Sep 17 00:00:00 2001 From: Michael Barton Date: Mon, 8 Jul 2024 20:32:07 +0100 Subject: [PATCH 2/2] Remove helper entirely --- .../general_functions/__init__.py | 1 - .../general_functions/postcode.py | 11 -- deprivation_scores/views.py | 100 +++++++++--------- 3 files changed, 49 insertions(+), 63 deletions(-) diff --git a/deprivation_scores/general_functions/__init__.py b/deprivation_scores/general_functions/__init__.py index ca7c948..b178e37 100644 --- a/deprivation_scores/general_functions/__init__.py +++ b/deprivation_scores/general_functions/__init__.py @@ -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 diff --git a/deprivation_scores/general_functions/postcode.py b/deprivation_scores/general_functions/postcode.py index abb086f..18db8e5 100644 --- a/deprivation_scores/general_functions/postcode.py +++ b/deprivation_scores/general_functions/postcode.py @@ -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"] diff --git a/deprivation_scores/views.py b/deprivation_scores/views.py index e430d08..d0b43f1 100644 --- a/deprivation_scores/views.py +++ b/deprivation_scores/views.py @@ -51,7 +51,6 @@ from .general_functions import ( lsoa_for_postcode, regions_for_postcode, - is_valid_postcode, ) @@ -467,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)