Skip to content

Commit

Permalink
import script for local council control
Browse files Browse the repository at this point in the history
Requires a .env entry for PARTY_CONTROL_URL

Fixes #202
  • Loading branch information
struan committed Jul 1, 2024
1 parent b6a1016 commit fe0235f
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 33 deletions.
18 changes: 18 additions & 0 deletions hub/management/commands/base_importers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,24 @@
RateLimitException,
)

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",
}


class MultipleAreaTypesMixin:
def handle(self, *args, **options):
Expand Down
17 changes: 1 addition & 16 deletions hub/management/commands/import_2024_ppcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,7 @@

from hub.models import Area, DataSet, DataType, Person, PersonData

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",
"Speaker of the House of Commons": "#DCDCDC",
"independent politician": "#DCDCDC",
}
from .base_importers import party_shades


class Command(BaseCommand):
Expand Down
81 changes: 81 additions & 0 deletions hub/management/commands/import_council_control.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
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,
)

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
18 changes: 1 addition & 17 deletions hub/management/commands/import_mps.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,7 @@

from hub.models import Area, DataSet, DataType, Person, PersonData

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",
"Speaker of the House of Commons": "#DCDCDC",
"Reclaim": "#101122",
"independent politician": "#DCDCDC",
}
from .base_importers import party_shades

party_map = {
"Conservative": "Conservative Party",
Expand Down
4 changes: 4 additions & 0 deletions local_intelligence_hub/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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")

Expand Down

0 comments on commit fe0235f

Please sign in to comment.