mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-19 05:59:44 -04:00
Merge pull request #304 from alphagov/add_valid_email_regexes
Add valid email regexes
This commit is contained in:
@@ -12,9 +12,9 @@ from wtforms import (
|
||||
HiddenField
|
||||
)
|
||||
from wtforms.fields.html5 import EmailField, TelField
|
||||
from wtforms.validators import DataRequired, Email, Length, Regexp
|
||||
from wtforms.validators import (DataRequired, Email, Length, Regexp)
|
||||
|
||||
from app.main.validators import Blacklist, CsvFileValidator
|
||||
from app.main.validators import (Blacklist, CsvFileValidator, ValidEmailDomainRegex)
|
||||
|
||||
from utils.recipients import (
|
||||
validate_phone_number,
|
||||
@@ -24,13 +24,11 @@ from utils.recipients import (
|
||||
|
||||
|
||||
def email_address(label='Email address'):
|
||||
gov_uk_email \
|
||||
= "(^[^@^\\s]+@[^@^\\.^\\s]+(\\.[^@^\\.^\\s]*)*.gov.uk)"
|
||||
return EmailField(label, validators=[
|
||||
Length(min=5, max=255),
|
||||
DataRequired(message='Email cannot be empty'),
|
||||
Email(message='Enter a valid email address'),
|
||||
Regexp(regex=gov_uk_email, message='Enter a gov.uk email address')])
|
||||
ValidEmailDomainRegex()])
|
||||
|
||||
|
||||
class UKMobileNumber(TelField):
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import re
|
||||
from wtforms import ValidationError
|
||||
from datetime import datetime
|
||||
from app.main.encryption import check_hash
|
||||
@@ -22,3 +23,18 @@ class CsvFileValidator(object):
|
||||
def __call__(self, form, field):
|
||||
if not form.file.data.mimetype == 'text/csv':
|
||||
raise ValidationError(self.message)
|
||||
|
||||
|
||||
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(
|
||||
"https://docs.google.com/forms/d/1AL8U-xJX_HAFEiQiJszGQw0PcEaEUnYATSntEghNDGo/viewform")
|
||||
valid_domains = current_app.config.get('EMAIL_DOMAIN_REGEXES', [])
|
||||
email_regex = "(^[^@^\\s]+@[^@^\\.^\\s]+(\\.[^@^\\.^\\s]*)*.({}))".format("|".join(valid_domains))
|
||||
if not re.match(email_regex, field.data):
|
||||
raise ValidationError(message)
|
||||
|
||||
Reference in New Issue
Block a user