mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-02-06 03:13:42 -05:00
This macro: - accepts a WTForm form field as a parameter - renders a form field which follows the GOV.UK Elements patterns, both visually and in markup terms It then changes any page which uses either: - the old, non-WTForms macro or - the old, WTFforms `render_field` macro …to use this new macro and removes both of the old ones. It also adds the option to display hint text above the textbox.
143 lines
4.8 KiB
Python
143 lines
4.8 KiB
Python
from flask_wtf import Form
|
|
from wtforms import StringField, PasswordField, ValidationError, TextAreaField
|
|
from wtforms.validators import DataRequired, Email, Length, Regexp
|
|
from app.main.validators import Blacklist, ValidateUserCodes
|
|
|
|
|
|
def email_address():
|
|
gov_uk_email \
|
|
= "(^[^@^\\s]+@[^@^\\.^\\s]+(\\.[^@^\\.^\\s]*)*.gov.uk)"
|
|
return StringField('Email address', 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')])
|
|
|
|
|
|
def mobile_number():
|
|
mobile_number_regex = "^\\+44[\\d]{10}$"
|
|
return StringField('Mobile phone number',
|
|
validators=[DataRequired(message='Mobile number can not be empty'),
|
|
Regexp(regex=mobile_number_regex, message='Enter a +44 mobile number')])
|
|
|
|
|
|
def password():
|
|
return PasswordField('Create a password',
|
|
validators=[DataRequired(message='Password can not be empty'),
|
|
Length(10, 255, message='Password must be at least 10 characters'),
|
|
Blacklist(message='That password is blacklisted, too common')])
|
|
|
|
|
|
def sms_code():
|
|
verify_code = '^\d{5}$'
|
|
return StringField('Text message confirmation code',
|
|
validators=[DataRequired(message='Text message confirmation code can not be empty'),
|
|
Regexp(regex=verify_code,
|
|
message='Text message confirmation code must be 5 digits'),
|
|
ValidateUserCodes(code_type='sms')])
|
|
|
|
|
|
def email_code():
|
|
verify_code = '^\d{5}$'
|
|
return StringField("Email confirmation code",
|
|
validators=[DataRequired(message='Email confirmation code can not be empty'),
|
|
Regexp(regex=verify_code, message='Email confirmation code must be 5 digits'),
|
|
ValidateUserCodes(code_type='email')])
|
|
|
|
|
|
class LoginForm(Form):
|
|
email_address = StringField('Email address', validators=[
|
|
Length(min=5, max=255),
|
|
DataRequired(message='Email cannot be empty'),
|
|
Email(message='Enter a valid email address')
|
|
])
|
|
password = PasswordField('Password', validators=[
|
|
DataRequired(message='Enter your password')
|
|
])
|
|
|
|
|
|
class RegisterUserForm(Form):
|
|
def __init__(self, existing_email_addresses, *args, **kwargs):
|
|
self.existing_emails = existing_email_addresses
|
|
super(RegisterUserForm, self).__init__(*args, **kwargs)
|
|
|
|
name = StringField('Full name',
|
|
validators=[DataRequired(message='Name can not be empty')])
|
|
email_address = email_address()
|
|
mobile_number = mobile_number()
|
|
password = password()
|
|
|
|
def validate_email_address(self, field):
|
|
# Validate email address is unique.
|
|
if self.existing_emails(field.data):
|
|
raise ValidationError('Email address already exists')
|
|
|
|
|
|
class TwoFactorForm(Form):
|
|
def __init__(self, user_codes, *args, **kwargs):
|
|
'''
|
|
Keyword arguments:
|
|
user_codes -- List of user code objects which have the fields
|
|
(code_type, expiry_datetime, code)
|
|
'''
|
|
self.user_codes = user_codes
|
|
super(TwoFactorForm, self).__init__(*args, **kwargs)
|
|
|
|
sms_code = sms_code()
|
|
|
|
|
|
class VerifyForm(Form):
|
|
def __init__(self, user_codes, *args, **kwargs):
|
|
'''
|
|
Keyword arguments:
|
|
user_codes -- List of user code objects which have the fields
|
|
(code_type, expiry_datetime, code)
|
|
'''
|
|
self.user_codes = user_codes
|
|
super(VerifyForm, self).__init__(*args, **kwargs)
|
|
|
|
sms_code = sms_code()
|
|
email_code = email_code()
|
|
|
|
|
|
class EmailNotReceivedForm(Form):
|
|
email_address = email_address()
|
|
|
|
|
|
class TextNotReceivedForm(Form):
|
|
mobile_number = mobile_number()
|
|
|
|
|
|
class AddServiceForm(Form):
|
|
def __init__(self, service_names, *args, **kwargs):
|
|
self.service_names = service_names
|
|
super(AddServiceForm, self).__init__(*args, **kwargs)
|
|
|
|
service_name = StringField(validators=[
|
|
DataRequired(message='Service name can not be empty')])
|
|
|
|
def validate_service_name(self, a):
|
|
if self.service_name.data in self.service_names:
|
|
raise ValidationError('Service name already exists')
|
|
|
|
|
|
class ServiceNameForm(Form):
|
|
service_name = StringField(u'New name')
|
|
|
|
|
|
class ConfirmPasswordForm(Form):
|
|
password = PasswordField(u'Enter password')
|
|
|
|
|
|
class TemplateForm(Form):
|
|
template_name = StringField(u'Template name')
|
|
template_body = TextAreaField(u'Message')
|
|
|
|
|
|
class ForgotPasswordForm(Form):
|
|
email_address = email_address()
|
|
|
|
|
|
class NewPasswordForm(Form):
|
|
new_password = password()
|