From 3a8618f7c3fc8928bd4856722a04755ce2d10d48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Sun, 9 Apr 2023 12:07:31 +0800 Subject: [PATCH] Fixed the csv_download function when downloading data with non-US-ASCII filenames in the "accounting.report.utils.csv_export" module. --- src/accounting/report/utils/csv_export.py | 3 ++- tests/test_report.py | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/accounting/report/utils/csv_export.py b/src/accounting/report/utils/csv_export.py index ff77576..613df51 100644 --- a/src/accounting/report/utils/csv_export.py +++ b/src/accounting/report/utils/csv_export.py @@ -22,6 +22,7 @@ from abc import ABC, abstractmethod from datetime import timedelta, date from decimal import Decimal from io import StringIO +from urllib.parse import quote from flask import Response @@ -53,7 +54,7 @@ def csv_download(filename: str, rows: list[BaseCSVRow]) -> Response: fp.seek(0) response: Response = Response(fp.read(), mimetype="text/csv") response.headers["Content-Disposition"] \ - = f"attachment; filename={filename}" + = f"attachment; filename={quote(filename)}" return response diff --git a/tests/test_report.py b/tests/test_report.py index f14e187..0d34317 100644 --- a/tests/test_report.py +++ b/tests/test_report.py @@ -132,6 +132,12 @@ class ReportTestCase(unittest.TestCase): response = client.get(f"{PREFIX}/search?q=Salary&as=csv") self.assertEqual(response.status_code, 403) + response = client.get(f"{PREFIX}/search?q=薪水") + self.assertEqual(response.status_code, 403) + + response = client.get(f"{PREFIX}/search?q=薪水&as=csv") + self.assertEqual(response.status_code, 403) + def test_viewer(self) -> None: """Test the permission as viewer. @@ -221,6 +227,14 @@ class ReportTestCase(unittest.TestCase): self.assertEqual(response.headers["Content-Type"], "text/csv; charset=utf-8") + response = client.get(f"{PREFIX}/search?q=薪水") + self.assertEqual(response.status_code, 200) + + response = client.get(f"{PREFIX}/search?q=薪水&as=csv") + self.assertEqual(response.status_code, 200) + self.assertEqual(response.headers["Content-Type"], + "text/csv; charset=utf-8") + def test_editor(self) -> None: """Test the permission as editor. @@ -310,6 +324,14 @@ class ReportTestCase(unittest.TestCase): self.assertEqual(response.headers["Content-Type"], "text/csv; charset=utf-8") + response = self.client.get(f"{PREFIX}/search?q=薪水") + self.assertEqual(response.status_code, 200) + + response = self.client.get(f"{PREFIX}/search?q=薪水&as=csv") + self.assertEqual(response.status_code, 200) + self.assertEqual(response.headers["Content-Type"], + "text/csv; charset=utf-8") + def test_empty_db(self) -> None: """Tests the empty database.