Fixed the csv_download function when downloading data with non-US-ASCII filenames in the "accounting.report.utils.csv_export" module.

This commit is contained in:
依瑪貓 2023-04-09 12:07:31 +08:00
parent 5d87205659
commit 3a8618f7c3
2 changed files with 24 additions and 1 deletions

View File

@ -22,6 +22,7 @@ from abc import ABC, abstractmethod
from datetime import timedelta, date from datetime import timedelta, date
from decimal import Decimal from decimal import Decimal
from io import StringIO from io import StringIO
from urllib.parse import quote
from flask import Response from flask import Response
@ -53,7 +54,7 @@ def csv_download(filename: str, rows: list[BaseCSVRow]) -> Response:
fp.seek(0) fp.seek(0)
response: Response = Response(fp.read(), mimetype="text/csv") response: Response = Response(fp.read(), mimetype="text/csv")
response.headers["Content-Disposition"] \ response.headers["Content-Disposition"] \
= f"attachment; filename={filename}" = f"attachment; filename={quote(filename)}"
return response return response

View File

@ -132,6 +132,12 @@ class ReportTestCase(unittest.TestCase):
response = client.get(f"{PREFIX}/search?q=Salary&as=csv") response = client.get(f"{PREFIX}/search?q=Salary&as=csv")
self.assertEqual(response.status_code, 403) 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: def test_viewer(self) -> None:
"""Test the permission as viewer. """Test the permission as viewer.
@ -221,6 +227,14 @@ class ReportTestCase(unittest.TestCase):
self.assertEqual(response.headers["Content-Type"], self.assertEqual(response.headers["Content-Type"],
"text/csv; charset=utf-8") "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: def test_editor(self) -> None:
"""Test the permission as editor. """Test the permission as editor.
@ -310,6 +324,14 @@ class ReportTestCase(unittest.TestCase):
self.assertEqual(response.headers["Content-Type"], self.assertEqual(response.headers["Content-Type"],
"text/csv; charset=utf-8") "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: def test_empty_db(self) -> None:
"""Tests the empty database. """Tests the empty database.