def forgetpassword_function(): forgetpsw = ForgetPswForm() forget_check = UserCheck() if request.method == 'POST': if forgetpsw.validate_on_submit(): user_count = User.objects( email=forgetpsw.email.data.lower()).count() if user_count == 1: this_user = User.objects( email=forgetpsw.email.data.lower(), ).first() now_time = int(time.time()) if 'forget' not in this_user: forgetstring = forget_check.forgetstring_encrypt( email=forgetpsw.email.data.lower()) this_user.forget = { 'string': forgetstring, 'time': int(time.time()) } this_user.save() try: mail_send(subject='你好,小熊笔记发来重置密码的密钥串', recipients=[this_user.email], html_body=render_template( 'mail/user_forgetpassword.html', user={ "name": this_user.username, "fstring": forgetstring })) except: flash(u"邮件发送失败") flash(u"已发送密码重置邮件,请前往邮箱查收。邮件一小时内有效") return redirect( url_for('sign_module.forgetpassword_function')) else: if now_time - int(this_user.forget['time']) > 3600: # Overtime forgetstring = forget_check.forgetstring_encrypt( email=forgetpsw.email.data.lower()) this_user.forget = { 'string': forgetstring, 'time': int(time.time()) } this_user.save() # send mail try: mail_send(subject='你好,小熊笔记发来重置密码的密钥串', recipients=[this_user.email], html_body=render_template( 'mail/user_forgetpassword.html', user={ "name": this_user.username, "fstring": forgetstring })) except: flash(u"邮件发送失败") flash(u"原密码重置邮件已失效,已重新生成并发送密码重置邮件,请前往邮箱查收") return redirect( url_for('sign_module.forgetpassword_function')) elif (now_time - int(this_user.forget['time']) < 3600) and (now_time - int(this_user.forget['time']) > 60): # send mail this_user.forget['time'] = now_time this_user.save() try: mail_send(subject='你好,小熊笔记发来重置密码的密钥串', recipients=[this_user.email], html_body=render_template( 'mail/user_forgetpassword.html', user={ "name": this_user.username, "fstring": this_user.forget.string })) except: flash(u"邮件发送失败") flash(u"密码重置邮件已重新发送") return redirect( url_for('sign_module.forgetpassword_function')) elif now_time - int(this_user.forget['time']) < 60: flash(u"密码重置邮件已发送,请勿频繁操作(邮件发送间隔为 1分钟)") return redirect( url_for('sign_module.forgetpassword_function')) else: flash(u"邮箱尚未注册,或者邮箱异常") return redirect(url_for('sign_module.forgetpassword_function')) else: flash(u"请填写正确的邮箱") return redirect(url_for('sign_module.forgetpassword_function')) return render_template('users/forgetpassword.html', forgetpsw=forgetpsw)
def forgetpassword_function(): forgetpsw = ForgetPswForm() forget_check = UserCheck() if request.method == 'POST': if forgetpsw.validate_on_submit(): user_count = User.objects(email=forgetpsw.email.data.lower()).count() if user_count == 1: this_user = User.objects( email=forgetpsw.email.data.lower(), ).first() now_time = int(time.time()) if 'forget' not in this_user: forgetstring = forget_check.forgetstring_encrypt(email=forgetpsw.email.data.lower()) this_user.forget = { 'string': forgetstring, 'time': int(time.time()) } this_user.save() try: mail_send(subject='你好,小熊笔记发来重置密码的密钥串', recipients=[this_user.email], html_body=render_template('mail/user_forgetpassword.html', user={"name": this_user.username, "fstring": forgetstring})) except: flash(u"邮件发送失败") flash(u"已发送密码重置邮件,请前往邮箱查收。邮件一小时内有效") return redirect(url_for('sign_module.forgetpassword_function')) else: if now_time - int(this_user.forget['time']) > 3600: # Overtime forgetstring = forget_check.forgetstring_encrypt(email=forgetpsw.email.data.lower()) this_user.forget = { 'string': forgetstring, 'time': int(time.time()) } this_user.save() # send mail try: mail_send(subject='你好,小熊笔记发来重置密码的密钥串', recipients=[this_user.email], html_body=render_template('mail/user_forgetpassword.html', user={"name": this_user.username, "fstring": forgetstring})) except: flash(u"邮件发送失败") flash(u"原密码重置邮件已失效,已重新生成并发送密码重置邮件,请前往邮箱查收") return redirect(url_for('sign_module.forgetpassword_function')) elif (now_time - int(this_user.forget['time']) < 3600) and (now_time - int(this_user.forget['time']) > 60): # send mail this_user.forget['time'] = now_time this_user.save() try: mail_send(subject='你好,小熊笔记发来重置密码的密钥串', recipients=[this_user.email], html_body=render_template('mail/user_forgetpassword.html', user={"name": this_user.username, "fstring": this_user.forget.string})) except: flash(u"邮件发送失败") flash(u"密码重置邮件已重新发送") return redirect(url_for('sign_module.forgetpassword_function')) elif now_time - int(this_user.forget['time']) < 60: flash(u"密码重置邮件已发送,请勿频繁操作(邮件发送间隔为 1分钟)") return redirect(url_for('sign_module.forgetpassword_function')) else: flash(u"邮箱尚未注册,或者邮箱异常") return redirect(url_for('sign_module.forgetpassword_function')) else: flash(u"请填写正确的邮箱") return redirect(url_for('sign_module.forgetpassword_function')) return render_template('users/forgetpassword.html', forgetpsw=forgetpsw)