mirror of
https://github.com/wassname/flask-security.git
synced 2026-08-14 12:20:13 +08:00
Simplify routing a tiny bit
This commit is contained in:
+33
-3
@@ -9,9 +9,23 @@
|
||||
:license: MIT, see LICENSE for more details.
|
||||
"""
|
||||
|
||||
from flask import request
|
||||
from flask import request, current_app as app
|
||||
from flask.ext.wtf import Form, TextField, PasswordField, SubmitField, \
|
||||
HiddenField, Required, BooleanField, EqualTo, Email
|
||||
HiddenField, Required, BooleanField, EqualTo, Email, ValidationError
|
||||
from werkzeug.local import LocalProxy
|
||||
|
||||
from .exceptions import UserNotFoundError
|
||||
|
||||
|
||||
# Convenient reference
|
||||
_datastore = LocalProxy(lambda: app.security.datastore)
|
||||
|
||||
|
||||
def valid_user_email(form, field):
|
||||
try:
|
||||
_datastore.find_user(email=field.data)
|
||||
except UserNotFoundError:
|
||||
raise ValidationError('Invalid email address')
|
||||
|
||||
|
||||
class EmailFormMixin():
|
||||
@@ -20,6 +34,13 @@ class EmailFormMixin():
|
||||
Email(message="Invalid email address")])
|
||||
|
||||
|
||||
class UserEmailFormMixin():
|
||||
email = TextField("Email Address",
|
||||
validators=[Required(message="Email not provided"),
|
||||
Email(message="Invalid email address"),
|
||||
valid_user_email])
|
||||
|
||||
|
||||
class PasswordFormMixin():
|
||||
password = PasswordField("Password",
|
||||
validators=[Required(message="Password not provided")])
|
||||
@@ -30,7 +51,16 @@ class PasswordConfirmFormMixin():
|
||||
validators=[EqualTo('password', message="Passwords do not match")])
|
||||
|
||||
|
||||
class ForgotPasswordForm(Form, EmailFormMixin):
|
||||
class ResendConfirmationForm(Form, UserEmailFormMixin):
|
||||
"""The default forgot password form"""
|
||||
|
||||
submit = SubmitField("Resend Confirmation Instructions")
|
||||
|
||||
def to_dict(self):
|
||||
return dict(email=self.email.data)
|
||||
|
||||
|
||||
class ForgotPasswordForm(Form, UserEmailFormMixin):
|
||||
"""The default forgot password form"""
|
||||
|
||||
submit = SubmitField("Recover Password")
|
||||
|
||||
Reference in New Issue
Block a user