Skip to content

Commit 1fbe437

Browse files
committed
Add ability to download all existing outputs for a project #1880
Signed-off-by: tdruez <[email protected]>
1 parent 1485a94 commit 1fbe437

File tree

6 files changed

+40
-17
lines changed

6 files changed

+40
-17
lines changed

scanpipe/pipes/output.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,18 +1153,6 @@ def make_zip_from_files(files):
11531153
return zip_buffer
11541154

11551155

1156-
# def to_all_formats(project):
1157-
# """Generate all output formats for a project and return a zipfile."""
1158-
# files = []
1159-
# for output_function in FORMAT_TO_FUNCTION_MAPPING.values():
1160-
# output_file = output_function(project)
1161-
# filename = safe_filename(f"{project.name}_{output_file.name}")
1162-
# files.append((filename, output_file))
1163-
# zip_buffer = make_zip_from_files(files)
1164-
# zip_buffer.name = "scancodeio_output_files.zip"
1165-
# return zip_buffer
1166-
1167-
11681156
def to_all_formats(project):
11691157
"""Generate all output formats for a project and return a Django File-like zip."""
11701158
files = []
@@ -1180,3 +1168,15 @@ def to_all_formats(project):
11801168
zip_file.name = safe_filename(f"{project.name}_outputs.zip")
11811169

11821170
return zip_file
1171+
1172+
1173+
def to_all_outputs(project):
1174+
"""Return a Django File-like zip containing all existing project's output/ files."""
1175+
files = [(path.name, path) for path in project.output_path.glob("*")]
1176+
zip_buffer = make_zip_from_files(files)
1177+
1178+
# Wrap it into a Django File-like object
1179+
zip_file = ContentFile(zip_buffer.getvalue())
1180+
zip_file.name = safe_filename(f"{project.name}_outputs.zip")
1181+
1182+
return zip_file

scanpipe/templates/scanpipe/dropdowns/project_download_dropdown.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<strong>ORT (package-list)</strong>
4242
</a>
4343
<hr class="dropdown-divider" />
44-
<a href="{% url 'project_results' project.slug 'all' %}" class="dropdown-item">
44+
<a href="{% url 'project_results' project.slug 'all_formats' %}" class="dropdown-item">
4545
<strong>All formats</strong>
4646
</a>
4747
</div>

scanpipe/templates/scanpipe/includes/project_downloads.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
</div>
7373
</div>
7474
<span class="p-1">|</span>
75-
<a class="tag is-success is-medium" href="{% url 'project_results' project.slug 'all' %}">
75+
<a class="tag is-success is-medium" href="{% url 'project_results' project.slug 'all_formats' %}">
7676
All formats
7777
</a>
7878
</div>

scanpipe/templates/scanpipe/panels/project_outputs.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,10 @@
1818
</div>
1919
</div>
2020
{% endfor %}
21+
<div class="panel-block">
22+
<a class="button is-link is-outlined is-fullwidth" href="{% url 'project_results' project.slug 'all_outputs' %}">
23+
<span class="icon mr-1"><i class="fa-solid fa-download"></i></span>
24+
Download all outputs
25+
</a>
26+
</div>
2127
</article>

scanpipe/tests/pipes/test_output.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -644,13 +644,28 @@ def test_scanpipe_pipes_outputs_to_all_formats(self):
644644

645645
self.assertEqual("asgiref_outputs.zip", output_file.name)
646646

647-
output_file.seek(0) # Important for reading from start
648647
with zipfile.ZipFile(output_file, "r") as zip_ref:
649648
zip_contents = zip_ref.namelist()
650649
file_count = len(zip_contents)
651650

652651
expected_file_count = len(output.FORMAT_TO_FUNCTION_MAPPING)
653-
self.assertEqual(file_count, expected_file_count)
652+
self.assertEqual(expected_file_count, file_count)
653+
654+
def test_scanpipe_pipes_outputs_to_all_outputs(self):
655+
fixtures = self.data / "asgiref" / "asgiref-3.3.0_fixtures.json"
656+
call_command("loaddata", fixtures, **{"verbosity": 0})
657+
project = Project.objects.get(name="asgiref")
658+
659+
with self.assertNumQueries(0):
660+
output_file = output.to_all_outputs(project=project)
661+
662+
self.assertEqual("asgiref_outputs.zip", output_file.name)
663+
664+
with zipfile.ZipFile(output_file, "r") as zip_ref:
665+
zip_contents = zip_ref.namelist()
666+
file_count = len(zip_contents)
667+
668+
self.assertEqual(len(project.output_root), file_count)
654669

655670
def test_scanpipe_pipes_outputs_make_unknown_license_object(self):
656671
licensing = get_licensing()

scanpipe/views.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1630,8 +1630,10 @@ def get(self, request, *args, **kwargs):
16301630
output_file = output.to_attribution(project)
16311631
elif format == "ort-package-list":
16321632
output_file = output.to_ort_package_list_yml(project)
1633-
elif format == "all":
1633+
elif format == "all_formats":
16341634
output_file = output.to_all_formats(project)
1635+
elif format == "all_outputs":
1636+
output_file = output.to_all_outputs(project)
16351637
else:
16361638
raise Http404("Format not supported.")
16371639

0 commit comments

Comments
 (0)