def post(self, request): email = request.POST.get('email') password = request.POST.get('password') re_password = request.POST.get('re_password') vaild_code = request.POST.get('vaild_code') cache_code = cache.get(email) if Db_Redis_Conn.llen(email + 'bk') >= 5: # from django.shortcuts import render_to_response # response = render_to_response('403.html', {}) # response.status_code = 403 # return response return restful.forbidden_error(message='访问频繁') if password != re_password: return restful.params_error('两次密码不一致') if vaild_code != cache_code: # 使用redis限制用户频繁输入错的验证码 bad_key = email + 'bk' Db_Redis_Conn.lpush(bad_key, 1) return restful.params_error('验证码错误') user = User.objects.get(email=email) if user: user.set_password(password) user.save() return restful.ok() else: return restful.params_error(message="用户不存在")
def post(self, request): email = request.POST.get('email') captcha_code = request.POST.get('imgcode') if email: cache_code = cache.get(captcha_code) if captcha_code.lower() == cache_code: send_reset_email(email=email) # 传入email用于回传 return restful.ok(message=email) else: return restful.params_error(message="验证码错误") else: return restful.params_error(message="请填写邮箱")
def post(self, request): form = UserManageForm(request.POST) if form.is_valid(): password = form.cleaned_data.get("password") user = form.save(commit=False) user.image = request.POST.get("image") user.set_password(password) user.save() return restful.ok() else: return restful.params_error(message=form.get_first_error())
def post(self, request): form = LoginForm(request.POST) if form.is_valid(): username = form.cleaned_data.get('username') password = form.cleaned_data.get('password') remember = form.cleaned_data.get('remember') user = authenticate(username=username, password=password) if user: if user.is_active: if remember: request.session.set_expiry(None) else: request.session.set_expiry(0) login(request, user) return restful.ok() else: return restful.unauth(message="用户已被停用") else: return restful.params_error(message="用户不存在或密码错误") else: print(form.get_errors()) return restful.params_error(message=form.get_first_error())
def put(self, request): qd = QueryDict(request.body) put_dict = {k: v[0] if len(v) == 1 else v for k, v in qd.lists()} form = UserManageForm(put_dict) if form.is_valid(): password = put_dict.get('password', '') # user_id = put_dict.get('userid','') # user_name = put_dict.get('username','') # telephone = put_dict.get('telephone','') # email = put_dict.get('email','') # user = User.objects.filter(pk=user_id).update(username=user_name,telephone=telephone,email=email) user = form.save(commit=False) user.set_password(password) user.save() return restful.ok() else: return restful.params_error(message=form.get_first_error())