aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMitch Riedstra <mitch@riedstra.us>2018-01-09 00:41:10 -0500
committerMitch Riedstra <mitch@riedstra.us>2018-01-09 00:41:10 -0500
commit8bdb452b40e93ad29cca0b98b3d388bfbe5093f4 (patch)
tree398a68d2c24596ec31bc939db010c31274afb508
parent88c792763d22050e383af1cc5e2741b8b5993ad6 (diff)
downloaddispatch-tracker-8bdb452b40e93ad29cca0b98b3d388bfbe5093f4.tar.gz
dispatch-tracker-8bdb452b40e93ad29cca0b98b3d388bfbe5093f4.tar.xz
Initial version of paperwork download--still doesn't download lol
-rw-r--r--app/dispatch/templates/dispatch/companies/detail.html5
-rw-r--r--app/dispatch/urls.py1
-rw-r--r--app/dispatch/views.py50
3 files changed, 53 insertions, 3 deletions
diff --git a/app/dispatch/templates/dispatch/companies/detail.html b/app/dispatch/templates/dispatch/companies/detail.html
index cd29b16..5dd2414 100644
--- a/app/dispatch/templates/dispatch/companies/detail.html
+++ b/app/dispatch/templates/dispatch/companies/detail.html
@@ -9,6 +9,11 @@
<h1>Company: {{ object.name }}</h1>
<h2>Loads for {{week_dates.start_date|date:"l, F, d"}} to {{week_dates.end_date|date:"l, F, d"}}</h2>
</div>
+ <div class="col s12 m3">
+ <div class="right-align">
+ <a class="btn blue" href="{% url 'customer_download' object.id %}?date={{week_dates.start_date|date:"m/d/Y"}}">Download Paperwork</a>
+ </div>
+ </div>
</div>
<div class="row">
diff --git a/app/dispatch/urls.py b/app/dispatch/urls.py
index c7622ff..330dd05 100644
--- a/app/dispatch/urls.py
+++ b/app/dispatch/urls.py
@@ -28,6 +28,7 @@ urlpatterns = [
url(r'^customers/view/(?P<pk>\d+)$', views.CustomerDetail.as_view(), name='customer_detail'),
url(r'^customers/edit/(?P<pk>\d+)$', views.CustomerUpdate.as_view(), name='customer_edit'),
url(r'^customers/delete/(?P<pk>\d+)$', views.CustomerDelete.as_view(), name='customer_delete'),
+ url(r'^customers/download/(?P<pk>\d+)$', views.CustomerDownload, name='customer_download'),
url(r'^loads/$', views.LoadList.as_view(), name='load_list'),
url(r'^loads/new$', views.LoadCreate.as_view(), name='load_new'),
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