Skip to content

Commit

Permalink
csvs: Can download in a ZIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jarofgreen committed Nov 7, 2022
1 parent d2371d0 commit b2946fd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
22 changes: 22 additions & 0 deletions cove_ofds/process.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import os.path
import zipfile

import flattentool
from libcoveofds.additionalfields import AdditionalFields
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions cove_ofds/templates/cove_ofds/explore.html
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ <h4 class="panel-title">
<a href="{{download_ods_url}}">{% trans 'Spreadsheet (ODS)' %} ({{ download_ods_size|filesizeformat }})</a>
</li>
{% endif %}
{% if can_download_csvs_zip %}
<li>
<span class="glyphicon glyphicon-download" aria-hidden="true"></span>
<a href="{{download_csvs_zip_url}}">{% trans 'CSVs in a ZIP file' %} ({{ download_csvs_zip_size|filesizeformat }})</a>
</li>
{% endif %}
</ul>
{% endif %}
</div>
Expand Down

0 comments on commit b2946fd

Please sign in to comment.