From b2946fd765be09d420fae7fb3c868059ca092ccb Mon Sep 17 00:00:00 2001 From: James Date: Mon, 7 Nov 2022 11:34:23 +0000 Subject: [PATCH] csvs: Can download in a ZIP https://github.com/Open-Telecoms-Data/cove-ofds/issues/16 --- cove_ofds/process.py | 22 ++++++++++++++++++++++ cove_ofds/templates/cove_ofds/explore.html | 6 ++++++ 2 files changed, 28 insertions(+) diff --git a/cove_ofds/process.py b/cove_ofds/process.py index 7d893fb..353f076 100644 --- a/cove_ofds/process.py +++ b/cove_ofds/process.py @@ -1,5 +1,6 @@ import json import os.path +import zipfile import flattentool from libcoveofds.additionalfields import AdditionalFields @@ -279,6 +280,12 @@ def get_context(self): class ConvertJSONIntoSpreadsheets(ProcessDataTask): """Convert primary format (JSON) to spreadsheets""" + def __init__(self, supplied_data): + super().__init__(supplied_data) + self.csvs_zip_filename = os.path.join( + self.supplied_data.data_dir(), "flatten", "flattened.csvs.zip" + ) + def process(self, process_data: dict) -> dict: # TODO don't run if already done @@ -292,6 +299,12 @@ def process(self, process_data: dict) -> dict: flattentool.flatten(process_data["json_data_filename"], **flatten_kwargs) + # Make Zip file of all CSV files + with zipfile.ZipFile(self.csvs_zip_filename, "w") as out_zip: + for f in os.listdir(output_dir): + if os.path.isfile(os.path.join(output_dir, f)) and f.endswith(".csv"): + out_zip.write(os.path.join(output_dir, f), arcname=f) + return process_data def get_context(self): @@ -320,6 +333,15 @@ def get_context(self): context["download_ods_size"] = os.stat(ods_filename).st_size else: context["can_download_ods"] = False + # CSVs + if os.path.exists(self.csvs_zip_filename): + context["can_download_csvs_zip"] = True + context["download_csvs_zip_url"] = os.path.join( + self.supplied_data.data_url(), "flatten", "flattened.csvs.zip" + ) + context["download_csvs_zip_size"] = os.stat(ods_filename).st_size + else: + context["can_download_csvs_zip"] = False # done! return context diff --git a/cove_ofds/templates/cove_ofds/explore.html b/cove_ofds/templates/cove_ofds/explore.html index 99d6341..08429e1 100644 --- a/cove_ofds/templates/cove_ofds/explore.html +++ b/cove_ofds/templates/cove_ofds/explore.html @@ -99,6 +99,12 @@

{% trans 'Spreadsheet (ODS)' %} ({{ download_ods_size|filesizeformat }}) {% endif %} + {% if can_download_csvs_zip %} +
  • + + {% trans 'CSVs in a ZIP file' %} ({{ download_csvs_zip_size|filesizeformat }}) +
  • + {% endif %} {% endif %}