def modify_password(request): """ 修改密码,需要登入权限 :param request: :return: """ # 获取用户实例 user = get_user(request) password = request.POST.get('password') password_repeat = request.POST.get('password_repeat') # 先判断两次密码是否一致,不一致直接返回错误消息 or 密码不符合LoginForm验证条件 if password != password_repeat or not LoginForm({ 'user': user.user, 'password': password }).is_valid(): return HttpResponseRedirect( reverse('user:settings') + '?message=password_repeatInconsistent') code = request.POST.get('Vcode') # code与有效期都符合条件judgment返回v实例,否则返回None v = judgment_code(user_user=user.user, code=code, period_of_validity=5) if v: # 修改密码,registered()方法将密码加密并且保存数据.最后删除v实例,使code失效. user.password = password user.registered() v.delete() # 密码修改成功,退出登录. return HttpResponseRedirect(reverse('user:logout')) else: # code错误或code已失效,在前台给出错误提示. return HttpResponseRedirect( reverse('user:settings') + '?message=code-error')
def bind_email(request): """ 绑定邮箱 message:前台提示绑定结果信息. :param request: :return: """ user = get_user(request) # 当用户已绑定邮箱的情况下,返回邮箱已绑定信息. if user.email: return HttpResponseRedirect( reverse('user:settings') + '?message=bind-email-Already') else: email = request.POST.get('email') code = request.POST.get('Vcode') # 当Vcode创建时间大于5分钟前的时间则Vcode为有效,否则即为无效. v = judgment_code(user_user=user.user, code=code, period_of_validity=5) # 如果user与code匹配,并且Vcode为有效,则表明验证通过.进行邮箱绑定. if v: user.email = email user.save() message = '?message=bind-email-success' # 验证成功立即删除Vcode v.delete() else: message = '?message=code-error' return HttpResponseRedirect(reverse('user:settings') + message)
def reset_password(request, code): """ 重置密码界面与修改密码操作. :param request: :param code: 唯一验证码,有效期5分钟. :return: """ content = {'title': '重置密码', 'code': code} # GET请求跳转重置界面 if request.method == 'GET': return render(request, template_name='user/reset_password.html', context=content) else: password = request.POST.get('password') password_repeat = request.POST.get('password_repeat') f = LoginForm({'user': '******', 'password': password}) # 验证密码是否符合规范与两次密码是否一致 if f.is_valid() and password == password_repeat: v = judgment_code(user_user=None, code=code, period_of_validity=5) if v: # 通过v找到用户实例, 修改用户密码, 加密密码并且保存. user = User.objects.filter(user=v.user).first() user.password = password user.registered() # 删除code的实例v v.delete() return HttpResponseRedirect( reverse('home') + '?message=ResetPasswordSuccess') else: # 前台错误消息提示 content.update({'error': '重置失败,请检查链接有效期.'}) else: # 前台错误消息提示 content.update({'error': '重置失败,请检查密码是否符合规范.'}) return render(request, template_name='user/reset_password.html', context=content)