Skip to content

Commit

Permalink
update mp relevant votes import to not use external_id
Browse files Browse the repository at this point in the history
  • Loading branch information
struan committed Feb 20, 2024
1 parent bdff481 commit e0b5f27
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions hub/management/commands/import_mps_relevant_votes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from functools import cache

from django.core.management.base import BaseCommand

import requests
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -76,16 +89,19 @@ 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",
date__gt=date,
).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")
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit e0b5f27

Please sign in to comment.