mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-02-05 02:42:26 -05:00
> If a user tries to save a template containing something like > ((name,date)) we should give a validation error. This is because it causes havoc with the column headers in CSV files. https://www.pivotaltracker.com/story/show/117043389
52 lines
1.7 KiB
Python
52 lines
1.7 KiB
Python
import re
|
||
from wtforms import ValidationError
|
||
from datetime import datetime
|
||
from app.main.encryption import check_hash
|
||
from 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 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]+\.)*({})$".format("|".join(valid_domains))
|
||
if not re.match(email_regex, field.data.lower()):
|
||
raise ValidationError(message)
|
||
|
||
|
||
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)
|