diff --git a/reports/reports/eu_data.py b/reports/reports/eu_data.py index 5e6f623aa..add32ac55 100644 --- a/reports/reports/eu_data.py +++ b/reports/reports/eu_data.py @@ -39,6 +39,7 @@ def headers(self) -> [dict]: for header in self.headers_list ] + def row(self, row) -> [dict]: return { field.replace("_", " ").capitalize(): str(getattr(row, field, None)) diff --git a/reports/views.py b/reports/views.py index 635ab4ebe..ea1b8cca5 100644 --- a/reports/views.py +++ b/reports/views.py @@ -115,12 +115,21 @@ def export_report_to_csv(request, report_slug, current_tab=None): writer = csv.writer(response) - # Check if the report is a table or a chart - if hasattr(report_instance, "headers"): - # For table reports - writer.writerow([header["text"] for header in headers]) + # For ag grid reports + if hasattr(report_instance, "headers_list"): + header_row = [header.replace("_", " ").capitalize() for header in report_instance.headers_list] + writer.writerow(header_row) + for row in report_instance.rows(): + data_row = [str(row[header.replace("_", " ").capitalize()]) for header in report_instance.headers_list] + writer.writerow(data_row) + + # For govuk table reports + elif hasattr(report_instance, "headers"): + writer.writerow([header.get("text", None) for header in headers]) for row in rows: writer.writerow([column["text"] for column in row]) + + # For charts else: writer.writerow(["Date", "Data"])