jasminefiner
9/26/2018 - 10:01 AM

06

UMS Email Password Reset 6

# app/auth_blueprint/views.py
# ...
from .forms import ChangePasswordForm
# ...
@auth.route('/change_password', methods=['GET', 'POST'])
@login_required
def change_password():
    form = ChangePasswordForm()
    if form.validate_on_submit():
        if current_user.verify_password(form.oldpassword.data):
            current_user.change_password(form.newpassword.data)
            db.session.add(current_user)
            db.session.commit()
            flash('Your password has been updated!', 'success')
            return redirect(url_for('main.index'))
        flash('Your password is incorrect.', 'danger')
    for field, errors in form.errors.items():
        for error in errors:
            flash(u"Error in the %s field - %s" % (
                getattr(form, field).label.text,
                error
            ), 'danger')
    return render_template('auth/change_password.html',
                           form=form)