Accept common spreadsheet formats, not just CSV

We require users to export their spreadsheets as CSV files before
uploading them. But this seems like the sort of thing a computer should
be able to do.

So this commit adds a wrapper class which:
- takes a the uploaded file
- returns it in a normalised format, or reads it using pyexcel[1]
- gives the data back in CSV format

This allows us to accept `.csv`, `.xlsx`, `.xls` (97 and 95), `.ods`,
`.xlsm` and `.tsv` files. We can upload the resultant CSV just like
normal, and process it for errors as before.

Testing
---

To test this I’ve added a selection of common spreadsheet files as test
data. They all contain the same data, so the tests look to see that the
resultant CSV output is the same for each.

UI changes
---

This commit doesn’t change the UI, apart from to give a different error
message if a user uploads a file type that we still don’t understand.

I intend to do this as a separate pull request, in order to fulfil
https://www.pivotaltracker.com/story/show/119371637
This commit is contained in:
Chris Hill-Scott
2016-05-05 15:41:11 +01:00
parent a348bce191
commit 1409ca36ca
13 changed files with 111 additions and 9 deletions

View File

@@ -1,6 +1,7 @@
import re
from wtforms import ValidationError
from notifications_utils.template import Template
from app.utils import Spreadsheet
class Blacklist(object):
@@ -20,8 +21,8 @@ class CsvFileValidator(object):
self.message = message
def __call__(self, form, field):
if not field.data.filename.lower().endswith('.csv'):
raise ValidationError("{} is not a CSV file".format(field.data.filename))
if not Spreadsheet.can_handle(field.data.filename):
raise ValidationError("{} isnt a spreadsheet that Notify can read".format(field.data.filename))
class ValidEmailDomainRegex(object):

View File

@@ -27,7 +27,7 @@ from app.main.uploader import (
s3download
)
from app import job_api_client, service_api_client, current_service, user_api_client, statistics_api_client
from app.utils import user_has_permissions, get_errors_for_csv
from app.utils import user_has_permissions, get_errors_for_csv, Spreadsheet
def get_send_button_text(template_type, number_of_messages):
@@ -112,10 +112,7 @@ def send_messages(service_id, template_id):
s3upload(
upload_id,
service_id,
{
'file_name': form.file.data.filename,
'data': form.file.data.read().decode('utf-8')
},
Spreadsheet.from_file(form.file.data.filename, form.file.data).as_dict,
current_app.config['AWS_REGION']
)
session['upload_data'] = {