Skip to content

Commit

Permalink
Simplify the XLSX output code for CodebaseRelation list view #858
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Druez <tdruez@nexb.com>
  • Loading branch information
tdruez committed Aug 16, 2023
1 parent 24686c8 commit b145d0a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 40 deletions.
4 changes: 3 additions & 1 deletion scanpipe/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,11 @@ class CodebaseRelationSerializer(serializers.ModelSerializer):
class Meta:
model = CodebaseRelation
fields = [
"from_resource",
"to_resource",
"status",
"map_type",
"score",
"from_resource",
]


Expand Down
11 changes: 11 additions & 0 deletions scanpipe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
{% load humanize %}

{% block paginator_count %}
{{ paginator.count|intcomma }} relations
<span style="vertical-align: middle">
{{ paginator.count|intcomma }} relations
</span>
<a href="{% url 'project_resources' project.slug %}?tag=to&relation_map_type=none&status=_EMPTY_" target="_blank" class="button is-small is-info is-outlined">
<span>Un-mapped <strong>to/</strong> resources</span>
<span class="icon">
Expand Down
42 changes: 4 additions & 38 deletions scanpipe/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import json
import operator
from collections import Counter
from collections import namedtuple
from contextlib import suppress
from pathlib import Path

Expand Down Expand Up @@ -1228,12 +1227,6 @@ class ProjectErrorListView(
]


RelationRow = namedtuple(
"RelationRow",
field_names=["to_resource", "status", "map_type", "score", "from_resource"],
)


class CodebaseRelationListView(
ConditionalLoginRequired,
ProjectRelatedViewMixin,
Expand All @@ -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 = [
Expand All @@ -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,
Expand Down

0 comments on commit b145d0a

Please # to comment.