Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possble to download all files from form #148

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,14 @@ <h1 class="icon icon-form">
</div>
</div>
<div class="right">
<button name="action" value="CSV" class="button bicolor icon icon-download">{% trans 'Download CSV' %}</button>
<div class="col">
<button name="action" value="CSV" class="button bicolor icon icon-download">{% trans 'Download CSV' %}</button>
</div>
{% if contains_files %}
<div class="col">
<button name="action" value="ZIP" class="button bicolor icon icon-download">{% trans 'Download ZIP' %}</button>
</div>
{% endif %}
</div>
</div>
</form>
Expand Down
24 changes: 23 additions & 1 deletion wagtailstreamforms/views/submission_list.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import csv
import zipfile
import os.path
import datetime

from django.core.exceptions import PermissionDenied
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
)

Expand Down