def password(): password_form = SettingResetPasswordForm() if request.method == 'POST': if password_form.validate_on_submit(): usercheck_d = UserCheck() post_password_encrypt = usercheck_d.password_encrypt( email=session['user']['email'], password=password_form.beforepassword.data) this_user = User.objects( email=session['user']['email']).first() if this_user.password == post_password_encrypt: new_password_encrypt = usercheck_d.password_encrypt( email=session['user']['email'], password=password_form.newpassword.data) this_user.password = new_password_encrypt this_user.save() flash(u"密码修改成功") return redirect( url_for('sign_module.setting_function', setcate="password")) else: flash(u"原密码错误,请重试") return redirect( url_for('sign_module.setting_function', setcate="password")) else: flash(u"数据提交失败,请检查输入内容") return redirect( url_for('sign_module.setting_function', setcate="password")) return render_template('users/setting_password.html', password_form=password_form)
def resetpassword_function(): forgetstring = request.args.get('forgetstring', '') resetform = ResetPswForm(forgetstring=forgetstring) reset_check = UserCheck() if request.method == 'POST': if resetform.validate_on_submit(): user_count = User.objects( email=resetform.email.data.lower()).count() if user_count == 1: this_user = User.objects( email=resetform.email.data.lower(), ).first() if 'forget' in this_user and this_user.forget[ 'string'] == resetform.forgetstring.data and (int( (time.time())) - int(this_user.forget['time']) < 3600): this_user.password = reset_check.password_encrypt( email=resetform.email.data.lower(), password=resetform.password.data) this_user.forget = None this_user.save() flash(u"密码已经修改成功,去登陆吧") return redirect(url_for('sign_module.login_function')) else: flash(u"数据匹配失败,请核对你的信息") return redirect( url_for('sign_module.resetpassword_function')) else: flash(u"邮件验证失败") return redirect(url_for('sign_module.resetpassword_function')) else: flash(u"信息核对失败,密码修改失败,请重新输入") return redirect(url_for('sign_module.resetpassword_function')) return render_template('users/resetpassword.html', resetform=resetform)
def resetpassword_function(): forgetstring = request.args.get('forgetstring', '') resetform = ResetPswForm(forgetstring=forgetstring) reset_check = UserCheck() if request.method == 'POST': if resetform.validate_on_submit(): user_count = User.objects(email=resetform.email.data.lower()).count() if user_count == 1: this_user = User.objects( email=resetform.email.data.lower(), ).first() if 'forget' in this_user and this_user.forget['string'] == resetform.forgetstring.data and (int((time.time())) - int(this_user.forget['time']) < 3600): this_user.password = reset_check.password_encrypt( email=resetform.email.data.lower(), password=resetform.password.data) this_user.forget = None this_user.save() flash(u"密码已经修改成功,去登陆吧") return redirect(url_for('sign_module.login_function')) else: flash(u"数据匹配失败,请核对你的信息") return redirect(url_for('sign_module.resetpassword_function')) else: flash(u"邮件验证失败") return redirect(url_for('sign_module.resetpassword_function')) else: flash(u"信息核对失败,密码修改失败,请重新输入") return redirect(url_for('sign_module.resetpassword_function')) return render_template('users/resetpassword.html', resetform=resetform)
def password(): password_form = SettingResetPasswordForm() if request.method == 'POST': if password_form.validate_on_submit(): usercheck_d = UserCheck() post_password_encrypt = usercheck_d.password_encrypt( email=session['user']['email'], password=password_form.beforepassword.data) this_user = User.objects(email=session['user']['email']).first() if this_user.password == post_password_encrypt: new_password_encrypt = usercheck_d.password_encrypt( email=session['user']['email'], password=password_form.newpassword.data) this_user.password = new_password_encrypt this_user.save() flash(u"密码修改成功") return redirect(url_for('sign_module.setting_function', setcate="password")) else: flash(u"原密码错误,请重试") return redirect(url_for('sign_module.setting_function', setcate="password")) else: flash(u"数据提交失败,请检查输入内容") return redirect(url_for('sign_module.setting_function', setcate="password")) return render_template('users/setting_password.html', password_form=password_form)
def login_function(): login = LoginForm() login_check = UserCheck() if request.method == 'POST': # POST if login.validate_on_submit(): # Count the User of input information user_count = User.objects( email=login.email.data.lower(), password=login_check.password_encrypt( email=login.email.data.lower(), password=login.password.data) ).count() if user_count == 1: # Login successful # Add Session this_user = User.objects( email=login.email.data, ).first() session.permanent = True session['user'] = { "username": this_user.username, "email": this_user.email, "email_md5": common.md5_encrypt(login.email.data), "status": this_user.status, "role": this_user.role, "description": this_user.description } next_page = request.args.get('next', '') if next_page == '': # Redirect to /me flash(u"欢迎回来,亲。") return redirect(url_for('sign_module.me_function')) else: # Redirect to next page return redirect(next_page) else: flash(u"用户名不存在或密码错误") return redirect(url_for('sign_module.login_function')) else: flash(u"数据提交失败,请检查输入内容") return redirect(url_for('sign_module.login_function')) return render_template('users/login.html', login=login)
def register_function(): register = RegisterForm() register_check = UserCheck() if request.method == 'POST': if register.validate_on_submit(): # Count the User of input information user_count = User.objects( email=register.email.data.lower()).count() if user_count == 0: print register.username.data.lower() if User.objects( _username=register.username.data.lower()).count() == 0: # 注销入库 User(email=register.email.data.lower(), username=register.username.data, password=register_check.password_encrypt( email=register.email.data.lower(), password=register.password.data), _username=register.username.data.lower()).save() # Register Email try: mail_send(subject='恭喜你,小熊笔记账号注册成功!', recipients=[register.email.data.lower()], html_body=render_template( 'mail/user_register.html', user={ "name": register.username.data, "email": register.email.data.lower() })) except: flash(u"邮件发送失败") flash(u"注册成功,请登录吧,亲") return redirect(url_for('sign_module.login_function')) else: flash(u"用户名已经被使用,请更换") return redirect(url_for('sign_module.register_function')) else: flash(u"邮箱已经被使用,请尝试找回密码") return redirect(url_for('sign_module.register_function')) else: flash(u"填写的内容不完善,请重试") return redirect(url_for('sign_module.register_function')) return render_template("users/register.html", register=register)
def login_function(): login = LoginForm() login_check = UserCheck() if request.method == 'POST': # POST if login.validate_on_submit(): # Count the User of input information user_count = User.objects( email=login.email.data.lower(), password=login_check.password_encrypt( email=login.email.data.lower(), password=login.password.data)).count() if user_count == 1: # Login successful # Add Session this_user = User.objects(email=login.email.data, ).first() session.permanent = True session['user'] = { "username": this_user.username, "email": this_user.email, "email_md5": common.md5_encrypt(login.email.data), "status": this_user.status, "role": this_user.role, "description": this_user.description } next_page = request.args.get('next', '') if next_page == '': # Redirect to /me flash(u"欢迎回来,亲。") return redirect(url_for('sign_module.me_function')) else: # Redirect to next page return redirect(next_page) else: flash(u"用户名不存在或密码错误") return redirect(url_for('sign_module.login_function')) else: flash(u"数据提交失败,请检查输入内容") return redirect(url_for('sign_module.login_function')) return render_template('users/login.html', login=login)
def register_function(): register = RegisterForm() register_check = UserCheck() if request.method == 'POST': if register.validate_on_submit(): # Count the User of input information user_count = User.objects(email=register.email.data.lower()).count() if user_count == 0: print register.username.data.lower() if User.objects(_username=register.username.data.lower()).count() == 0: # 注销入库 User( email=register.email.data.lower(), username=register.username.data, password=register_check.password_encrypt( email=register.email.data.lower(), password=register.password.data), _username=register.username.data.lower() ).save() # Register Email try: mail_send(subject='恭喜你,小熊笔记账号注册成功!', recipients=[register.email.data.lower()], html_body=render_template('mail/user_register.html', user={"name": register.username.data, "email": register.email.data.lower()})) except: flash(u"邮件发送失败") flash(u"注册成功,请登录吧,亲") return redirect(url_for('sign_module.login_function')) else: flash(u"用户名已经被使用,请更换") return redirect(url_for('sign_module.register_function')) else: flash(u"邮箱已经被使用,请尝试找回密码") return redirect(url_for('sign_module.register_function')) else: flash(u"填写的内容不完善,请重试") return redirect(url_for('sign_module.register_function')) return render_template("users/register.html", register=register)