From ec67cee42fd0f8ac989b5b93949938059b661f0c Mon Sep 17 00:00:00 2001 From: Mitch Riedstra Date: Tue, 9 Jan 2018 01:26:02 -0500 Subject: Add the ability to download all of the paperwork from a customer view --- app/dispatch/views.py | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) (limited to 'app/dispatch/views.py') diff --git a/app/dispatch/views.py b/app/dispatch/views.py index 10e23f8..ef5bad8 100644 --- a/app/dispatch/views.py +++ b/app/dispatch/views.py @@ -235,18 +235,18 @@ def CustomerDownload(request, pk): customer = Customer.objects.get(pk=pk) week_dates = get_week_dates(request.GET.get('date', None)) + td = tempfile.TemporaryDirectory() + zip_file_basename = customer.name + '-' + \ + week_dates['start_date'].strftime('%m.%d.%Y') + \ + '.zip' + zip_file_name = td.name + '/' + zip_file_basename + print(zip_file_name) + z = zipfile.ZipFile(zip_file_name, 'w') + loads = customer.load_set.filter( date__range=(week_dates['start_date'], week_dates['end_date'])) - tempdir = tempfile.TemporaryDirectory() - # print(tempdir) - - # I'm going to need to gather all of the paperwork files from the loads. - # Put them into a tmp folder. I then need to zip them up and serve that - # file up to the user - - z = zipfile.ZipFile('/tmp/test.zip', 'w') for l in loads: for p in l.paperwork_set.all(): @@ -262,19 +262,28 @@ def CustomerDownload(request, pk): desc = l.description.replace('"', '') - # Dest file path - dfp = tempdir.name + '/' + desc + '-' + l.user.identity.name + ext + # Get the name for the zip file zipname = desc + '-' + l.user.identity.name + ext - shutil.copyfile(p.document.path, dfp) - - z.write(dfp, zipname) + z.write(p.document.path, zipname) # print(p.document) + # Finally close off the zip file z.close() - + fh = open(zip_file_name, 'rb') + + response = HttpResponse( + fh.read(), + content_type='application/force-download') + + response['Content-Disposition'] = \ + 'attachment; filename="{}"'.format(smart_str(zip_file_basename)) + response['X-Sendfile'] = smart_str(fh) + + print('%r' % response) + return response class CustomerUpdate(UserPassesTestMixin, UpdateView): -- cgit v1.2.3