Refactor forms and import other commonly used functions into package

This commit is contained in:
Matt Wright
2012-06-19 11:55:23 -04:00
parent 1f7c2625f7
commit a902530773
4 changed files with 51 additions and 22 deletions
+2
View File
@@ -10,6 +10,8 @@
:license: MIT, see LICENSE for more details.
"""
from flask.ext.login import login_user, logout_user
from .core import Security, RoleMixin, UserMixin, AnonymousUser, \
AuthenticationProvider, current_user
from .decorators import auth_token_required, http_auth_required, \
+43 -21
View File
@@ -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,
+1
View File
@@ -35,6 +35,7 @@ def find_user_by_reset_token(token):
def send_reset_password_instructions(user):
url = url_for('flask_security.reset',
email=user.email,
reset_token=user.reset_password_token)
reset_link = request.url_root[:-1] + url
+5 -1
View File
@@ -27,8 +27,12 @@ class DefaultSecurityTests(SecurityTest):
r = self.authenticate(password="")
self.assertIn("Password not provided", r.data)
def test_invalid_user(self):
def test_invalid_email(self):
r = self.authenticate(email="bogus")
self.assertIn("Invalid email address", r.data)
def test_invalid_user(self):
r = self.authenticate(email="bogus@bogus.com")
self.assertIn("Specified user does not exist", r.data)
def test_bad_password(self):