From a18320f5e69898d51ffd5e4c04000b504021f90d Mon Sep 17 00:00:00 2001 From: Struan Donald Date: Mon, 24 Jun 2024 15:04:57 +0100 Subject: [PATCH] import script for local council control Requires a .env entry for PARTY_CONTROL_URL Fixes #202 --- .../commands/import_council_control.py | 95 +++++++++++++++++++ local_intelligence_hub/settings.py | 4 + 2 files changed, 99 insertions(+) create mode 100644 hub/management/commands/import_council_control.py diff --git a/hub/management/commands/import_council_control.py b/hub/management/commands/import_council_control.py new file mode 100644 index 000000000..33a7830b4 --- /dev/null +++ b/hub/management/commands/import_council_control.py @@ -0,0 +1,95 @@ +from datetime import date + +from django.conf import settings + +import pandas as pd + +from hub.import_utils import filter_authority_type +from hub.models import DataSet + +from .base_importers import BaseImportFromDataFrameCommand, MultipleAreaTypesMixin + +party_shades = { + "Alba Party": "#005EB8", + "Alliance Party of Northern Ireland": "#F6CB2F", + "Conservative Party": "#0087DC", + "Democratic Unionist Party": "#D46A4C", + "Green Party": "#6AB023", + "Labour Co-operative": "#E4003B", + "Labour Party": "#E4003B", + "Liberal Democrats": "#FAA61A", + "Plaid Cymru": "#005B54", + "Scottish National Party": "#FDF38E", + "Sinn Féin": "#326760", + "Social Democratic and Labour Party": "#2AA82C", + "Reclaim": "#101122", + "No overall control": "#EEE", + "Independents": "#DCDCDC", +} + +party_map = { + "CON": "Conservative Party", + "LAB": "Labout Party", + "LD": "Liberal Democrats", + "PC": "Plaid Cymru", + "SNP": "Scottish National Party", + "GRN": "Green Party", + "IND": "Independents", + "NOC": "No overall control", +} + + +class Command(MultipleAreaTypesMixin, BaseImportFromDataFrameCommand): + cons_row = "lua code" + message = "Importing council control" + uses_gss = True + do_not_convert = True + + area_types = ["STC", "DIS"] + + defaults = { + "data_type": "text", + "category": "place", + "subcategory": "", + "release_date": str(date.today()), + "label": "Council Control", + "source_label": "Data from Open Council Data UK.", + "source": "http://opencouncildata.co.uk/", + "source_type": "api", + "table": "areadata", + "default_value": "", + "data_url": "", + "comparators": DataSet.in_comparators(), + "unit_type": "raw", + "unit_distribution": "", + "is_shadable": True, + "is_filterable": True, + "options": [ + {"title": party, "shader": shade} for party, shade in party_shades.items() + ], + "is_public": True, + } + + data_sets = { + "council_control": { + "defaults": defaults, + "col": "majority", + }, + } + + def get_row_data(self, row, conf): + party = party_map.get(row[conf["col"]]) + + if party is None: + party = row[conf["col"]] + self.stderr.write(f"No party map for {conf['col']}") + + return party + + def get_dataframe(self): + if settings.PARTY_CONTROL_URL == "": + return None + + df = pd.read_csv(settings.PARTY_CONTROL_URL) + df = filter_authority_type(df, self.area_type, "lua code") + return df diff --git a/local_intelligence_hub/settings.py b/local_intelligence_hub/settings.py index 7f47629f7..833798f4b 100644 --- a/local_intelligence_hub/settings.py +++ b/local_intelligence_hub/settings.py @@ -32,6 +32,7 @@ MAILCHIMP_TCC_KEY=(str, ""), MAILCHIMP_TCC_SERVER_PREFIX=(str, ""), MAILCHIMP_TCC_LIST_ID=(str, ""), + PARTY_CONTROL_URL=(str, ""), ) environ.Env.read_env(BASE_DIR / ".env") @@ -58,6 +59,9 @@ MAILCHIMP_TCC_SERVER_PREFIX = env("MAILCHIMP_TCC_SERVER_PREFIX") MAILCHIMP_TCC_LIST_ID = env("MAILCHIMP_TCC_LIST_ID") +# import config +PARTY_CONTROL_URL = env("PARTY_CONTROL_URL") + # make sure CSRF checking still works behind load balancers SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")