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 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) (limited to 'app/dispatch/misc.py') 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('_', ' ') -- cgit v1.2.3