2016-03-18 12:05:50 +00:00
|
|
|
|
import re
|
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
|
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):
|
|
|
|
|
|
if field.data in ['password1234', 'passw0rd1234']:
|
|
|
|
|
|
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):
|
2016-04-29 15:40:35 +01:00
|
|
|
|
if not field.data.filename.lower().endswith('.csv'):
|
|
|
|
|
|
raise ValidationError("{} is not a CSV file".format(field.data.filename))
|
2016-03-18 12:05:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ValidEmailDomainRegex(object):
|
|
|
|
|
|
|
|
|
|
|
|
def __call__(self, form, field):
|
|
|
|
|
|
from flask import (current_app, url_for)
|
|
|
|
|
|
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-03-18 12:05:50 +00:00
|
|
|
|
valid_domains = current_app.config.get('EMAIL_DOMAIN_REGEXES', [])
|
2016-04-06 11:01:22 +01:00
|
|
|
|
email_regex = "[^\@^\s]+@([^@^\\.^\\s]+\.)*({})$".format("|".join(valid_domains))
|
2016-04-06 16:45:35 +01:00
|
|
|
|
if not re.match(email_regex, 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)
|