From fa8438b594dffa8ed7f3c498f1579cf98d37ec2f Mon Sep 17 00:00:00 2001 From: Struan Donald Date: Wed, 21 Feb 2024 15:49:43 +0000 Subject: [PATCH] standardise party names on MP import TWFY has different names from previous sources so do some standardising. Also add Reclaim to shader options --- hub/management/commands/import_mps.py | 29 ++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/hub/management/commands/import_mps.py b/hub/management/commands/import_mps.py index d67fc5d8a..3f1391997 100644 --- a/hub/management/commands/import_mps.py +++ b/hub/management/commands/import_mps.py @@ -30,9 +30,25 @@ "Sinn Féin": "#326760", "Social Democratic and Labour Party": "#2AA82C", "Speaker of the House of Commons": "#DCDCDC", + "Reclaim": "#101122", "independent politician": "#DCDCDC", } +party_map = { + "Conservative": "Conservative Party", + "Conservative and Unionist Party": "Conservative Party", + "Green": "Green Party", + "Green Party (England & Wales)": "Green Party", + "SDLP (Social Democratic & Labour Party)": "Social Democratic and Labour Party", + "DUP": "Democratic Unionist Party", + "Labour": "Labour Party", + "Labour/Co-operative": "Labour Co-operative", + "Speaker": "Speaker of the House of Commons", + "Liberal Democrat": "Liberal Democrats", + "Alba": "Alba Party", + "Independent": "independent politician", +} + MP_IMPORT_COMMANDS = [ "import_mps_relevant_votes", "import_mps_select_committee_membership", @@ -58,6 +74,16 @@ def handle(self, quiet: bool = False, *args, **options): self.check_for_duplicate_mps() self.import_mp_images() + def standardise_party(self, party): + orig_party = party + if party_shades.get(party, None) is not None: + return party + + if party_map.get(party, None) is not None: + return party_map[party] + + return orig_party + # these are separate functions so we can mock them in tests def get_twfy_df(self): df = pd.read_csv("https://www.theyworkforyou.com/mps/?f=csv").rename( @@ -253,10 +279,11 @@ def import_mps(self): ) if "Party" in mp: try: + party = self.standardise_party(mp["Party"]) PersonData.objects.update_or_create( person=person, data_type=data_types["party"], - defaults={"data": mp["Party"]}, + defaults={"data": party}, ) except PersonData.MultipleObjectsReturned: # pragma: no cover PersonData.objects.filter(