Files
notifications-admin/app/main/validators.py
Nicholas Staples f581ff44d0 Only check on csv is the file extension.
Update validator logic

Update message for the validator.
2016-05-03 10:10:58 +01:00

49 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import re
from wtforms import ValidationError
from notifications_utils.template import Template
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)
class CsvFileValidator(object):
def __init__(self, message='Not a csv file'):
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))
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'
' <a href="{}">contact us</a>').format(url_for('main.feedback'))
valid_domains = current_app.config.get('EMAIL_DOMAIN_REGEXES', [])
email_regex = "[^\@^\s]+@([^@^\\.^\\s]+\.)*({})$".format("|".join(valid_domains))
if not re.match(email_regex, field.data.lower()):
raise ValidationError(message)
class NoCommasInPlaceHolders():
def __init__(self, message='You cant 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)