diff options
Diffstat (limited to 'app')
| -rw-r--r-- | app/dispatch/templates/dispatch/companies/detail.html | 5 | ||||
| -rw-r--r-- | app/dispatch/urls.py | 1 | ||||
| -rw-r--r-- | app/dispatch/views.py | 50 |
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 |
