import re
from notifications_utils.field import Field
from notifications_utils.formatters import formatted_list
from notifications_utils.recipients import (
InvalidEmailError,
validate_email_address,
)
from notifications_utils.sanitise_text import SanitiseSMS
from notifications_utils.template import BroadcastMessageTemplate
from wtforms import ValidationError
from app.main._commonly_used_passwords import commonly_used_passwords
from app.models.spreadsheet import Spreadsheet
from app.utils.user import is_gov_user
class CommonlyUsedPassword:
def __init__(self, message=None):
if not message:
message = 'Password is in list of commonly used passwords.'
self.message = message
def __call__(self, form, field):
if field.data in commonly_used_passwords:
raise ValidationError(self.message)
class CsvFileValidator:
def __init__(self, message='Not a csv file'):
self.message = message
def __call__(self, form, field):
if not Spreadsheet.can_handle(field.data.filename):
raise ValidationError("{} is not a spreadsheet that Notify can read".format(field.data.filename))
class ValidGovEmail:
def __call__(self, form, field):
if field.data == '':
return
from flask import url_for
message = '''
Enter a public sector email address or
find out who can use Notify
'''.format(url_for('main.who_can_use_notify'))
if not is_gov_user(field.data.lower()):
raise ValidationError(message)
class ValidEmail:
message = 'Enter a valid email address'
def __call__(self, form, field):
if field.data == '':
return
try:
validate_email_address(field.data)
except InvalidEmailError:
raise ValidationError(self.message)
class NoCommasInPlaceHolders:
def __init__(self, message='You cannot put commas between double brackets'):
self.message = message
def __call__(self, form, field):
if ',' in ''.join(Field(field.data).placeholders):
raise ValidationError(self.message)
class NoEmbeddedImagesInSVG:
def __init__(self, message='This SVG has an embedded raster image in it and will not render well'):
self.message = message
def __call__(self, form, field):
is_image_embedded = '