Skip to content

Commit ae641a9

Browse files
committed
add new behavior in ExportJSONMixin
Signed-off-by: tdruez <tdruez@aboutcode.org>
1 parent ed75eb9 commit ae641a9

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

scanpipe/tests/test_views.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,6 +1505,10 @@ def test_project_packages_export_json(self):
15051505

15061506
self.assertIsInstance(response, FileResponse)
15071507
self.assertEqual(response.get("Content-Type"), "application/json")
1508+
self.assertTrue(response.get("Content-Disposition").startswith("inline"))
1509+
1510+
with override_settings(SCANPIPE={"INLINE_DOWNLOAD_MAX_SIZE": 0}):
1511+
response = self.client.get(url + "?export_json=True")
15081512
self.assertTrue(response.get("Content-Disposition").startswith("attachment"))
15091513

15101514
file_content = b"".join(response.streaming_content).decode("utf-8")
@@ -1570,7 +1574,7 @@ def test_project_dependencies_export_json(self):
15701574

15711575
self.assertIsInstance(response, FileResponse)
15721576
self.assertEqual(response.get("Content-Type"), "application/json")
1573-
self.assertTrue(response.get("Content-Disposition").startswith("attachment"))
1577+
self.assertTrue(response.get("Content-Disposition").startswith("inline"))
15741578

15751579
file_content = b"".join(response.streaming_content).decode("utf-8")
15761580
json_data = json.loads(file_content)
@@ -1606,7 +1610,7 @@ def test_project_relations_export_json(self):
16061610

16071611
self.assertIsInstance(response, FileResponse)
16081612
self.assertEqual(response.get("Content-Type"), "application/json")
1609-
self.assertTrue(response.get("Content-Disposition").startswith("attachment"))
1613+
self.assertTrue(response.get("Content-Disposition").startswith("inline"))
16101614

16111615
file_content = b"".join(response.streaming_content).decode("utf-8")
16121616
json_data = json.loads(file_content)
@@ -1630,7 +1634,7 @@ def test_project_messages_export_json(self):
16301634

16311635
self.assertIsInstance(response, FileResponse)
16321636
self.assertEqual(response.get("Content-Type"), "application/json")
1633-
self.assertTrue(response.get("Content-Disposition").startswith("attachment"))
1637+
self.assertTrue(response.get("Content-Disposition").startswith("inline"))
16341638

16351639
file_content = b"".join(response.streaming_content).decode("utf-8")
16361640
json_data = json.loads(file_content)
@@ -1656,7 +1660,7 @@ def test_project_codebase_resources_export_json(self):
16561660

16571661
self.assertIsInstance(response, FileResponse)
16581662
self.assertEqual(response.get("Content-Type"), "application/json")
1659-
self.assertTrue(response.get("Content-Disposition").startswith("attachment"))
1663+
self.assertTrue(response.get("Content-Disposition").startswith("inline"))
16601664

16611665
file_content = b"".join(response.streaming_content).decode("utf-8")
16621666
json_data = json.loads(file_content)

scanpipe/views.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,11 +540,13 @@ def export_json_file_response(self):
540540
serializer = serializer_class(queryset, many=True)
541541
serialized_data = json.dumps(serializer.data, indent=2, cls=DjangoJSONEncoder)
542542

543-
output_file = io.BytesIO(serialized_data.encode("utf-8"))
543+
encoded_data = serialized_data.encode("utf-8")
544+
output_file = io.BytesIO(encoded_data)
545+
is_too_large = len(encoded_data) > scanpipe_settings.INLINE_DOWNLOAD_MAX_SIZE
544546

545547
return FileResponse(
546548
output_file,
547-
as_attachment=True,
549+
as_attachment=is_too_large,
548550
filename=self.get_export_json_filename(),
549551
content_type="application/json",
550552
)

0 commit comments

Comments
 (0)