2016-01-08 15:12:14 +00:00
|
|
|
from flask import (render_template, url_for, redirect, flash)
|
2016-01-05 17:52:09 +00:00
|
|
|
|
|
|
|
|
from app.main import main
|
2016-01-07 17:13:49 +00:00
|
|
|
from app.main.dao import users_dao
|
2016-01-06 17:37:07 +00:00
|
|
|
from app.main.forms import NewPasswordForm
|
2016-01-11 15:17:00 +00:00
|
|
|
from app.notify_client.sender import check_token, send_sms_code
|
2016-01-06 17:37:07 +00:00
|
|
|
|
2016-01-05 17:52:09 +00:00
|
|
|
|
2016-01-06 17:37:07 +00:00
|
|
|
@main.route('/new-password/<path:token>', methods=['GET', 'POST'])
|
|
|
|
|
def new_password(token):
|
2016-01-08 15:12:14 +00:00
|
|
|
email_address = check_token(token)
|
|
|
|
|
if not email_address:
|
2016-01-08 16:47:34 +00:00
|
|
|
flash('The link in the email we sent you has expired. Enter your email address to resend.')
|
2016-01-08 15:12:14 +00:00
|
|
|
return redirect(url_for('.forgot_password'))
|
|
|
|
|
|
2016-01-11 15:17:00 +00:00
|
|
|
user = users_dao.get_user_by_email(email_address=email_address)
|
2016-01-11 12:06:52 +00:00
|
|
|
if user and user.state != 'request_password_reset':
|
|
|
|
|
flash('The link in the email we sent you has already been used.')
|
|
|
|
|
return redirect(url_for('.index'))
|
2016-01-08 15:12:14 +00:00
|
|
|
|
2016-01-06 17:37:07 +00:00
|
|
|
form = NewPasswordForm()
|
2016-01-08 15:12:14 +00:00
|
|
|
|
2016-01-06 17:37:07 +00:00
|
|
|
if form.validate_on_submit():
|
2016-01-08 15:12:14 +00:00
|
|
|
users_dao.update_password(user, form.new_password.data)
|
|
|
|
|
send_sms_code(user.id, user.mobile_number)
|
|
|
|
|
return redirect(url_for('main.two_factor'))
|
2016-01-06 17:37:07 +00:00
|
|
|
else:
|
2016-01-08 15:12:14 +00:00
|
|
|
return render_template('views/new-password.html', token=token, form=form, user=user)
|