diff --git a/explorer/actions.py b/explorer/actions.py index 5b90a289..7972b9b0 100644 --- a/explorer/actions.py +++ b/explorer/actions.py @@ -21,7 +21,7 @@ def generate_report(modeladmin, request, queryset): content_type=queries["content_type"] ) response["Content-Disposition"] = queries["filename"] - response["Content-Length"] = queries["length"] + response["Content-Length"] = len(response.content) return response generate_report.short_description = description @@ -43,7 +43,6 @@ def _package(queries): is_one and CSVExporter(queries[0]).get_output() ) or _build_zip(queries) - ret["length"] = (is_one and len(ret["data"]) or ret["data"].blksize) return ret diff --git a/explorer/tests/test_actions.py b/explorer/tests/test_actions.py index c4018d30..cc24d112 100644 --- a/explorer/tests/test_actions.py +++ b/explorer/tests/test_actions.py @@ -1,4 +1,5 @@ import io +from unittest.mock import patch from zipfile import ZipFile from django.test import TestCase @@ -16,6 +17,16 @@ def test_single_query_is_csv_file(self): fn = generate_report_action() result = fn(None, None, [r, ]) self.assertEqual(result.content.lower().decode("utf-8-sig"), expected_csv) + self.assertEqual(int(result["Content-Length"]), len(result.content)) + + @patch("explorer.actions.CSVExporter.get_output") + def test_single_query_with_unicode_has_byte_content_length(self, get_output): + get_output.return_value = "name\r\nЗавершен\r\n" + + result = generate_report_action()(None, None, [SimpleQueryFactory()]) + + self.assertEqual(int(result["Content-Length"]), len(result.content)) + self.assertGreater(len(result.content), len(get_output.return_value)) def test_multiple_queries_are_zip_file(self): @@ -32,6 +43,7 @@ def test_multiple_queries_are_zip_file(self): self.assertEqual(len(z.namelist()), 2) self.assertEqual(z.namelist()[0], f"{q.title}.csv") self.assertEqual(got_csv.lower().decode("utf-8-sig"), expected_csv) + self.assertEqual(int(res["Content-Length"]), len(res.content)) # if commas are not removed from the filename, then Chrome throws # "duplicate headers received from server"