From 601b0cabf6b396c64c01e511f2a9e6e733892f3c Mon Sep 17 00:00:00 2001 From: Struan Donald Date: Mon, 16 Sep 2024 15:51:23 +0200 Subject: [PATCH] Great Big Green Week 2024 import Add a generic processing command to turn the spreadsheet of events into one with constituencies. Add a new command for the 2024 events. Fixes #593 --- hub/management/commands/base_generators.py | 6 ++- hub/management/commands/base_importers.py | 5 ++- hub/management/commands/generate_gbgw_data.py | 28 ++++++++++++ .../commands/import_gbgw_events_24.py | 43 +++++++++++++++++++ 4 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 hub/management/commands/generate_gbgw_data.py create mode 100644 hub/management/commands/import_gbgw_events_24.py diff --git a/hub/management/commands/base_generators.py b/hub/management/commands/base_generators.py index 427e193cf..e2f031d03 100644 --- a/hub/management/commands/base_generators.py +++ b/hub/management/commands/base_generators.py @@ -24,7 +24,7 @@ "MTD": "STC", "NMD": "DIS", "DIS": "DIS", - "WMC": "WMC", + "WMC": "WMC23", "WMCF": "WMC23", } @@ -166,12 +166,16 @@ def add_arguments(self, parser): "-i", "--ignore", action="store_true", help="Ignore existing data file" ) + def _setup(self): + pass + def handle(self, quiet=False, ignore=False, *args, **options): self._quiet = quiet if not self._quiet: self.stdout.write(self.message) self._ignore = ignore + self._setup(*args, **options) df = self.get_dataframe() out_df = self.process_data(df) self.save_data(out_df) diff --git a/hub/management/commands/base_importers.py b/hub/management/commands/base_importers.py index 1107afc7f..e0c426100 100644 --- a/hub/management/commands/base_importers.py +++ b/hub/management/commands/base_importers.py @@ -131,6 +131,7 @@ def add_data_sets(self, df=None): def _fill_empty_entries(self): for data_type in self.data_types.values(): + datum_example = AreaData.objects.filter(data_type=data_type).first() if ( data_type.data_type in [ @@ -140,8 +141,8 @@ def _fill_empty_entries(self): ] and data_type.data_set.table == "areadata" and data_type.data_set.fill_blanks + and datum_example is not None ): - datum_example = AreaData.objects.filter(data_type=data_type).first() if datum_example.float: key = "float" elif datum_example.int: @@ -428,7 +429,7 @@ def process_data(self, df): def handle(self, quiet=False, *args, **options): self._quiet = quiet df = self.get_dataframe() - if not df: + if df.empty: if not self._quiet: self.stdout.write(f"missing data for {self.message} ({self.area_type})") return diff --git a/hub/management/commands/generate_gbgw_data.py b/hub/management/commands/generate_gbgw_data.py new file mode 100644 index 000000000..937b10824 --- /dev/null +++ b/hub/management/commands/generate_gbgw_data.py @@ -0,0 +1,28 @@ +from django.conf import settings + +from .base_generators import BaseLatLonGeneratorCommand + + +class Command(BaseLatLonGeneratorCommand): + help = "Generate CSV file of GBGW events with area name'" + message = "Generating a CSV of areas for GBGW events" + + row_name = "Organisation" + + def add_arguments(self, parser): + super().add_arguments(parser) + + parser.add_argument( + "--year", + required=True, + action="store", + help="The last two digits of the year for the events, e.g 24 for 2024", + ) + + def _setup(self, *args, **kwargs): + year = kwargs["year"] + self.data_file = settings.BASE_DIR / "data" / f"gbgw_events_{year}.csv" + self.out_file = settings.BASE_DIR / "data" / f"gbgw_events_{year}_processed.csv" + + def get_location_from_row(self, row): + return {"lat_lon": [row.Lat, row.Long]} diff --git a/hub/management/commands/import_gbgw_events_24.py b/hub/management/commands/import_gbgw_events_24.py new file mode 100644 index 000000000..268edbc74 --- /dev/null +++ b/hub/management/commands/import_gbgw_events_24.py @@ -0,0 +1,43 @@ +from django.conf import settings + +from hub.models import DataSet + +from .base_importers import BaseConstituencyCountImportCommand, MultipleAreaTypesMixin + + +class Command(MultipleAreaTypesMixin, BaseConstituencyCountImportCommand): + help = "Import data about number of GBGW events in 2024 per constituency" + message = "Importing 2024 GBGW events" + uses_gss = False + + data_file = settings.BASE_DIR / "data" / "gbgw_events_24_processed.csv" + + area_types = ["WMC23", "STC", "DIS"] + cons_col_map = { + "WMC23": "WMC23", + "STC": "STC", + "DIS": "DIS", + } + + data_sets = { + "constituency_gbgw_2024_event_count": { + "defaults": { + "label": "Number of Great Big Green Week 2024 events", + "description": "Number of public Great Big Green week 2024 events held in the area. This exludes any private events that were held.", + "release_date": "September 2024", + "data_type": "integer", + "category": "movement", + "subcategory": "events", + "source_label": "Data from The Climate Coalition.", + "source": "https://greatbiggreenweek.com/", + "source_type": "google sheet", + "data_url": "", + "table": "areadata", + "is_public": True, + "default_value": 10, + "comparators": DataSet.numerical_comparators(), + "unit_type": "raw", + "unit_distribution": "people_in_area", + } + } + }