aboutsummaryrefslogtreecommitdiff
path: root/app/dispatch/views.py
diff options
context:
space:
mode:
authorMitch Riedstra <mitch@riedstra.us>2018-01-09 01:26:02 -0500
committerMitch Riedstra <mitch@riedstra.us>2018-01-09 01:26:02 -0500
commitec67cee42fd0f8ac989b5b93949938059b661f0c (patch)
tree9b7a4c5ae126ac9ce4cd277f044d929a46271d15 /app/dispatch/views.py
parent8bdb452b40e93ad29cca0b98b3d388bfbe5093f4 (diff)
downloaddispatch-tracker-ec67cee42fd0f8ac989b5b93949938059b661f0c.tar.gz
dispatch-tracker-ec67cee42fd0f8ac989b5b93949938059b661f0c.tar.xz
Add the ability to download all of the paperwork from a customer view
Diffstat (limited to 'app/dispatch/views.py')
-rw-r--r--app/dispatch/views.py37
1 files changed, 23 insertions, 14 deletions
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):