Skip to content

Commit

Permalink
Refine the implementation of select_across #1524
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <[email protected]>
  • Loading branch information
tdruez committed Jan 15, 2025
1 parent 2714268 commit 6b9b437
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 22 deletions.
24 changes: 16 additions & 8 deletions scanpipe/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,20 @@ def save(self, project):
return input_source


class BaseProjectActionForm(forms.Form):
select_across = forms.BooleanField(
label="",
required=False,
initial=0,
help_text="All project matching current search and filters will be included.",
)
url_query = forms.CharField(
widget=forms.HiddenInput,
required=False,
help_text="Stores the current URL filters.",
)


class ArchiveProjectForm(forms.Form):
remove_input = forms.BooleanField(
label="Remove inputs",
Expand All @@ -256,7 +270,7 @@ class ArchiveProjectForm(forms.Form):
)


class ProjectOutputDownloadForm(forms.Form):
class ProjectOutputDownloadForm(BaseProjectActionForm):
output_format = forms.ChoiceField(
label="Choose the output format to include in the ZIP file",
choices=[
Expand All @@ -272,7 +286,7 @@ class ProjectOutputDownloadForm(forms.Form):
)


class ProjectReportForm(forms.Form):
class ProjectReportForm(BaseProjectActionForm):
model_name = forms.ChoiceField(
label="Choose the object type to include in the XLSX file",
choices=[
Expand All @@ -287,12 +301,6 @@ class ProjectReportForm(forms.Form):
initial="discoveredpackage",
widget=forms.RadioSelect,
)
select_across = forms.BooleanField(
label="",
required=False,
initial=0,
help_text="All project matching current search and filters will be included.",
)


class ListTextarea(forms.CharField):
Expand Down
2 changes: 1 addition & 1 deletion scanpipe/pipes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def collect_and_create_codebase_resources(project, batch_size=5000):
Collect and create codebase resources including the "to/" and "from/" context using
the resource tag field.
The default ``batch_size`` can be overriden, although the benefits of a value
The default ``batch_size`` can be overridden, although the benefits of a value
greater than 5000 objects are usually not significant.
"""
model_class = CodebaseResource
Expand Down
18 changes: 15 additions & 3 deletions scanpipe/templates/scanpipe/modals/projects_download_modal.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{% load humanize %}
<div class="modal" id="modal-projects-download">
<div class="modal-background"></div>
<div class="modal-card">
Expand All @@ -7,10 +8,21 @@
</header>
<form action="{% url 'project_action' %}" method="post" id="download-projects-form">{% csrf_token %}
<section class="modal-card-body">
<ul class="mb-3">
{{ outputs_download_form.as_ul }}
</ul>
<div class="field">
<label class="label">{{ outputs_download_form.output_format.label }}</label>
<div class="control">
{{ outputs_download_form.output_format }}
</div>
</div>
<div class="field">
<label class="checkbox" for="{{ outputs_download_form.select_across.id_for_label }}">
<input type="checkbox" name="{{ outputs_download_form.select_across.name }}" id="{{ outputs_download_form.select_across.id_for_label }}">
Include all {{ paginator.count|intcomma }} projects
</label>
<p class="help">{{ outputs_download_form.select_across.help_text }}</p>
</div>
</section>
<input type="hidden" name="{{ outputs_download_form.url_query.name }}" value="{{ request.GET.urlencode }}">
<input type="hidden" name="action" value="download">
<footer class="modal-card-foot is-flex is-justify-content-space-between">
<button class="button has-text-weight-semibold" type="reset">Cancel</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<p class="help">{{ report_form.select_across.help_text }}</p>
</div>
</section>
<input type="hidden" name="{{ report_form.url_query.name }}" value="{{ request.GET.urlencode }}">
<input type="hidden" name="action" value="report">
<footer class="modal-card-foot is-flex is-justify-content-space-between">
<button class="button has-text-weight-semibold" type="reset">Cancel</button>
Expand Down
20 changes: 10 additions & 10 deletions scanpipe/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
from collections import Counter
from contextlib import suppress
from pathlib import Path
from urllib.parse import urlparse

from django.apps import apps
from django.conf import settings
Expand Down Expand Up @@ -1234,22 +1233,23 @@ def export_xlsx_file_response(self):

return super().export_xlsx_file_response()

def get_projects_queryset(self, select_across=False):
if select_across:
# TODO: We could store the previous URL in the Action form instead
if referrer_url := self.request.META.get("HTTP_REFERER", ""):
url_query = urlparse(referrer_url).query
project_filterset = ProjectFilterSet(data=QueryDict(url_query))
if project_filterset.is_valid():
return project_filterset.qs
def get_projects_queryset(self, select_across=False, url_query=""):
if select_across and url_query:
project_filterset = ProjectFilterSet(data=QueryDict(url_query))
if project_filterset.is_valid():
return project_filterset.qs

return Project.objects.filter(pk__in=self.selected_project_ids)

def get_export_xlsx_queryset(self):
model_name = self.report_form.cleaned_data["model_name"]
# TODO: Make th 2 following fields available in all actions
select_across = self.report_form.cleaned_data["select_across"]
url_query = self.report_form.cleaned_data["url_query"]
queryset = output.get_queryset(project=None, model_name=model_name)
projects = self.get_projects_queryset(select_across=select_across)
projects = self.get_projects_queryset(
select_across=select_across, url_query=url_query
)
return queryset.filter(project__in=projects)

def get_export_xlsx_prepend_fields(self):
Expand Down

0 comments on commit 6b9b437

Please sign in to comment.