From: Cezary Czaplewski Date: Tue, 20 Aug 2019 20:47:10 +0000 (+0200) Subject: button to download all files as zip archive X-Git-Url: http://mmka.chem.univ.gda.pl/gitweb/?a=commitdiff_plain;h=9ed732a162468afe926d1982efa882468c5178a3;p=django_unres.git button to download all files as zip archive --- diff --git a/django_simple/todo/templates/details.html b/django_simple/todo/templates/details.html index 28e640e..36e8e63 100644 --- a/django_simple/todo/templates/details.html +++ b/django_simple/todo/templates/details.html @@ -369,6 +369,12 @@ Created {{ task.created_date }} {{ task.jobdirname }} + {% if task.done %} +   + + Download as zip + {% endif %} diff --git a/django_simple/todo/templates/details1.html b/django_simple/todo/templates/details1.html index e3135c8..eec9c50 100644 --- a/django_simple/todo/templates/details1.html +++ b/django_simple/todo/templates/details1.html @@ -358,6 +358,12 @@ Created {{ task.created_date }} {{ task.jobdirname }} + {% if task.done %} +   + + Download as zip + {% endif %} diff --git a/django_simple/todo/urls.py b/django_simple/todo/urls.py index fd3c4f1..5543e47 100644 --- a/django_simple/todo/urls.py +++ b/django_simple/todo/urls.py @@ -23,4 +23,5 @@ urlpatterns = [ url(r'^details1/(?P\w+)/(?P\d+)/$', views.details1,name='details1'), url(r'^details1/(?P\w+)/(?P\d+)/restart/$', views.restart1,name='restart1'), url(r'^delete/(?P\d+)/$', views.delete), + url(r'(?P\d+)/all.zip$', views.zip_all_files), ] diff --git a/django_simple/todo/views.py b/django_simple/todo/views.py index 1157bfa..cddfc88 100644 --- a/django_simple/todo/views.py +++ b/django_simple/todo/views.py @@ -1161,3 +1161,24 @@ def refresh_done0(task): return + +@login_required +def zip_all_files(request, task_id): + import os, zipfile + from django.http import HttpResponse + from wsgiref.util import FileWrapper + from django.core.files.temp import NamedTemporaryFile + + task = get_object_or_404(Task, id=task_id) + + temp=NamedTemporaryFile() + archive = zipfile.ZipFile(temp, 'w', zipfile.ZIP_DEFLATED) + for (dirpath, dirnames, filenames) in os.walk(task.jobdirname): + for file1 in filenames: + archive.write(dirpath+"/"+file1,file1) + archive.close() + temp.seek(0) + wrapper = FileWrapper(temp) + response = HttpResponse(wrapper, content_type='application/zip') + response['Content-Disposition'] = 'attachment; filename=all.zip' + return response