diff options
| author | Mitch Riedstra <mitch@riedstra.us> | 2018-01-09 01:26:02 -0500 |
|---|---|---|
| committer | Mitch Riedstra <mitch@riedstra.us> | 2018-01-09 01:26:02 -0500 |
| commit | ec67cee42fd0f8ac989b5b93949938059b661f0c (patch) | |
| tree | 9b7a4c5ae126ac9ce4cd277f044d929a46271d15 | |
| parent | 8bdb452b40e93ad29cca0b98b3d388bfbe5093f4 (diff) | |
| download | dispatch-tracker-ec67cee42fd0f8ac989b5b93949938059b661f0c.tar.gz dispatch-tracker-ec67cee42fd0f8ac989b5b93949938059b661f0c.tar.xz | |
Add the ability to download all of the paperwork from a customer view
| -rw-r--r-- | app/dispatch/models.py | 18 | ||||
| -rw-r--r-- | app/dispatch/templates/dispatch/companies/detail.html | 3 | ||||
| -rw-r--r-- | app/dispatch/templatetags/dynamic_key.py | 4 | ||||
| -rw-r--r-- | app/dispatch/views.py | 37 |
4 files changed, 47 insertions, 15 deletions
diff --git a/app/dispatch/models.py b/app/dispatch/models.py index 58d47fb..b0f76e5 100644 --- a/app/dispatch/models.py +++ b/app/dispatch/models.py @@ -5,7 +5,8 @@ from auditlog.models import AuditlogHistoryField # from django.contrib.auth import get_user_model from datetime import datetime from django.core.exceptions import ObjectDoesNotExist -from .misc import get_week_dates, paperwork_user_directory_path +from .misc import get_week_dates, paperwork_user_directory_path, \ + get_week_dates_datetime from django.core.urlresolvers import reverse import re @@ -24,6 +25,21 @@ class Customer(models.Model): def __str__(self): return self.name + def has_paperwork(self, dt=None): + week_dates = get_week_dates_datetime(dt) + + loads = self.load_set.filter( + date__range=(week_dates['start_date'], + week_dates['end_date'])) + + if len(loads) >= 1: + for l in loads: + if len(l.paperwork_set.all()) >=1: + return True + + return False + + class Load(models.Model): history = AuditlogHistoryField() diff --git a/app/dispatch/templates/dispatch/companies/detail.html b/app/dispatch/templates/dispatch/companies/detail.html index 5dd2414..ffd3c09 100644 --- a/app/dispatch/templates/dispatch/companies/detail.html +++ b/app/dispatch/templates/dispatch/companies/detail.html @@ -9,11 +9,14 @@ <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> + {% load dynamic_key %} + {% if object|customer_has_load_for_datetime:week_dates.start_date %} <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> + {% endif %} </div> <div class="row"> diff --git a/app/dispatch/templatetags/dynamic_key.py b/app/dispatch/templatetags/dynamic_key.py index 91a3a13..ae23704 100644 --- a/app/dispatch/templatetags/dynamic_key.py +++ b/app/dispatch/templatetags/dynamic_key.py @@ -7,6 +7,10 @@ def keyvalue(dictionary, key): return dictionary.get(key) @register.filter +def customer_has_load_for_datetime(cu, dt): + return cu.has_paperwork(dt) + +@register.filter def getindex(my_list, index): return my_list[index] 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): |
