Skip to content

Commit

Permalink
Working ag grid export
Browse files Browse the repository at this point in the history
  • Loading branch information
nboyse committed Feb 6, 2024
1 parent 61b4d91 commit 96616c7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions reports/reports/eu_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
17 changes: 13 additions & 4 deletions reports/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])

Expand Down

0 comments on commit 96616c7

Please sign in to comment.