From 276fa6a35dcf0ff8156bbd357000a3452692990b Mon Sep 17 00:00:00 2001 From: awdem Date: Mon, 20 May 2024 11:43:11 +0100 Subject: [PATCH] refactors EMS importers to remove postcode field hardcoding --- .../apps/data_importers/ems_importers.py | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/polling_stations/apps/data_importers/ems_importers.py b/polling_stations/apps/data_importers/ems_importers.py index 90068e5317..1c2c7a977f 100644 --- a/polling_stations/apps/data_importers/ems_importers.py +++ b/polling_stations/apps/data_importers/ems_importers.py @@ -249,9 +249,10 @@ class BaseXpressDemocracyClubCsvImporter(BaseXpressCsvImporter, metaclass=abc.AB easting_field = "polling_place_easting" northing_field = "polling_place_northing" residential_uprn_field = "property_urn" + residential_postcode_field = "addressline6" def address_record_to_dict(self, record): - if record.addressline6.strip() == "": + if getattr(record, self.residential_postcode_field).strip() == "": return None address = format_residential_address( @@ -268,7 +269,7 @@ def address_record_to_dict(self, record): return { "address": address.strip(), - "postcode": record.addressline6.strip(), + "postcode": getattr(record, self.residential_postcode_field).strip(), "polling_station_id": getattr(record, self.station_id_field).strip(), "uprn": uprn, } @@ -344,6 +345,7 @@ class BaseHalaroseCsvImporter( "pollingstationaddress_5", ] residential_uprn_field = "uprn" + residential_postcode_field = "housepostcode" def get_station_hash(self, record): return "-".join( @@ -435,7 +437,7 @@ def address_record_to_dict(self, record): if record.streetname.lower().strip() == "other electors address": return None - if record.housepostcode.strip() == "": + if getattr(record, self.residential_postcode_field).strip() == "": return None address = self.get_residential_address(record) @@ -449,7 +451,7 @@ def address_record_to_dict(self, record): return { "address": address, - "postcode": record.housepostcode.strip(), + "postcode": getattr(record, self.residential_postcode_field).strip(), "polling_station_id": station_id, "uprn": uprn, } @@ -472,16 +474,17 @@ class BaseDemocracyCountsCsvImporter( csv_delimiter = "," station_name_field = "placename" address_fields = ["add1", "add2", "add3", "add4", "add5", "add6"] - postcode_field = "postcode" + station_postcode_field = "postcode" station_id_field = "stationcode" residential_uprn_field = "uprn" + residential_postcode_field = "postcode" def address_record_to_dict(self, record): - if getattr(record, self.postcode_field).strip() == "A1 1AA": + if getattr(record, self.residential_postcode_field).strip() == "A1 1AA": # this is a dummy record return None - if not getattr(record, self.postcode_field).strip(): + if not getattr(record, self.residential_postcode_field).strip(): return None address = format_residential_address( @@ -495,7 +498,7 @@ def address_record_to_dict(self, record): return { "address": address, - "postcode": getattr(record, self.postcode_field).strip(), + "postcode": getattr(record, self.residential_postcode_field).strip(), "polling_station_id": getattr(record, self.station_id_field).strip(), "uprn": uprn, } @@ -536,7 +539,7 @@ def station_record_to_dict(self, record): return { "internal_council_id": getattr(record, self.station_id_field).strip(), - "postcode": getattr(record, self.postcode_field).strip(), + "postcode": getattr(record, self.station_postcode_field).strip(), "address": address, "location": location, }