additions for base fields

This commit is contained in:
Kyle Roux 2017-05-19 17:50:09 -07:00
parent c7d3b5c3e8
commit 76c77a6b12

102
forms.py
View File

@ -1,28 +1,49 @@
from flask_wtf import FlaskForm from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField from wtforms import StringField, PasswordField, BooleanField, Form
from wtforms.validators import DataRequired, Email, Length, EqualTo from wtforms.validators import DataRequired, Email, Length, EqualTo
class EmailFieldMixin(Form):
class LoginForm(FlaskForm): """
''' mixin class to provide email field to forms
Form Login """
'''
email = StringField( email = StringField(
'Email', 'Email',
validators=[ validators=[
DataRequired(), DataRequired(),
Email() Email()
] ]
) )
class PasswordFieldMixin(Form):
"""
mixin class to provide password field
"""
def __init__(self, *args, **kwargs):
super(PasswordFieldMixin, self).__init__(*args, **kwargs)
password_field = self._fields.pop('password')
self._fields['password'] = password_field
if 'password_confirm' in self._fields:
self._fields['password'].validators.append(
EqualTo(
'password_confirm',
'Passwords are not the same.'
)
)
confirm_password_field = self._fields.pop('password_confirm')
self._fields['password_confirm'] = confirm_password_field
password = PasswordField( password = PasswordField(
'Password', 'Password',
validators=[ validators=[
DataRequired() DataRequired()
] ]
) )
class LoginForm(FlaskForm, EmailFieldMixin, PasswordFieldMixin):
'''
Form Login
'''
class SignupForm(FlaskForm): class SignupForm(FlaskForm, EmailFieldMixin, PasswordFieldMixin):
''' '''
Form signup Form signup
''' '''
@ -30,63 +51,32 @@ class SignupForm(FlaskForm):
'Username', 'Username',
validators=[ validators=[
DataRequired(), DataRequired(),
Length(5, 30, ''' Length(
You can not have less than 5 characters or more 30. 5, # min
''') 30, # max
] 'You can not have less than 5 characters or more 30.' # error message
) )
email = StringField( ]
'Email', )
validators=[
DataRequired(),
Email(),
Length(1, 254, 'Too long.')
]
)
password = PasswordField(
'Password',
validators=[
DataRequired(),
EqualTo(
'password_confirm',
'Passwords are not the same.'
)
]
)
password_confirm = PasswordField('Repeat password') password_confirm = PasswordField('Repeat password')
accept_tos = BooleanField( accept_tos = BooleanField(
'I accept the terms and conditions.', 'I accept the terms and conditions.',
validators=[ validators=[
DataRequired('Please accept the terms and conditions.') DataRequired('Please accept the terms and conditions.')
] ]
) )
def __init__(self, *args, **kwargs):
super(SignupForm, self).__init__(*args, **kwargs)
tos = self._fields.pop('accept_tos')
self._fields['accept_tos'] = tos
class EmailResetPasswordForm(FlaskForm, EmailFieldMixin):
class EmailResetPasswordForm(FlaskForm):
''' '''
Form send email reset password Form send email reset password
''' '''
email = StringField(
'Email',
validators=[
DataRequired(),
Email()
]
)
class ResetPasswordForm(FlaskForm): class ResetPasswordForm(FlaskForm):
''' '''
Form update password Form update password
''' '''
password = PasswordField(
'Password',
validators=[
DataRequired(),
EqualTo(
'password_confirm',
'Passwords are not the same.'
)
]
)
password_confirm = PasswordField('Repeat password') password_confirm = PasswordField('Repeat password')