diff --git a/scanpipe/forms.py b/scanpipe/forms.py index c7a2fe7c7..7f9a0298c 100644 --- a/scanpipe/forms.py +++ b/scanpipe/forms.py @@ -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"), @@ -266,7 +267,8 @@ class ProjectOutputDownloadForm(forms.Form): ("attribution", "Attribution"), ], required=True, - widget=forms.CheckboxSelectMultiple, + initial="json", + widget=forms.RadioSelect, ) diff --git a/scanpipe/pipes/output.py b/scanpipe/pipes/output.py index a71789811..48787a9d7 100644 --- a/scanpipe/pipes/output.py +++ b/scanpipe/pipes/output.py @@ -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, +} diff --git a/scanpipe/templates/scanpipe/modals/projects_download_modal.html b/scanpipe/templates/scanpipe/modals/projects_download_modal.html index f96542be4..2bab0c761 100644 --- a/scanpipe/templates/scanpipe/modals/projects_download_modal.html +++ b/scanpipe/templates/scanpipe/modals/projects_download_modal.html @@ -7,10 +7,7 @@
{% csrf_token %} diff --git a/scanpipe/views.py b/scanpipe/views.py index 3e99ec5c5..55ff2295c 100644 --- a/scanpipe/views.py +++ b/scanpipe/views.py @@ -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,