Skip to content

Commit

Permalink
Merge main and fix conflict #164
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Druez <[email protected]>
  • Loading branch information
tdruez committed Aug 15, 2022
2 parents 8a1d284 + 15b3b45 commit 78dd934
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 13 deletions.
9 changes: 5 additions & 4 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,13 @@ v31.0.0 (next)

- CodebaseResource.name now contains both the bare file name with extension, as
opposed to just the bare file name without extension.

- Using a name stripped from its extension was something that was not used in
other AboutCode project or tools.

Using a name stripped from its extension was something that was not used in
other AboutCode project or tools.
https://github.com/nexB/scancode.io/issues/467

- Export current results as XLSX for resource, packages, and errors list views.
https://github.com/nexB/scancode.io/issues/48

v30.2.0 (2021-12-17)
--------------------

Expand Down
4 changes: 2 additions & 2 deletions scanpipe/pipes/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def to_json(project):
return output_file


def _queryset_to_xlsx_worksheet(queryset, workbook, exclude_fields=()):
def queryset_to_xlsx_worksheet(queryset, workbook, exclude_fields=()):
"""
Adds a new worksheet to the ``workbook`` ``xlsxwriter.Workbook`` using the
``queryset``. The ``queryset`` "model_name" is used as a name for the
Expand Down Expand Up @@ -378,6 +378,6 @@ def to_xlsx(project):

with xlsxwriter.Workbook(output_file) as workbook:
for queryset in querysets:
_queryset_to_xlsx_worksheet(queryset, workbook, exclude_fields)
queryset_to_xlsx_worksheet(queryset, workbook, exclude_fields)

return output_file
17 changes: 17 additions & 0 deletions scanpipe/templates/scanpipe/includes/list_actions_dropdown.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<div class="dropdown is-hoverable">
<div class="dropdown-trigger">
<a class="px-1 is-black-link" aria-haspopup="true" aria-controls="dropdown-menu-action">
<i class="fas fa-sliders-h"></i>
</a>
</div>
<div class="dropdown-menu" id="dropdown-menu-action" role="menu">
<div class="dropdown-content">
<span class="dropdown-item">
Actions:
</span>
<a href="?{{ export_xlsx_url_query }}" class="dropdown-item">
<i class="fas fa-download mr-2"></i>Export results as XLSX
</a>
</div>
</div>
</div>
3 changes: 3 additions & 0 deletions scanpipe/templates/scanpipe/includes/pagination_header.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<div class="is-flex is-justify-content-space-between">
<div>
{{ paginator.count|intcomma }} results
{% if export_xlsx_url_query %}
{% include "scanpipe/includes/list_actions_dropdown.html" %}
{% endif %}
</div>
<div>
{% if page_obj.has_previous %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
</a>
</div>
</div>
</div>
</div>
47 changes: 41 additions & 6 deletions scanpipe/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# ScanCode.io is a free software code scanning tool from nexB Inc. and others.
# Visit https://github.com/nexB/scancode.io for support and download.

import io
import json
from collections import Counter
from contextlib import suppress
Expand All @@ -40,6 +41,7 @@
from django.views.generic.edit import FormView

import saneyaml
import xlsxwriter
from django_filters.views import FilterView

from scancodeio.auth import ConditionalLoginRequired
Expand Down Expand Up @@ -252,6 +254,42 @@ def get_context_data(self, **kwargs):
return context


class ExportXLSXMixin:
"""
Adds the ability to export the current filtered QuerySet of a `FilterView` into
the XLSX format.
"""

export_xlsx_query_param = "export_xlsx"

def export_xlsx_file_response(self):
filtered_qs = self.filterset.qs
output_file = io.BytesIO()
with xlsxwriter.Workbook(output_file) as workbook:
output.queryset_to_xlsx_worksheet(filtered_qs, workbook)

filename = f"{self.project.name}_{self.model._meta.model_name}.xlsx"
output_file.seek(0)
return FileResponse(output_file, as_attachment=True, filename=filename)

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)

query_dict = self.request.GET.copy()
query_dict[self.export_xlsx_query_param] = True
context["export_xlsx_url_query"] = query_dict.urlencode()

return context

def get(self, request, *args, **kwargs):
response = super().get(request, *args, **kwargs)

if request.GET.get(self.export_xlsx_query_param):
return self.export_xlsx_file_response()

return response


class PaginatedFilterView(FilterView):
"""
Adds a `url_params_without_page` value in the template context to include the
Expand Down Expand Up @@ -658,6 +696,7 @@ class CodebaseResourceListView(
ConditionalLoginRequired,
PrefetchRelatedViewMixin,
ProjectRelatedViewMixin,
ExportXLSXMixin,
PaginatedFilterView,
):
model = CodebaseResource
Expand All @@ -668,11 +707,6 @@ class CodebaseResourceListView(

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)

query_dict = self.request.GET.copy()
query_dict.pop("page", None)
context["url_params_without_page"] = query_dict.urlencode()

context["include_compliance_alert"] = scanpipe_app.policies_enabled
return context

Expand All @@ -681,6 +715,7 @@ class DiscoveredPackageListView(
ConditionalLoginRequired,
PrefetchRelatedViewMixin,
ProjectRelatedViewMixin,
ExportXLSXMixin,
PaginatedFilterView,
):
model = DiscoveredPackage
Expand All @@ -691,7 +726,7 @@ class DiscoveredPackageListView(


class ProjectErrorListView(
ConditionalLoginRequired, ProjectRelatedViewMixin, FilterView
ConditionalLoginRequired, ProjectRelatedViewMixin, ExportXLSXMixin, FilterView
):
model = ProjectError
filterset_class = ErrorFilterSet
Expand Down

0 comments on commit 78dd934

Please sign in to comment.