def cancel_account_request():
    form = CancelAccoutForm()
    if form.validate_on_submit():
        if current_user.verify(form.password.data):
            email = current_user.email
            token = current_user.generate_token_for_cancel_account()
            send_mail(email, 'Confirm to cancel account', 'auth/email/cancel_account', user=current_user, token=token)
            flash('An email with instructions to confirm canceling your account address has been sent to you.')
            return redirect(url_for('main.index'))
        else:
            flash('Invalid password.')
    return render_template('auth/cancel_account.html', form=form, title='Cancel Account',
                           current_time=datetime.utcnow())
def change_password():
    form = ChangePasswordForm()
    if form.validate_on_submit():
        if current_user.verify(form.old_password.data):
            current_user.password = form.password.data
            db.session.add(current_user)
            db.session.commit()
            flash('You haved updated your password.')
            return redirect(url_for('main.index'))
        else:
            flash('Your password is not correct.')
    return render_template('auth/change_password.html', form=form, title='Change Password',
                           current_time=datetime.utcnow())
def change_email_request():
    form = ChangeEmailForm()
    if form.validate_on_submit():
        if current_user.verify(form.password.data):
            new_email = form.email.data
            token = current_user.generate_token_for_change_email(new_email)
            send_mail(new_email, 'Confirm your email address', 'auth/email/change_email', user=current_user,
                      token=token)
            flash('An email with instructions to confirm your new email address has been sent to you.')
            return redirect(url_for('auth.login'))
        else:
            flash('Invalid email or password')
    return render_template('auth/change_email.html', form=form, title='Change Email', current_time=datetime.utcnow())