From b145d0a124998e422a98fa58356dd700276762bb Mon Sep 17 00:00:00 2001 From: Thomas Druez Date: Wed, 16 Aug 2023 12:08:27 +0200 Subject: [PATCH] Simplify the XLSX output code for CodebaseRelation list view #858 Signed-off-by: Thomas Druez --- scanpipe/api/serializers.py | 4 +- scanpipe/models.py | 11 +++++ .../includes/pagination_header_relations.html | 4 +- scanpipe/views.py | 42 ++----------------- 4 files changed, 21 insertions(+), 40 deletions(-) diff --git a/scanpipe/api/serializers.py b/scanpipe/api/serializers.py index be773bb31..6e7cd5cdb 100644 --- a/scanpipe/api/serializers.py +++ b/scanpipe/api/serializers.py @@ -385,9 +385,11 @@ class CodebaseRelationSerializer(serializers.ModelSerializer): class Meta: model = CodebaseRelation fields = [ - "from_resource", "to_resource", + "status", "map_type", + "score", + "from_resource", ] diff --git a/scanpipe/models.py b/scanpipe/models.py index e4ec33cb5..a3cb26fe6 100644 --- a/scanpipe/models.py +++ b/scanpipe/models.py @@ -2251,6 +2251,17 @@ class Meta: def __str__(self): return f"{self.from_resource.pk} > {self.to_resource.pk} using {self.map_type}" + @property + def status(self): + return self.to_resource.status + + @property + def score(self): + score = self.extra_data.get("path_score", "") + if diff_ratio := self.extra_data.get("diff_ratio", ""): + score += f" diff_ratio: {diff_ratio}" + return score + class VulnerabilityMixin(models.Model): """Add the vulnerability related fields and methods.""" diff --git a/scanpipe/templates/scanpipe/includes/pagination_header_relations.html b/scanpipe/templates/scanpipe/includes/pagination_header_relations.html index 0468ffa3b..c2ab655f2 100644 --- a/scanpipe/templates/scanpipe/includes/pagination_header_relations.html +++ b/scanpipe/templates/scanpipe/includes/pagination_header_relations.html @@ -2,7 +2,9 @@ {% load humanize %} {% block paginator_count %} - {{ paginator.count|intcomma }} relations + + {{ paginator.count|intcomma }} relations + Un-mapped to/ resources diff --git a/scanpipe/views.py b/scanpipe/views.py index 472949b90..695e7ff31 100644 --- a/scanpipe/views.py +++ b/scanpipe/views.py @@ -25,7 +25,6 @@ import json import operator from collections import Counter -from collections import namedtuple from contextlib import suppress from pathlib import Path @@ -1228,12 +1227,6 @@ class ProjectErrorListView( ] -RelationRow = namedtuple( - "RelationRow", - field_names=["to_resource", "status", "map_type", "score", "from_resource"], -) - - class CodebaseRelationListView( ConditionalLoginRequired, ProjectRelatedViewMixin, @@ -1250,7 +1243,10 @@ class CodebaseRelationListView( "to_resource", queryset=unordered_resources.only("path", "is_text", "status"), ), - Prefetch("from_resource", queryset=unordered_resources.only("path", "is_text")), + Prefetch( + "from_resource", + queryset=unordered_resources.only("path", "is_text", "status"), + ), ] paginate_by = settings.SCANCODEIO_PAGINATE_BY.get("relation", 100) table_columns = [ @@ -1272,36 +1268,6 @@ def get_filterset_kwargs(self, filterset_class): kwargs.update({"project": self.project}) return kwargs - @staticmethod - def get_rows(qs): - for relation in qs: - score = relation.extra_data.get("path_score", "") - if diff_ratio := relation.extra_data.get("diff_ratio", ""): - score += f" diff_ratio: {diff_ratio}" - yield RelationRow( - relation.to_resource.path, - relation.to_resource.status, - relation.map_type, - score, - relation.from_resource.path, - ) - - def export_xlsx_file_response(self): - filtered_qs = self.filterset.qs - output_file = io.BytesIO() - - with xlsxwriter.Workbook(output_file) as workbook: - output._add_xlsx_worksheet( - workbook=workbook, - worksheet_name="RELATIONS", - rows=self.get_rows(qs=filtered_qs), - fields=RelationRow._fields, - ) - - filename = f"{self.project.name}_{self.model._meta.model_name}.xlsx" - output_file.seek(0) - return FileResponse(output_file, as_attachment=True, filename=filename) - class CodebaseResourceDetailsView( ConditionalLoginRequired,