Handle files that can’t be interpreted as spreadsheets

There shouldn’t be a case where we see a `ValueError` on upload any
more. Our file handling should be robust enough to deal with whatever is
thrown at it.

This commit:
- adds test files with bad data (PNG files with their extensions changed to look
  like spreadsheets)
- catches whatever exceptions are raised by trying to parse these files
- returns a helpful flash message to the user

Anything else should raise a `500`, eg if the file can’t be uploaded to S3.
This commit is contained in:
Chris Hill-Scott
2016-05-13 21:25:59 +01:00
parent 1409ca36ca
commit 7bbc307a3e
8 changed files with 34 additions and 16 deletions

View File

@@ -4,6 +4,8 @@ import json
import uuid
import itertools
from contextlib import suppress
from zipfile import BadZipFile
from xlrd.biffh import XLRDError
from flask import (
request,
@@ -123,10 +125,10 @@ def send_messages(service_id, template_id):
service_id=service_id,
upload_id=upload_id,
template_type=template.template_type))
except ValueError as e:
flash('There was a problem uploading: {}'.format(form.file.data.filename))
flash(str(e))
return redirect(url_for('.send_messages', service_id=service_id, template_id=template_id))
except (UnicodeDecodeError, BadZipFile, XLRDError):
flash('Couldnt read {}. Try using a different file format.'.format(
form.file.data.filename
))
return render_template(
'views/send.html',