Skip to content

Commit

Permalink
Add support for all models in XLSX report #1524
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <[email protected]>
  • Loading branch information
tdruez committed Jan 10, 2025
1 parent 611520e commit 727bbba
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ v34.9.4 (unreleased)
at once from a directory containing input files.
https://github.com/aboutcode-org/scancode.io/issues/1437

- Add a "TODOS" sheet containing on REQUIRES_REVIEW resources in XLSX.
https://github.com/aboutcode-org/scancode.io/issues/1524

v34.9.3 (2024-12-31)
--------------------

Expand Down
61 changes: 61 additions & 0 deletions scanpipe/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,67 @@ class ProjectOutputDownloadForm(forms.Form):
)


class ProjectReportForm(forms.Form):
model_name = forms.ChoiceField(
label="Choose the object type to include in the XLSX file",
choices=[
("discoveredpackage", "Packages"),
("discovereddependency", "Dependencies"),
("codebaseresource", "Resources"),
("codebaserelation", "Relations"),
("projectmessage", "Messages"),
("todos", "TODOs"),
],
required=True,
initial="discoveredpackage",
widget=forms.RadioSelect,
)

# TODO: Remove duplication
def get_queryset(self):
from scanpipe.models import CodebaseRelation
from scanpipe.models import CodebaseResource
from scanpipe.models import DiscoveredDependency
from scanpipe.models import DiscoveredPackage
from scanpipe.models import ProjectMessage
from scanpipe.pipes import flag
from scanpipe.pipes.output import TODO_FIELDS

querysets = {
"discoveredpackage": (
DiscoveredPackage.objects.order_by(
"type",
"namespace",
"name",
"version",
)
),
"discovereddependency": (
DiscoveredDependency.objects.prefetch_for_serializer().order_by(
"type",
"namespace",
"name",
"version",
"datasource_id",
)
),
"codebaseresource": (
CodebaseResource.objects.without_symlinks().prefetch_for_serializer()
),
"codebaserelation": (
CodebaseRelation.objects.select_related("from_resource", "to_resource")
),
"projectmessage": ProjectMessage.objects.all(),
"todos": (
CodebaseResource.objects.files()
.status(flag.REQUIRES_REVIEW)
# .only(*TODO_FIELDS)
),
}

return querysets.get(self.cleaned_data["model_name"])


class ListTextarea(forms.CharField):
"""
A Django form field that displays as a textarea and converts each line of input
Expand Down
2 changes: 1 addition & 1 deletion scanpipe/pipes/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def get_todos_data(project):
"""Return the list of Resources that requires review."""
return (
project.codebaseresources.files()
.filter(status=flag.REQUIRES_REVIEW)
.status(flag.REQUIRES_REVIEW)
.values(*TODO_FIELDS)
)

Expand Down
6 changes: 3 additions & 3 deletions scanpipe/templates/scanpipe/modals/projects_report_modal.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
</header>
<form action="{% url 'project_action' %}" method="post" id="report-projects-form">{% csrf_token %}
<section class="modal-card-body">
<div class="notification is-info has-text-weight-semibold">
All the packages for the selected projects will be included in an XLSX report.
</div>
<ul class="mb-3">
{{ report_form.as_ul }}
</ul>
</section>
<input type="hidden" name="action" value="report">
<footer class="modal-card-foot is-flex is-justify-content-space-between">
Expand Down
11 changes: 8 additions & 3 deletions scanpipe/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
from scanpipe.forms import ProjectCloneForm
from scanpipe.forms import ProjectForm
from scanpipe.forms import ProjectOutputDownloadForm
from scanpipe.forms import ProjectReportForm
from scanpipe.forms import ProjectSettingsForm
from scanpipe.models import CodebaseRelation
from scanpipe.models import CodebaseResource
Expand Down Expand Up @@ -598,6 +599,7 @@ def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["archive_form"] = ArchiveProjectForm()
context["outputs_download_form"] = ProjectOutputDownloadForm()
context["report_form"] = ProjectReportForm()
return context

def get_queryset(self):
Expand Down Expand Up @@ -1220,10 +1222,13 @@ def get_projects_queryset(self):
return Project.objects.filter(pk__in=self.selected_project_ids)

def get_export_xlsx_queryset(self):
report_form = ProjectReportForm(self.request.POST)
if not report_form.is_valid():
return HttpResponseRedirect(self.success_url)

queryset = report_form.get_queryset()
projects = self.get_projects_queryset()
packages = DiscoveredPackage.objects.filter(project__in=projects)
packages = packages.select_related("project")
return packages
return queryset.filter(project__in=projects)

def get_export_xlsx_extra_fields(self):
return ["project"]
Expand Down

0 comments on commit 727bbba

Please sign in to comment.