2016-01-12 18:10:16 +00:00
|
|
|
import re
|
2015-11-27 09:47:29 +00:00
|
|
|
from flask_wtf import Form
|
2016-01-12 10:43:23 +00:00
|
|
|
|
|
|
|
|
from wtforms import (
|
|
|
|
|
StringField,
|
|
|
|
|
PasswordField,
|
|
|
|
|
ValidationError,
|
|
|
|
|
TextAreaField,
|
|
|
|
|
FileField
|
|
|
|
|
)
|
2015-12-01 13:23:54 +00:00
|
|
|
from wtforms.validators import DataRequired, Email, Length, Regexp
|
2016-01-11 15:00:51 +00:00
|
|
|
|
|
|
|
|
from app.main.validators import Blacklist, ValidateUserCodes, CsvFileValidator
|
|
|
|
|
from app.main.dao import verify_codes_dao
|
|
|
|
|
from app.main.encryption import check_hash
|
2015-12-01 15:51:09 +00:00
|
|
|
|
2015-11-27 09:47:29 +00:00
|
|
|
|
2016-01-08 12:00:52 +00:00
|
|
|
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')])
|
|
|
|
|
|
|
|
|
|
|
2016-01-12 18:10:16 +00:00
|
|
|
class UKMobileNumber(StringField):
|
|
|
|
|
|
|
|
|
|
def pre_validate(self, form):
|
|
|
|
|
|
|
|
|
|
self.data = self.data.replace('(', '')
|
|
|
|
|
self.data = self.data.replace(')', '')
|
|
|
|
|
self.data = self.data.replace(' ', '')
|
|
|
|
|
self.data = self.data.replace('-', '')
|
|
|
|
|
|
|
|
|
|
if self.data.startswith('+'):
|
|
|
|
|
self.data = self.data[1:]
|
|
|
|
|
|
2016-01-12 20:55:46 +00:00
|
|
|
if not sum(
|
2016-01-12 18:10:16 +00:00
|
|
|
self.data.startswith(prefix) for prefix in ['07', '447', '4407', '00447']
|
2016-01-12 20:55:46 +00:00
|
|
|
):
|
|
|
|
|
raise ValidationError('Must be a UK mobile number (eg 07700 900460)')
|
2016-01-12 18:10:16 +00:00
|
|
|
|
|
|
|
|
for digit in self.data:
|
|
|
|
|
try:
|
|
|
|
|
int(digit)
|
|
|
|
|
except(ValueError):
|
|
|
|
|
raise ValidationError('Must not contain letters or symbols')
|
|
|
|
|
|
|
|
|
|
self.data = self.data.split('7', 1)[1]
|
|
|
|
|
|
|
|
|
|
if len(self.data) > 9:
|
|
|
|
|
raise ValidationError('Too many digits')
|
|
|
|
|
|
|
|
|
|
if len(self.data) < 9:
|
2016-01-12 20:55:46 +00:00
|
|
|
raise ValidationError('Not enough digits')
|
2016-01-12 18:10:16 +00:00
|
|
|
|
|
|
|
|
def post_validate(self, form, validation_stopped):
|
|
|
|
|
|
|
|
|
|
if len(self.data) != 9:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
self.data = '+44 7{} {} {}'.format(*re.findall('...', self.data))
|
|
|
|
|
|
|
|
|
|
|
2016-01-08 12:00:52 +00:00
|
|
|
def mobile_number():
|
2016-01-13 09:26:38 +00:00
|
|
|
return UKMobileNumber('Mobile phone number',
|
|
|
|
|
validators=[DataRequired(message='Cannot be empty')])
|
2016-01-08 12:00:52 +00:00
|
|
|
|
|
|
|
|
|
2016-01-12 11:25:46 +00:00
|
|
|
def password(label='Create a password'):
|
|
|
|
|
return PasswordField(label,
|
2016-01-08 12:00:52 +00:00
|
|
|
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')])
|
|
|
|
|
|
|
|
|
|
|
2015-11-27 09:47:29 +00:00
|
|
|
class LoginForm(Form):
|
|
|
|
|
email_address = StringField('Email address', validators=[
|
2015-11-27 16:25:56 +00:00
|
|
|
Length(min=5, max=255),
|
2015-11-27 09:47:29 +00:00
|
|
|
DataRequired(message='Email cannot be empty'),
|
2016-01-08 12:00:52 +00:00
|
|
|
Email(message='Enter a valid email address')
|
2015-11-27 09:47:29 +00:00
|
|
|
])
|
2015-12-02 15:23:03 +00:00
|
|
|
password = PasswordField('Password', validators=[
|
2016-01-08 12:00:52 +00:00
|
|
|
DataRequired(message='Enter your password')
|
2015-11-27 09:47:29 +00:00
|
|
|
])
|
2015-12-01 13:23:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class RegisterUserForm(Form):
|
2016-01-08 15:12:14 +00:00
|
|
|
def __init__(self, existing_email_addresses, *args, **kwargs):
|
2016-01-05 12:41:20 +00:00
|
|
|
self.existing_emails = existing_email_addresses
|
|
|
|
|
super(RegisterUserForm, self).__init__(*args, **kwargs)
|
|
|
|
|
|
2015-12-02 15:23:03 +00:00
|
|
|
name = StringField('Full name',
|
2015-12-01 13:23:54 +00:00
|
|
|
validators=[DataRequired(message='Name can not be empty')])
|
2016-01-08 12:00:52 +00:00
|
|
|
email_address = email_address()
|
|
|
|
|
mobile_number = mobile_number()
|
|
|
|
|
password = password()
|
2015-12-04 16:21:01 +00:00
|
|
|
|
2016-01-05 12:41:20 +00:00
|
|
|
def validate_email_address(self, field):
|
|
|
|
|
# Validate email address is unique.
|
2016-01-08 15:12:14 +00:00
|
|
|
if self.existing_emails(field.data):
|
2016-01-05 12:41:20 +00:00
|
|
|
raise ValidationError('Email address already exists')
|
|
|
|
|
|
2015-12-04 16:21:01 +00:00
|
|
|
|
2015-12-07 16:56:11 +00:00
|
|
|
class TwoFactorForm(Form):
|
2016-01-07 12:43:10 +00:00
|
|
|
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)
|
|
|
|
|
|
2016-01-08 12:00:52 +00:00
|
|
|
sms_code = sms_code()
|
2015-12-08 12:36:54 +00:00
|
|
|
|
2015-12-07 16:56:11 +00:00
|
|
|
|
2015-12-04 16:21:01 +00:00
|
|
|
class VerifyForm(Form):
|
2016-01-07 12:43:10 +00:00
|
|
|
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)
|
|
|
|
|
|
2016-01-08 12:00:52 +00:00
|
|
|
sms_code = sms_code()
|
|
|
|
|
email_code = email_code()
|
2015-12-14 17:12:28 +00:00
|
|
|
|
|
|
|
|
|
2015-12-15 15:35:30 +00:00
|
|
|
class EmailNotReceivedForm(Form):
|
2016-01-08 12:00:52 +00:00
|
|
|
email_address = email_address()
|
2015-12-15 15:35:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class TextNotReceivedForm(Form):
|
2016-01-08 12:00:52 +00:00
|
|
|
mobile_number = mobile_number()
|
2015-12-15 15:35:30 +00:00
|
|
|
|
|
|
|
|
|
2015-12-14 17:12:28 +00:00
|
|
|
class AddServiceForm(Form):
|
2016-01-15 16:10:24 +00:00
|
|
|
def __init__(self, names_func, *args, **kwargs):
|
|
|
|
|
"""
|
|
|
|
|
Keyword arguments:
|
|
|
|
|
names_func -- Returns a list of unique service_names already registered
|
|
|
|
|
on the system.
|
|
|
|
|
"""
|
|
|
|
|
self._names_func = names_func
|
2016-01-04 15:31:50 +00:00
|
|
|
super(AddServiceForm, self).__init__(*args, **kwargs)
|
|
|
|
|
|
2016-01-05 17:08:50 +00:00
|
|
|
service_name = StringField(validators=[
|
2016-01-08 12:00:52 +00:00
|
|
|
DataRequired(message='Service name can not be empty')])
|
2015-12-15 10:17:43 +00:00
|
|
|
|
|
|
|
|
def validate_service_name(self, a):
|
2016-01-15 16:10:24 +00:00
|
|
|
if self.service_name.data in self._names_func():
|
2016-01-04 15:31:50 +00:00
|
|
|
raise ValidationError('Service name already exists')
|
2016-01-04 14:00:39 +00:00
|
|
|
|
|
|
|
|
|
2016-01-11 13:15:10 +00:00
|
|
|
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')
|
|
|
|
|
|
|
|
|
|
|
2016-01-05 17:52:09 +00:00
|
|
|
class ForgotPasswordForm(Form):
|
2016-01-08 12:00:52 +00:00
|
|
|
email_address = email_address()
|
2016-01-04 14:00:39 +00:00
|
|
|
|
|
|
|
|
|
2016-01-06 17:37:07 +00:00
|
|
|
class NewPasswordForm(Form):
|
2016-01-08 12:00:52 +00:00
|
|
|
new_password = password()
|
2016-01-11 15:00:51 +00:00
|
|
|
|
|
|
|
|
|
2016-01-12 11:25:46 +00:00
|
|
|
class ChangePasswordForm(Form):
|
|
|
|
|
old_password = password('Current password')
|
|
|
|
|
new_password = password('New password')
|
|
|
|
|
|
|
|
|
|
|
2016-01-11 15:00:51 +00:00
|
|
|
class CsvUploadForm(Form):
|
2016-01-12 10:43:23 +00:00
|
|
|
file = FileField('File to upload', validators=[DataRequired(
|
|
|
|
|
message='Please pick a file'), CsvFileValidator()])
|
2016-01-12 10:28:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class ChangeNameForm(Form):
|
|
|
|
|
new_name = StringField(u'Your name')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ChangeEmailForm(Form):
|
|
|
|
|
email_address = email_address()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ConfirmEmailForm(Form):
|
|
|
|
|
email_code = email_code()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ChangeMobileNumberForm(Form):
|
|
|
|
|
mobile_number = mobile_number()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ConfirmMobileNumberForm(Form):
|
|
|
|
|
sms_code = sms_code()
|