Skip to content

Commit 0f904d5

Browse files
committed
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.
1 parent 3056ae6 commit 0f904d5

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

wagtailstreamforms/templates/streamforms/index_submissions.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,12 @@ <h1 class="icon icon-form">
8686
</div>
8787
</div>
8888
<div class="right">
89-
<button name="action" value="CSV" class="button bicolor icon icon-download">{% trans 'Download CSV' %}</button>
89+
<div class="col">
90+
<button name="action" value="CSV" class="button bicolor icon icon-download">{% trans 'Download CSV' %}</button>
91+
</div>
92+
<div class="col">
93+
<button name="action" value="ZIP" class="button bicolor icon icon-download">{% trans 'Download ZIP' %}</button>
94+
</div>
9095
</div>
9196
</div>
9297
</form>

wagtailstreamforms/views/submission_list.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import csv
2+
import zipfile
3+
import os.path
24
import datetime
35

46
from django.core.exceptions import PermissionDenied
@@ -10,7 +12,7 @@
1012
from wagtail.contrib.modeladmin.helpers import PermissionHelper
1113
from wagtailstreamforms import hooks
1214
from wagtailstreamforms.forms import SelectDateForm
13-
from wagtailstreamforms.models import Form
15+
from wagtailstreamforms.models import Form, FormSubmissionFile
1416

1517

1618
class SubmissionListView(SingleObjectMixin, ListView):
@@ -46,6 +48,9 @@ def get(self, request, *args, **kwargs):
4648
if request.GET.get("action") == "CSV":
4749
return self.csv()
4850

51+
if request.GET.get("action") == "ZIP":
52+
return self.zip()
53+
4954
return super().get(request, *args, **kwargs)
5055

5156
def csv(self):
@@ -67,6 +72,22 @@ def csv(self):
6772

6873
return response
6974

75+
def zip(self):
76+
response = HttpResponse(content_type="application/zip")
77+
response["Content-Disposition"] = "attachment;filename=export.zip"
78+
79+
submission_files = FormSubmissionFile.objects.filter(submission__form=self.object)
80+
81+
# Write a zipfile to the response
82+
zipf = zipfile.ZipFile(response, "w")
83+
84+
for submission in submission_files:
85+
# write a file to the response with only the correct filename
86+
zipf.writestr(os.path.split(submission.file.name)[1], submission.file.read())
87+
88+
zipf.close()
89+
return response
90+
7091
def get_queryset(self):
7192
submission_class = self.object.get_submission_class()
7293
self.queryset = submission_class._default_manager.filter(form=self.object)

0 commit comments

Comments
 (0)