def validate_current_password(form, field):
"""
Validates current password entered with the password stored in
database
:param form: The form which is being passed in
:type form: AccountForm
:param field: The data value for the 'password' entered by User
:type field : PasswordField
"""
if form.user is not None:
if not form.user.is_password_valid(field.data):
raise ValidationError('Invalid password')
else:
raise ValidationError('User instance not passed to form '
'validation')
python类PasswordField()的实例源码
def validate_new_password_repeat(form, field):
"""
Validates new password repeat and checks if it matches 'new_password'
:param form: The form which is being passed in
:type form: AccountForm
:param field: The data value for the 'password' entered by User
:type field : PasswordField
"""
if form.email is not None:
# Email form is present, so it's optional
if len(field.data) == 0 and len(form.new_password.data) == 0:
return
if field.data != form.new_password.data:
raise ValidationError('The password needs to match the new '
'password')
def valid_password(form, field):
"""
Function to check for validity of a password
:param form: The form which is being passed in
:type form: Form
:param field: The data value for the 'password' inserted by User
:type field : PasswordField
"""
if len(field.data) == 0:
raise ValidationError('new password cannot be empty')
if len(field.data) < 10 or len(field.data) > 500:
raise ValidationError('Password needs to be between 10 and 500 '
'characters long (you entered %s characters'
% len(field.data))
def validate_password_repeat(form, field):
"""
Validates if the repeated password is the same as 'password'
:param form: The form which is being passed in
:type form: CompleteSignupForm
:param field : The data value for the 'password' entered by User
:type field : PasswordField
"""
if field.data != form.password.data:
raise ValidationError('The password needs to match the new '
'password')
def validate_new_password(form, field):
"""
Validates the new password entered
:param form: The form which is being passed in
:type form: AccountForm
:param field: The data value for the 'password' entered by User
:type field : PasswordField
"""
if len(field.data) == 0 and len(form.new_password_repeat.data) == 0:
return
valid_password(form, field)
def scaffold_form(self):
form_class = super(UserAdmin, self).scaffold_form()
form_class.password = PasswordField()
return form_class