aboutsummaryrefslogtreecommitdiff
path: root/app/dispatch/misc.py
diff options
context:
space:
mode:
authorMitch Riedstra <mitch@riedstra.us>2018-02-04 12:31:29 -0500
committerMitch Riedstra <mitch@riedstra.us>2018-02-04 12:31:29 -0500
commit4f2acad7c8ff56bbc41893be3cfaceb9a8d2175f (patch)
treefa9d7246d3e908f272f0c31f8cdb2a57fb84e9d5 /app/dispatch/misc.py
parent7c07b1cd3caaf7d7cc16048cfe1bf230f6123ee4 (diff)
downloaddispatch-tracker-4f2acad7c8ff56bbc41893be3cfaceb9a8d2175f.tar.gz
dispatch-tracker-4f2acad7c8ff56bbc41893be3cfaceb9a8d2175f.tar.xz
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
Diffstat (limited to 'app/dispatch/misc.py')
-rw-r--r--app/dispatch/misc.py24
1 files changed, 19 insertions, 5 deletions
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('_', ' ')