From 4f2acad7c8ff56bbc41893be3cfaceb9a8d2175f Mon Sep 17 00:00:00 2001 From: Mitch Riedstra Date: Sun, 4 Feb 2018 12:31:29 -0500 Subject: Fix issue with averages not being displayed due to an invoice existing. Also fixed filename parsing. Modified django's function and placed it in dispatch/misc.py --- app/dispatch/misc.py | 24 +++++++++++++++++++----- app/dispatch/views.py | 25 +++++++++++++------------ 2 files changed, 32 insertions(+), 17 deletions(-) (limited to 'app/dispatch') diff --git a/app/dispatch/misc.py b/app/dispatch/misc.py index 7bcdaa8..c1d100a 100644 --- a/app/dispatch/misc.py +++ b/app/dispatch/misc.py @@ -1,7 +1,8 @@ from django.utils import formats +from django.utils.encoding import force_text from datetime import datetime, timedelta from dateutil import rrule -import uuid, tempfile, zipfile, os +import uuid, tempfile, zipfile, os, re # Text formatted date. *sigh* def get_week_dates(date=None): @@ -62,11 +63,9 @@ def zip_attachments_for_loads(loads, pre_append, date): print(e) ext = '.pdf' - desc = l.description.replace('"', '') - desc = l.description.replace('/', '--') - # Get the name for the zip file - zipname = desc + '-' + l.user.identity.name + ext + zipname = l.description + '-' + l.user.identity.name + ext + zipname = get_valid_filename(zipname) z.write(p.document.path, zipname) # print(p.document) @@ -78,3 +77,18 @@ def zip_attachments_for_loads(loads, pre_append, date): fh = open(zip_file_name, 'rb') return (zip_file_basename, fh) + + +def get_valid_filename(s): + """ + Specifically modified from django.utils.text to allow spaces in the + filename. Still strips leading and trailing spaces. + + Specifically, leading and trailing spaces are removed and anything that is + not a unicode alphanumeric, dash, underscore, or dot, is removed. + >>> get_valid_filename("john's portrait in 2004.jpg") + 'johns portrait in 2004.jpg' + """ + s = force_text(s).strip().replace(' ', '_') + # Convert back to spaces once we're done because... I'm lazy + return re.sub(r'(?u)[^-\w.]', '', s).replace('_', ' ') diff --git a/app/dispatch/views.py b/app/dispatch/views.py index b16e551..1042d67 100644 --- a/app/dispatch/views.py +++ b/app/dispatch/views.py @@ -18,7 +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, zip_attachments_for_loads +from .misc import get_week_dates, split_loads_by_day, zip_attachments_for_loads, \ + get_valid_filename import re, os, tempfile, datetime User = get_user_model() @@ -156,16 +157,16 @@ class DriverSummary(UserPassesTestMixin, LoadDateSort): loads_by_date = context['loads'] if len(context['related_invoices']) >= 1: context['can_invoice'] = False - else: - for d in loads_by_date: - # Iterate over the array for the given date - for l in loads_by_date[d]: - stats['count'] += 1 - stats['sum'] += l.amount - # Any load not up to par will break the chain - if not l.can_invoice(): - context['can_invoice'] = False + for d in loads_by_date: + # Iterate over the array for the given date + for l in loads_by_date[d]: + stats['count'] += 1 + stats['sum'] += l.amount + + # Any load not up to par will break the chain + if not l.can_invoice(): + context['can_invoice'] = False if stats['sum'] is not 0 and stats['count'] is not 0: stats['average'] = stats['sum']/stats['count'] @@ -472,14 +473,14 @@ def PaperworkDownload(request, load_id, pk): print(e) ext = '.pdf' - desc = load.description.replace('"', '') + filename = get_valid_filename(load.description + ext) response = HttpResponse( fh.read(), content_type='application/force-download') response['Content-Disposition'] = \ - 'attachment; filename="{}"'.format(smart_str(desc + ext)) + 'attachment; filename="{}"'.format(smart_str(filename)) response['X-Sendfile'] = smart_str(paperwork.document.path) return response except Exception as e: -- cgit v1.2.3