diff options
| author | Mitch Riedstra <mitch@riedstra.us> | 2018-01-09 00:41:10 -0500 |
|---|---|---|
| committer | Mitch Riedstra <mitch@riedstra.us> | 2018-01-09 00:41:10 -0500 |
| commit | 8bdb452b40e93ad29cca0b98b3d388bfbe5093f4 (patch) | |
| tree | 398a68d2c24596ec31bc939db010c31274afb508 /app/dispatch/views.py | |
| parent | 88c792763d22050e383af1cc5e2741b8b5993ad6 (diff) | |
| download | dispatch-tracker-8bdb452b40e93ad29cca0b98b3d388bfbe5093f4.tar.gz dispatch-tracker-8bdb452b40e93ad29cca0b98b3d388bfbe5093f4.tar.xz | |
Initial version of paperwork download--still doesn't download lol
Diffstat (limited to 'app/dispatch/views.py')
| -rw-r--r-- | app/dispatch/views.py | 50 |
1 files changed, 47 insertions, 3 deletions
diff --git a/app/dispatch/views.py b/app/dispatch/views.py index 2ca9d22..10e23f8 100644 --- a/app/dispatch/views.py +++ b/app/dispatch/views.py @@ -19,9 +19,7 @@ from django.contrib.auth import get_user_model from django.contrib.auth.mixins import UserPassesTestMixin # from django.http import HttpResponseRedirect from .misc import get_week_dates, split_loads_by_day -import datetime -import re -import os +import re, os, tempfile, datetime, shutil, zipfile User = get_user_model() @@ -233,6 +231,52 @@ class CustomerDetail(LoadDateSort): model = Customer +def CustomerDownload(request, pk): + customer = Customer.objects.get(pk=pk) + week_dates = get_week_dates(request.GET.get('date', None)) + + 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(): + + if os.path.exists(p.document.path): + try: + exp = re.compile('\.[^.]*$', re.IGNORECASE) + ext = exp.findall(p.document.path) + ext = ext[0] + except Exception as e: + print(e) + ext = '.pdf' + + desc = l.description.replace('"', '') + + # Dest file path + dfp = tempdir.name + '/' + desc + '-' + l.user.identity.name + ext + zipname = desc + '-' + l.user.identity.name + ext + + shutil.copyfile(p.document.path, dfp) + + z.write(dfp, zipname) + # print(p.document) + + z.close() + + + + + class CustomerUpdate(UserPassesTestMixin, UpdateView): template_name = "dispatch/companies/edit.html" model = Customer |
