def front_reset_pwd(request): if request.method == 'GET': return render(request, 'front_reset_pwd.html') else: username = request.user form = ResetpwdForm(request.POST, user=username) if form.is_vaild(): oldpwd = form.cleaned_data.get('oldpwd') newpwd = form.cleaned_data.get('newpwd') user = FrontUserModel.objects.filter(username=username, password=oldpwd).first() if user: is_vaild = user.check_password() if is_vaild: user.set_password(newpwd) user.save() return myjson.json_result() else: return myjson.json_params_error(message=u'密码验证错误!') else: user = user.create(username=username, password=newpwd) return myjson.json_result() else: return render(request, 'front_reset_pwd.html', {'error': form.get_error()})
def cms_reset_pwd(request): if request.method == 'GET': return render(request, 'cms_reset_pwd.html') else: username = request.user ## 使用cleaned_password方法 # form = ResetpwdForm(request.POST,username=username) # return render(request,'cms_reset_pwd.html',{'error':form.errors}) # 使用save_password方法 form = ResetpwdForm(request.POST, user=username) if form.is_vaild(): oldpwd = form.cleaned_data.get('oldpwd') user = authenticate(username=username, password=oldpwd) if user: is_vaild = form.save_password() if is_vaild: return myjson.json_result() else: return myjson.json_params_error(message=u'密码验证错误!') else: user = user.create(username=username, password=newpwd) return myjson.json_result() else: return render(request, 'cms_reset_pwd.html', {'error': form.get_error()})
def reset_password(request, code=''): if request.method == 'GET': return render(request, 'front_resetpwd.html') else: form = ResetpwdForm(request.POST) if form.is_valid(): # 需要从缓存中通过code来获取email email = cache.get(code) password = form.cleaned_data.get('password') user = FrontUserModel.objects.filter(email=email).first() if user: user.set_password(password) return HttpResponse(u'密码修改成功') else: return HttpResponse(u'没有该用户') else: return xtjson.json_params_error(message=form.get_error())
def post(self): form = ResetpwdForm(request.form) if form.validate(): oldpwd = form.oldpwd.data newpwd = form.newpwd.data user = g.user if (user.password == oldpwd): user.password = newpwd db.session.commit() return restful.success() else: return restful.params_error("旧密码错误!") else: return restful.params_error(form.get_error())
def reset_password(request, code=''): if request.method == 'GET': return render(request, 'front_resetpwd.html') else: form = ResetpwdForm(request.POST) if form.is_valid(): print code email = cache.get(code) print email password = form.cleaned_data.get('password') user = FrontUserModel.objects.filter(email=email).first() if user: user.set_password(password) return HttpResponse(u'密码修改成功') else: return HttpResponse(u'没有该用户') else: return render(request, 'front_resetpwd.html', {'error': form.get_error()})