Skip to content

Commit

Permalink
Refine the implementation of get_projects_queryset #1524
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <[email protected]>
  • Loading branch information
tdruez committed Jan 16, 2025
1 parent a17e2e9 commit 4791152
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions scanpipe/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1226,30 +1226,36 @@ def perform_action(self, action, project_uuid, action_kwargs=None):
def get_success_message(self, action, count):
return f"{count} projects have been {action}."

def get_projects_queryset(self, action_form=None):
"""
Return the Project QuerySet from the user selection.
An instance of BaseProjectActionForm can be provided as the ``action_form``
argument for the support of ``select_across``.
"""
if action_form:
select_across = self.report_form.cleaned_data.get("select_across")
url_query = self.report_form.cleaned_data.GET("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 export_xlsx_file_response(self):
self.report_form = ProjectReportForm(self.request.POST)
if not self.report_form.is_valid():
return HttpResponseRedirect(self.success_url)

return super().export_xlsx_file_response()

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):
projects = self.get_projects_queryset(action_form=self.report_form)

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, url_query=url_query
)

return queryset.filter(project__in=projects)

def get_export_xlsx_prepend_fields(self):
Expand All @@ -1269,7 +1275,8 @@ def download_outputs_zip_response(self):

output_format = outputs_download_form.cleaned_data["output_format"]
output_function = output.FORMAT_TO_FUNCTION_MAPPING.get(output_format)
projects = self.get_projects_queryset()

projects = self.get_projects_queryset(action_form=outputs_download_form)

# In-memory file storage for the zip archive
zip_buffer = io.BytesIO()
Expand Down

0 comments on commit 4791152

Please sign in to comment.