Skip to content

Commit

Permalink
refactors EMS importers to remove postcode field hardcoding
Browse files Browse the repository at this point in the history
  • Loading branch information
awdem committed May 20, 2024
1 parent 1d7edbe commit 276fa6a
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions polling_stations/apps/data_importers/ems_importers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
}
Expand Down Expand Up @@ -344,6 +345,7 @@ class BaseHalaroseCsvImporter(
"pollingstationaddress_5",
]
residential_uprn_field = "uprn"
residential_postcode_field = "housepostcode"

def get_station_hash(self, record):
return "-".join(
Expand Down Expand Up @@ -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)
Expand All @@ -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,
}
Expand All @@ -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(
Expand All @@ -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,
}
Expand Down Expand Up @@ -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,
}
Expand Down

0 comments on commit 276fa6a

Please sign in to comment.