flask-login-example/forms.py

83 lines
2.3 KiB
Python
Raw Normal View History

2017-05-17 20:16:30 +02:00
from flask_wtf import FlaskForm
2017-05-20 02:50:09 +02:00
from wtforms import StringField, PasswordField, BooleanField, Form
2017-05-17 20:16:30 +02:00
from wtforms.validators import DataRequired, Email, Length, EqualTo
2017-05-20 02:50:09 +02:00
class EmailFieldMixin(Form):
"""
mixin class to provide email field to forms
"""
2017-05-18 18:25:54 +02:00
email = StringField(
'Email',
validators=[
DataRequired(),
Email()
2017-05-20 02:50:09 +02:00
]
)
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
2017-05-18 18:25:54 +02:00
password = PasswordField(
'Password',
validators=[
DataRequired()
2017-05-20 02:50:09 +02:00
]
)
2017-05-17 20:16:30 +02:00
2017-05-20 02:50:09 +02:00
class LoginForm(FlaskForm, EmailFieldMixin, PasswordFieldMixin):
'''
Form Login
'''
class SignupForm(FlaskForm, EmailFieldMixin, PasswordFieldMixin):
2017-05-18 18:25:54 +02:00
'''
Form signup
'''
username = StringField(
'Username',
validators=[
DataRequired(),
2017-05-20 02:50:09 +02:00
Length(
5, # min
30, # max
'You can not have less than 5 characters or more 30.' # error message
)
]
)
2017-05-18 18:25:54 +02:00
password_confirm = PasswordField('Repeat password')
accept_tos = BooleanField(
'I accept the terms and conditions.',
validators=[
DataRequired('Please accept the terms and conditions.')
2017-05-20 02:50:09 +02:00
]
)
def __init__(self, *args, **kwargs):
super(SignupForm, self).__init__(*args, **kwargs)
tos = self._fields.pop('accept_tos')
self._fields['accept_tos'] = tos
2017-05-17 20:16:30 +02:00
2017-05-20 02:50:09 +02:00
class EmailResetPasswordForm(FlaskForm, EmailFieldMixin):
2017-05-18 18:25:54 +02:00
'''
Form send email reset password
'''
2017-05-17 20:16:30 +02:00
class ResetPasswordForm(FlaskForm):
2017-05-18 18:25:54 +02:00
'''
Form update password
2017-05-20 02:50:09 +02:00
'''
2017-05-18 18:25:54 +02:00
password_confirm = PasswordField('Repeat password')