diff --git a/wagtailstreamforms/templates/streamforms/index_submissions.html b/wagtailstreamforms/templates/streamforms/index_submissions.html index 8a6f50da..4201dfc1 100644 --- a/wagtailstreamforms/templates/streamforms/index_submissions.html +++ b/wagtailstreamforms/templates/streamforms/index_submissions.html @@ -86,7 +86,14 @@

- +
+ +
+ {% if contains_files %} +
+ +
+ {% endif %}
diff --git a/wagtailstreamforms/views/submission_list.py b/wagtailstreamforms/views/submission_list.py index 6842241b..bf8fed45 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) @@ -107,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 } )