Skip to content

Commit

Permalink
update mp import script to use twfyid as external id
Browse files Browse the repository at this point in the history
  • Loading branch information
struan committed Feb 19, 2024
1 parent d61deae commit 93f9527
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
31 changes: 16 additions & 15 deletions hub/management/commands/import_mps.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,6 @@ def get_parliament_ids_df(self):
id_df = id_df.drop(columns=["scheme"])
id_df = id_df.rename(columns={"identifier": "parlid"})

# TODO: Remove this temporary workaround, once February 2024 byelection MPs are added upstream.
patch_df = pd.DataFrame(
[{"twfyid": 26287, "parlid": "5010"}, {"twfyid": 26288, "parlid": "5011"}]
)
id_df = pd.concat([id_df, patch_df])

return id_df

def get_area_map(self):
Expand Down Expand Up @@ -217,17 +211,17 @@ def import_mps(self):
)
continue

if area and "parlid" in mp:
if area and "twfyid" in mp:
name = f"{mp['First name']} {mp['Last name']}"
if pd.isna(mp["parlid"]):
print(f"No parlid for {name}, not updating")
if pd.isna(mp["twfyid"]):
print(f"No twfyid for {name}, not updating")

Check warning on line 217 in hub/management/commands/import_mps.py

View check run for this annotation

Codecov / codecov/patch

hub/management/commands/import_mps.py#L217

Added line #L217 was not covered by tests
continue

try:
person, created = Person.objects.update_or_create(
person_type="MP",
external_id=mp["parlid"],
id_type="parlid",
external_id=mp["twfyid"],
id_type="twfyid",
defaults={
"name": name,
"area": area,
Expand Down Expand Up @@ -294,15 +288,22 @@ def import_mp_images(self):
if not self._quiet:
print("Importing MP Images")
for mp in tqdm(
Person.objects.filter(person_type="MP").all(), disable=self._quiet
PersonData.objects.filter(
data_type__name="parlid", person__person_type="MP"
)
.select_related("person")
.all(),
disable=self._quiet,
):
image_url = f"https://members-api.parliament.uk/api/Members/{mp.external_id}/Thumbnail"
image_url = (
f"https://members-api.parliament.uk/api/Members/{mp.data}/Thumbnail"
)
file, headers = urllib.request.urlretrieve(image_url)
mime_type = magic.from_file(file, mime=True)
extension = mime_type.split("/")[1]
image = File(open(file, "rb"))
mp.photo.save(f"mp_{mp.external_id}.{extension}", image)
mp.save()
mp.person.photo.save(f"mp_{mp.data}.{extension}", image)
mp.person.save()

def check_for_duplicate_mps(self):
duplicates = (
Expand Down
2 changes: 1 addition & 1 deletion hub/tests/test_import_mps.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def test_import(self, id_df_mock, twfy_df_mock, retrieve_mock, request_mock):

first = mps[0]
self.assertEqual(first.name, "James Madeupname")
self.assertEqual(first.external_id, "1")
self.assertEqual(first.external_id, "26")
self.assertRegex(first.photo.url, r"/media/person/mp_1.jpeg")

all_data = PersonData.objects.filter(person=first)
Expand Down

0 comments on commit 93f9527

Please sign in to comment.