mirror of
https://github.com/wassname/flask-security.git
synced 2026-08-03 12:50:24 +08:00
Refactor forms and import other commonly used functions into package
This commit is contained in:
+43
-21
@@ -14,51 +14,73 @@ from flask.ext.wtf import Form, TextField, PasswordField, SubmitField, \
|
||||
HiddenField, Required, BooleanField, EqualTo, Email
|
||||
|
||||
|
||||
class ForgotPasswordForm(Form):
|
||||
class EmailFormMixin():
|
||||
email = TextField("Email Address",
|
||||
validators=[Required(message="Email not provided")])
|
||||
validators=[Required(message="Email not provided"),
|
||||
Email(message="Invalid email address")])
|
||||
|
||||
|
||||
class PasswordFormMixin():
|
||||
password = PasswordField("Password",
|
||||
validators=[Required(message="Password not provided")])
|
||||
|
||||
|
||||
class PasswordConfirmFormMixin():
|
||||
password_confirm = PasswordField("Retype Password",
|
||||
validators=[EqualTo('password', message="Passwords do not match")])
|
||||
|
||||
|
||||
class ForgotPasswordForm(Form, EmailFormMixin):
|
||||
"""The default forgot password form"""
|
||||
|
||||
submit = SubmitField("Recover Password")
|
||||
|
||||
def to_dict(self):
|
||||
return dict(email=self.email.data)
|
||||
|
||||
|
||||
class LoginForm(Form):
|
||||
class LoginForm(Form, EmailFormMixin, PasswordFormMixin):
|
||||
"""The default login form"""
|
||||
|
||||
email = TextField("Email Address",
|
||||
validators=[Required(message="Email not provided")])
|
||||
password = PasswordField("Password",
|
||||
validators=[Required(message="Password not provided")])
|
||||
remember = BooleanField("Remember Me")
|
||||
next = HiddenField()
|
||||
submit = SubmitField("Login")
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(LoginForm, self).__init__(*args, **kwargs)
|
||||
self.next.data = request.args.get('next', None)
|
||||
|
||||
if request.method == 'GET':
|
||||
self.next.data = request.args.get('next', None)
|
||||
|
||||
|
||||
class RegisterForm(Form):
|
||||
class RegisterForm(Form,
|
||||
EmailFormMixin,
|
||||
PasswordFormMixin,
|
||||
PasswordConfirmFormMixin):
|
||||
"""The default register form"""
|
||||
|
||||
email = TextField("Email Address",
|
||||
validators=[Required(message='Email not provided'), Email()])
|
||||
password = PasswordField("Password",
|
||||
validators=[Required(message="Password not provided")])
|
||||
password_confirm = PasswordField("Retype Password",
|
||||
validators=[EqualTo('password', message="Passwords do not match")])
|
||||
submit = SubmitField("Register")
|
||||
|
||||
def to_dict(self):
|
||||
return dict(email=self.email.data, password=self.password.data)
|
||||
|
||||
|
||||
class ResetPasswordForm(Form):
|
||||
class ResetPasswordForm(Form,
|
||||
EmailFormMixin,
|
||||
PasswordFormMixin,
|
||||
PasswordConfirmFormMixin):
|
||||
"""The default reset password form"""
|
||||
|
||||
token = HiddenField(validators=[Required()])
|
||||
email = HiddenField(validators=[Required()])
|
||||
password = PasswordField("Password",
|
||||
validators=[Required(message="Password not provided")])
|
||||
password_confirm = PasswordField("Retype Password",
|
||||
validators=[EqualTo('password', message="Passwords do not match")])
|
||||
|
||||
submit = SubmitField("Reset Password")
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(ResetPasswordForm, self).__init__(*args, **kwargs)
|
||||
|
||||
if request.method == 'GET':
|
||||
self.token.data = request.args.get('token', None)
|
||||
self.email.data = request.args.get('email', None)
|
||||
|
||||
def to_dict(self):
|
||||
return dict(token=self.token.data,
|
||||
|
||||
Reference in New Issue
Block a user