From 16e21e64649e1d440c90bedb0f8a4db05637829f Mon Sep 17 00:00:00 2001 From: Struan Donald Date: Tue, 20 Feb 2024 17:46:56 +0000 Subject: [PATCH] update mp relevant votes import to not use external_id --- .../commands/import_mps_relevant_votes.py | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/hub/management/commands/import_mps_relevant_votes.py b/hub/management/commands/import_mps_relevant_votes.py index 26a8bd831..74f07ba82 100644 --- a/hub/management/commands/import_mps_relevant_votes.py +++ b/hub/management/commands/import_mps_relevant_votes.py @@ -1,3 +1,5 @@ +from functools import cache + from django.core.management.base import BaseCommand import requests @@ -30,6 +32,15 @@ def handle(self, quiet=False, *args, **options): self.delete_data() self.import_results(votes, edms) + @cache + def get_parlid_lookup(self): + lookup = {} + + for parlid in PersonData.objects.filter(data_type__name="parlid"): + lookup[parlid.person_id] = parlid.value() + + return lookup + def get_votes(self, division_id): api_url = self.vote_api_url + str(division_id) + ".json" response = requests.get(api_url) @@ -52,8 +63,10 @@ def get_votes(self, division_id): for member in data["NoVoteRecorded"]: vote_dict[str(member["MemberId"])] = "Did not vote" + parlid_lookup = self.get_parlid_lookup() + not_in_office_members = [ - str(person_data.person.external_id) + str(parlid_lookup[person_data.person_id]) for person_data in PersonData.objects.filter( data_type__name="mp_first_elected" ).filter(data__gt=date) @@ -76,8 +89,11 @@ def get_edm(self, edm_id): edm_name = data["Title"] date = data["DateTabled"] supporters = [str(member["MemberId"]) for member in data["Sponsors"]] + + parlid_lookup = self.get_parlid_lookup() + not_in_office = [ - str(person_data.person.external_id) + str(parlid_lookup[person_data.person_id]) for person_data in PersonData.objects.filter( person__person_type="MP", data_type__name="mp_first_elected", @@ -85,7 +101,7 @@ def get_edm(self, edm_id): ).select_related("person") ] all_members = [ - str(person.external_id) + str(parlid_lookup[person.id]) for person in Person.objects.filter( person_type="MP", end_date__isnull=True ).order_by("external_id") @@ -216,8 +232,11 @@ def create_data_types(self, votes, edms): def import_results(self, votes, edms): if not self._quiet: print("Adding MP data on relevant votes + EDMS to database") + + parlid_lookup = self.get_parlid_lookup() + for mp in tqdm(Person.objects.filter(person_type="MP"), disable=self._quiet): - mp_id = mp.external_id + mp_id = parlid_lookup[mp.id] for vote in votes: if mp_id in vote: