From 0f904d5d3f74f7d73a64f9baf9ba748442209456 Mon Sep 17 00:00:00 2001 From: Robin Vanhove Date: Fri, 24 Apr 2020 13:39:05 +0200 Subject: [PATCH 1/2] Make it possble to download all files from form Similar to download a CSV of the submissions, a zip file is created containing the files that were uploaded on the form. --- .../streamforms/index_submissions.html | 7 +++++- wagtailstreamforms/views/submission_list.py | 23 ++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/wagtailstreamforms/templates/streamforms/index_submissions.html b/wagtailstreamforms/templates/streamforms/index_submissions.html index 8a6f50da..d698f322 100644 --- a/wagtailstreamforms/templates/streamforms/index_submissions.html +++ b/wagtailstreamforms/templates/streamforms/index_submissions.html @@ -86,7 +86,12 @@

- +
+ +
+
+ +
diff --git a/wagtailstreamforms/views/submission_list.py b/wagtailstreamforms/views/submission_list.py index 6842241b..6f27312a 100644 --- a/wagtailstreamforms/views/submission_list.py +++ b/wagtailstreamforms/views/submission_list.py @@ -1,4 +1,6 @@ import csv +import zipfile +import os.path import datetime from django.core.exceptions import PermissionDenied @@ -10,7 +12,7 @@ from wagtail.contrib.modeladmin.helpers import PermissionHelper from wagtailstreamforms import hooks from wagtailstreamforms.forms import SelectDateForm -from wagtailstreamforms.models import Form +from wagtailstreamforms.models import Form, FormSubmissionFile class SubmissionListView(SingleObjectMixin, ListView): @@ -46,6 +48,9 @@ def get(self, request, *args, **kwargs): if request.GET.get("action") == "CSV": return self.csv() + if request.GET.get("action") == "ZIP": + return self.zip() + return super().get(request, *args, **kwargs) def csv(self): @@ -67,6 +72,22 @@ def csv(self): return response + def zip(self): + response = HttpResponse(content_type="application/zip") + response["Content-Disposition"] = "attachment;filename=export.zip" + + submission_files = FormSubmissionFile.objects.filter(submission__form=self.object) + + # Write a zipfile to the response + zipf = zipfile.ZipFile(response, "w") + + for submission in submission_files: + # write a file to the response with only the correct filename + zipf.writestr(os.path.split(submission.file.name)[1], submission.file.read()) + + zipf.close() + return response + def get_queryset(self): submission_class = self.object.get_submission_class() self.queryset = submission_class._default_manager.filter(form=self.object) From 0a2bcfd8b336a815024180e4889bcb9619a3a638 Mon Sep 17 00:00:00 2001 From: Robin Vanhove Date: Fri, 24 Apr 2020 13:54:31 +0200 Subject: [PATCH 2/2] Hide download ZIP file button if no files are present --- wagtailstreamforms/templates/streamforms/index_submissions.html | 2 ++ wagtailstreamforms/views/submission_list.py | 1 + 2 files changed, 3 insertions(+) diff --git a/wagtailstreamforms/templates/streamforms/index_submissions.html b/wagtailstreamforms/templates/streamforms/index_submissions.html index d698f322..4201dfc1 100644 --- a/wagtailstreamforms/templates/streamforms/index_submissions.html +++ b/wagtailstreamforms/templates/streamforms/index_submissions.html @@ -89,9 +89,11 @@

+ {% if contains_files %}
+ {% endif %} diff --git a/wagtailstreamforms/views/submission_list.py b/wagtailstreamforms/views/submission_list.py index 6f27312a..bf8fed45 100644 --- a/wagtailstreamforms/views/submission_list.py +++ b/wagtailstreamforms/views/submission_list.py @@ -128,6 +128,7 @@ def get_context_data(self, **kwargs): "has_delete_permission": self.permission_helper.user_can_delete_obj( self.request.user, self.object ), + "contains_files": FormSubmissionFile.objects.filter(submission__form=self.object).count() > 0 } )