def get(self, request, *args, **kwargs): #激活接口 action = request.query_params.get("action") if action == "activate": try: token = request.query_params.get("token") #获取token u_name = cache.get(token) #获取u_name if u_name: #判断u_name是否有数据 cache.delete(token) #有就删除 user = UserModel.get_user(u_name) user.is_active = True user.save() except Exception as e: print(e) # return redirect("/index/") return render(request, "index.html")
def do_login(self, request, *args, **kwargs): u_user = request.data.get("u_user") u_password = request.data.get("u_password") user = UserModel.get_user(u_user) if not user.check_password(u_password): raise AuthenticationFailed(detail="密码错误") if not user.is_active: raise PermissionDenied(detail="用户未激活") token = generate_token() cache.set(token, user, timeout=USER_TOKEN_TIMEOUT) data = {"msg": "登录成功", "status": HTTP_200_OK, "data": {"token": token}} return Response(data)
def do_login(self, request, *args, **kwargs): #登录-->就是验证用户名 密码邮箱等是否存在,并验证输入的是否正确, 验证账户的激活状态激活后才能登录 u_user = request.data.get("u_user") #用户名标示 u_password = request.data.get("u_password") user = UserModel.get_user(u_user) if not user.check_password(u_password): raise AuthenticationFailed( detail="密码错误") #AuthenticationFailed 认证失败 if not user.is_active: #判断用户是否 激活 raise PermissionDenied(detail="用户未激活") #权限拒绝 token = generate_token() cache.set(token, user, timeout=USER_TOKEN_TIMEOUT) #用户过期时间 data = {"msg": "登录成功", "status": HTTP_200_OK, "data": {"token": token}} return Response(data)
def get(self, request, *args, **kwargs): action = request.query_params.get("action") if action == "activate": try: token = request.query_params.get("token") u_name = cache.get(token) if u_name: cache.delete(token) user = UserModel.get_user(u_name) user.is_active = True user.save() # request.session["info"] = "激活成功" messages.warning(request._request, "激活成功") except Exception as e: print(e) # request.session["info"] = "激活失败" messages.warning(request._request, "激活失败") return redirect("/index/")