2015-12-01 15:51:09 +00:00
|
|
|
|
from wtforms import ValidationError
|
2016-04-14 12:00:55 +01:00
|
|
|
|
from notifications_utils.template import Template
|
2016-10-28 10:45:05 +01:00
|
|
|
|
from app.utils import (
|
|
|
|
|
|
Spreadsheet,
|
|
|
|
|
|
is_gov_user
|
|
|
|
|
|
)
|
2016-09-27 11:28:12 +01:00
|
|
|
|
from ._blacklisted_passwords import blacklisted_passwords
|
2015-12-01 15:51:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Blacklist(object):
|
|
|
|
|
|
def __init__(self, message=None):
|
|
|
|
|
|
if not message:
|
|
|
|
|
|
message = 'Password is blacklisted.'
|
|
|
|
|
|
self.message = message
|
|
|
|
|
|
|
|
|
|
|
|
def __call__(self, form, field):
|
2016-09-27 11:28:12 +01:00
|
|
|
|
if field.data in blacklisted_passwords:
|
2015-12-01 15:51:09 +00:00
|
|
|
|
raise ValidationError(self.message)
|
2016-01-07 12:43:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
2016-01-11 15:00:51 +00:00
|
|
|
|
class CsvFileValidator(object):
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, message='Not a csv file'):
|
|
|
|
|
|
self.message = message
|
|
|
|
|
|
|
|
|
|
|
|
def __call__(self, form, field):
|
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
2016-05-05 15:41:11 +01:00
|
|
|
|
if not Spreadsheet.can_handle(field.data.filename):
|
|
|
|
|
|
raise ValidationError("{} isn’t a spreadsheet that Notify can read".format(field.data.filename))
|
2016-03-18 12:05:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
2016-10-28 10:45:05 +01:00
|
|
|
|
class ValidGovEmail(object):
|
2016-03-18 12:05:50 +00:00
|
|
|
|
|
|
|
|
|
|
def __call__(self, form, field):
|
2016-10-28 10:45:05 +01:00
|
|
|
|
from flask import url_for
|
2016-03-18 12:05:50 +00:00
|
|
|
|
message = (
|
|
|
|
|
|
'Enter a central government email address.'
|
|
|
|
|
|
' If you think you should have access'
|
2016-04-22 15:58:17 +01:00
|
|
|
|
' <a href="{}">contact us</a>').format(url_for('main.feedback'))
|
2016-10-28 10:45:05 +01:00
|
|
|
|
if not is_gov_user(field.data.lower()):
|
2016-03-18 12:05:50 +00:00
|
|
|
|
raise ValidationError(message)
|
2016-04-07 16:02:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NoCommasInPlaceHolders():
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, message='You can’t have commas in your fields'):
|
|
|
|
|
|
self.message = message
|
|
|
|
|
|
|
|
|
|
|
|
def __call__(self, form, field):
|
|
|
|
|
|
if ',' in ''.join(Template({'content': field.data}).placeholders):
|
|
|
|
|
|
raise ValidationError(self.message)
|