aboutsummaryrefslogtreecommitdiff
path: root/app/dispatch/views.py
diff options
context:
space:
mode:
Diffstat (limited to 'app/dispatch/views.py')
-rw-r--r--app/dispatch/views.py50
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