Skip to content

Commit

Permalink
Implement proper format selection #1518
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <[email protected]>
  • Loading branch information
tdruez committed Jan 7, 2025
1 parent 8f9b5d6 commit b6cd12a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
6 changes: 4 additions & 2 deletions scanpipe/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ class ArchiveProjectForm(forms.Form):


class ProjectOutputDownloadForm(forms.Form):
output_formats = forms.MultipleChoiceField(
output_format = forms.ChoiceField(
label="Choose the output format to include in the ZIP file",
choices=[
("json", "JSON"),
("xlsx", "XLSX"),
Expand All @@ -266,7 +267,8 @@ class ProjectOutputDownloadForm(forms.Form):
("attribution", "Attribution"),
],
required=True,
widget=forms.CheckboxSelectMultiple,
initial="json",
widget=forms.RadioSelect,
)


Expand Down
9 changes: 9 additions & 0 deletions scanpipe/pipes/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -898,3 +898,12 @@ def to_attribution(project):

output_file.write_text(rendered_template)
return output_file


FORMAT_TO_FUNCTION_MAPPING = {
"json": to_json,
"xlsx": to_xlsx,
"spdx": to_spdx,
"cyclonedx": to_cyclonedx,
"attribution": to_attribution,
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
</header>
<form action="{% url 'project_action' %}" method="post" id="download-projects-form">{% csrf_token %}
<section class="modal-card-body">
<p class="mb-2">
Choose the formats to include in the ZIP file:
</p>
<ul class="mb-5">
<ul class="mb-3">
{{ outputs_download_form.as_ul }}
</ul>
</section>
Expand Down
8 changes: 4 additions & 4 deletions scanpipe/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1230,20 +1230,20 @@ def download_outputs_zip_response(self):
if not outputs_download_form.is_valid():
return HttpResponseRedirect(self.success_url)

# TODO:
# output_formats = outputs_download_form.cleaned_data["output_formats"]
output_format = outputs_download_form.cleaned_data["output_format"]
output_function = output.FORMAT_TO_FUNCTION_MAPPING.get(output_format)
projects = self.get_projects_queryset()

# In-memory file storage for the zip archive
zip_buffer = io.BytesIO()
with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zip_file:
for project in projects:
output_file = output.to_xlsx(project)
output_file = output_function(project)
filename = output.safe_filename(f"{project.name}_{output_file.name}")
with open(output_file, "rb") as f:
zip_file.writestr(filename, f.read())

zip_buffer.seek(0) # Move the buffer's cursor to the beginning
zip_buffer.seek(0)
return FileResponse(
zip_buffer,
as_attachment=True,
Expand Down

0 comments on commit b6cd12a

Please sign in to comment.