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
|
2017-02-14 17:06:32 +00:00
|
|
|
|
from notifications_utils.gsm import get_non_gsm_compatible_characters
|
2017-02-13 13:11:29 +00:00
|
|
|
|
|
2017-02-14 17:06:32 +00:00
|
|
|
|
from app import formatted_list
|
2017-02-13 13:11:29 +00:00
|
|
|
|
from app.main._blacklisted_passwords import blacklisted_passwords
|
2016-10-28 10:45:05 +01:00
|
|
|
|
from app.utils import (
|
|
|
|
|
|
Spreadsheet,
|
|
|
|
|
|
is_gov_user
|
|
|
|
|
|
)
|
2015-12-01 15:51:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-02-13 13:11:29 +00:00
|
|
|
|
class Blacklist:
|
2015-12-01 15:51:09 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
2017-02-13 13:11:29 +00:00
|
|
|
|
class CsvFileValidator:
|
2016-01-11 15:00:51 +00:00
|
|
|
|
|
|
|
|
|
|
def __init__(self, message='Not a csv file'):
|
|
|
|
|
|
self.message = message
|
|
|
|
|
|
|
|
|
|
|
|
def __call__(self, form, field):
|
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
|
|
|
|
|
|
|
|
|
|
|
2017-02-13 13:11:29 +00:00
|
|
|
|
class ValidGovEmail:
|
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 = (
|
2017-08-10 13:51:46 +01:00
|
|
|
|
'Enter a government email address.'
|
2016-03-18 12:05:50 +00:00
|
|
|
|
' If you think you should have access'
|
2016-12-12 11:25:43 +00:00
|
|
|
|
' <a href="{}">contact us</a>').format(url_for('main.support'))
|
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
|
|
|
|
|
|
|
|
|
|
|
2017-02-13 13:11:29 +00:00
|
|
|
|
class NoCommasInPlaceHolders:
|
2016-04-07 16:02:06 +01:00
|
|
|
|
|
|
|
|
|
|
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)
|
2017-02-13 13:11:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class OnlyGSMCharacters:
|
|
|
|
|
|
def __call__(self, form, field):
|
2017-02-14 17:06:32 +00:00
|
|
|
|
non_gsm_characters = sorted(list(get_non_gsm_compatible_characters(field.data)))
|
2017-02-13 13:11:29 +00:00
|
|
|
|
if non_gsm_characters:
|
2017-02-14 17:06:32 +00:00
|
|
|
|
raise ValidationError(
|
2017-02-15 16:21:14 +00:00
|
|
|
|
'You can’t use {} in text messages. {} won’t show up properly on everyone’s phones.'.format(
|
|
|
|
|
|
formatted_list(non_gsm_characters, conjunction='or', before_each='', after_each=''),
|
|
|
|
|
|
('It' if len(non_gsm_characters) == 1 else 'They')
|
2017-02-14 17:06:32 +00:00
|
|
|
|
)
|
|
|
|
|
|
)
|