diff options
| author | Mitch Riedstra <mitch@riedstra.us> | 2018-01-23 18:51:57 -0500 |
|---|---|---|
| committer | Mitch Riedstra <mitch@riedstra.us> | 2018-01-23 18:51:57 -0500 |
| commit | 78437d6f8afd75544a94d033ee4fb38dfe7c1914 (patch) | |
| tree | 0047b3f0791a55ea269cc15e11427b7af23e5b87 /app/dispatch/views.py | |
| parent | af3128b01e0b537ba21dd9a2bc819d6404a8fc9f (diff) | |
| download | dispatch-tracker-78437d6f8afd75544a94d033ee4fb38dfe7c1914.tar.gz dispatch-tracker-78437d6f8afd75544a94d033ee4fb38dfe7c1914.tar.xz | |
Move paperwork download code out into a function and add ability to download paperwork for users
Diffstat (limited to 'app/dispatch/views.py')
| -rw-r--r-- | app/dispatch/views.py | 56 |
1 files changed, 21 insertions, 35 deletions
diff --git a/app/dispatch/views.py b/app/dispatch/views.py index ef5bad8..f01cb31 100644 --- a/app/dispatch/views.py +++ b/app/dispatch/views.py @@ -18,8 +18,8 @@ from dispatch.forms import AddPaperworkForm, InviteForm 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 re, os, tempfile, datetime, shutil, zipfile +from .misc import get_week_dates, split_loads_by_day, zip_attachments_for_loads +import re, os, tempfile, datetime User = get_user_model() @@ -156,8 +156,6 @@ class DriverSummary(UserPassesTestMixin, LoadDateSort): stats['sum'] += l.amount # Any load not up to par will break the chain - print(l) - print(l.can_invoice()) if not l.can_invoice(): context['can_invoice'] = False @@ -235,57 +233,45 @@ 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'])) + basename, fh = zip_attachments_for_loads(loads, customer.name, week_dates['start_date']) - 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('"', '') + response = HttpResponse( + fh.read(), + content_type='application/force-download') - # Get the name for the zip file - zipname = desc + '-' + l.user.identity.name + ext + response['Content-Disposition'] = \ + 'attachment; filename="{}"'.format(smart_str(basename)) + response['X-Sendfile'] = smart_str(fh) - z.write(p.document.path, zipname) - # print(p.document) + print('%r' % response) + return response - # Finally close off the zip file - z.close() +def DriverDownload(request, pk): + print(dir(request)) + user = User.objects.get(pk=pk) + week_dates = get_week_dates(request.GET.get('date', None)) + loads = user.load_set.filter( + date__range=(week_dates['start_date'], + week_dates['end_date'])) - fh = open(zip_file_name, 'rb') + basename, fh = zip_attachments_for_loads(loads, user.identity.name, week_dates['start_date']) response = HttpResponse( fh.read(), content_type='application/force-download') response['Content-Disposition'] = \ - 'attachment; filename="{}"'.format(smart_str(zip_file_basename)) + 'attachment; filename="{}"'.format(smart_str(basename)) response['X-Sendfile'] = smart_str(fh) - print('%r' % response) + # print('%r' % response) return response - class CustomerUpdate(UserPassesTestMixin, UpdateView): template_name = "dispatch/companies/edit.html" model = Customer |
