|
47 | 47 | from scanpipe.models import Project |
48 | 48 | from scanpipe.models import Run |
49 | 49 | from scanpipe.models import RunInProgressError |
| 50 | +from scanpipe.pipes import output |
50 | 51 | from scanpipe.views import project_results_json_response |
51 | 52 |
|
52 | 53 | scanpipe_app = apps.get_app_config("scanpipe") |
53 | 54 |
|
54 | 55 |
|
55 | | -class PassThroughRenderer(renderers.BaseRenderer): |
56 | | - media_type = "" |
57 | | - |
58 | | - def render(self, data, **kwargs): |
59 | | - return data |
60 | | - |
61 | | - |
62 | 56 | class ProjectFilterSet(django_filters.rest_framework.FilterSet): |
63 | 57 | name = django_filters.CharFilter() |
64 | 58 | name__contains = django_filters.CharFilter( |
@@ -140,12 +134,32 @@ def results(self, request, *args, **kwargs): |
140 | 134 | """ |
141 | 135 | return project_results_json_response(self.get_object()) |
142 | 136 |
|
143 | | - @action( |
144 | | - detail=True, name="Results (download)", renderer_classes=[PassThroughRenderer] |
145 | | - ) |
| 137 | + @action(detail=True, name="Results (download)") |
146 | 138 | def results_download(self, request, *args, **kwargs): |
147 | | - """Return the results as an attachment.""" |
148 | | - return project_results_json_response(self.get_object(), as_attachment=True) |
| 139 | + """Return the results in the provided `output_format` as an attachment.""" |
| 140 | + project = self.get_object() |
| 141 | + format = request.query_params.get("output_format", "json") |
| 142 | + |
| 143 | + if format == "json": |
| 144 | + return project_results_json_response(project, as_attachment=True) |
| 145 | + elif format == "xlsx": |
| 146 | + output_file = output.to_xlsx(project) |
| 147 | + elif format == "spdx": |
| 148 | + output_file = output.to_spdx(project) |
| 149 | + elif format == "cyclonedx": |
| 150 | + output_file = output.to_cyclonedx(project) |
| 151 | + elif format == "attribution": |
| 152 | + output_file = output.to_attribution(project) |
| 153 | + else: |
| 154 | + message = {"status": f"Format {format} not supported."} |
| 155 | + return Response(message, status=status.HTTP_400_BAD_REQUEST) |
| 156 | + |
| 157 | + filename = output.safe_filename(f"scancodeio_{project.name}_{output_file.name}") |
| 158 | + return FileResponse( |
| 159 | + output_file.open("rb"), |
| 160 | + filename=filename, |
| 161 | + as_attachment=True, |
| 162 | + ) |
149 | 163 |
|
150 | 164 | @action(detail=True) |
151 | 165 | def summary(self, request, *args, **kwargs): |
|
0 commit comments