def post(self, request, *args, **kwargs): """ 注册 :param response: :param args: :param kwargs: :return: """ body = request.body #返回的是字符串 data = json.loads(body) #把字符串解析为字典 form = UserForm(data) result = form.is_valid() # 校验数据是否合法 if not result: default_log.error(form.errors.as_json()) raise MyException() if User.objects.filter( username=form.cleaned_data['username']).exists(): raise MyException(message='用户已存在') user = User.objects.create_user(username=form.cleaned_data['username'], password=form.cleaned_data['password']) if user: login(request, user) return response_success("注册成功") else: raise MyException(message="注册失败")
def put(self, request, service_id, *args, **kwargs): """ 代表的是更新服务 :param request: :param service_id: 服务的id :param args: :param kwargs: :return: """ service = Service.objects.filter(id=service_id).first() if service is None: return response_success() body = request.body if not body: return response_success() data = json.loads(body) form = ServiceForm(data) if form.is_valid(): # Service.objects.filter(id=service_id).update(name=form.cleaned_data["name"], # description=form.cleaned_data["description"], # parent=form.cleaned_data["parent"]) Service.objects.filter(id=service_id).update(**form.cleaned_data) return response_success() else: raise MyException()
def test_success(request): a = 2 b = 2 c = 2 if a != 1: print("hahahahha") raise MyException( message="aaaaa" ) # 抛出异常,会去到中间件my_middle_ware.py的process_exception里面,来判断是什么异常 # default_log.info("info-1") return response_success({"a": 1})
def get(self, request, *args, **kwargs): """ 登录 :param response: :param args: :param kwargs: :return: """ form = UserForm(request.GET) result = form.is_valid() # 校验数据是否合法 if not result: default_log.error(form.errors.as_json()) raise MyException() user = authenticate(username=form.cleaned_data['username'], password=form.cleaned_data['password']) if user: login(request, user) # 登录持久化 , 生成session return response_success('登录成功') else: raise MyException(message="登录失败")
def get(self, request, *args, **kwargs): """ 代表登录 :param request: :param args: :param kwargs: :return: """ form = UserForm(request.GET) result = form.is_valid() if not result: default_log.error(form.errors.as_json()) raise MyException() user = authenticate(username=form.cleaned_data["username"], password=form.cleaned_data["password"]) print("UsersView-get", user) if user: login(request, user) # 登录持久化, 生成session return response_success("登录成功") else: raise MyException(message="登录失败")
def get(self, request, *args, **kwargs): """ 获取用户的登录信息 :param response: :param args: :param kwargs: :return: """ user = request.user if user.is_authenticated: # 判断用户是否认证过 ret = {"username": user.username, "id": user.id} return response_success(ret) else: raise MyException(message="用户没有登录")
def post(self, request, *args, **kwargs): """ 代表的是创建接口 :param request: :param args: :param kwargs: :return: """ body = request.body if not body: return response_success() data = json.loads(body) form = InterfaceForm(data) if form.is_valid(): Interface.objects.create(**form.cleaned_data) return response_success() else: print(form.errors.as_json()) raise MyException()
def post(self, request, *args, **kwargs): """ 代表的是创建服务 :param request: :param args: :param kwargs: :return: """ body = request.body if not body: return response_success() data = json.loads(body) form = ServiceForm(data) if form.is_valid(): # Service.objects.create(name=form.cleaned_data["name"], # description=form.cleaned_data["description"], # parent=form.cleaned_data["parent"]) Service.objects.create(**form.cleaned_data) return response_success() else: raise MyException()
def put(self, request, mock_id, *args, **kwargs): """ 代表的是更新mock :param request: :param mock_id: mock的id :param args: :param kwargs: :return: """ mock = Mock.objects.filter(id=mock_id).first() if mock is None: return response_success() body = request.body if not body: return response_success() data = json.loads(body) form = MockForm(data) if form.is_valid(): Mock.objects.filter(id=mock_id).update(**form.cleaned_data) return response_success() else: raise MyException()
def put(self, request, interface_id, *args, **kwargs): """ 代表的是更新接口 :param request: :param interface_id: 接口的id :param args: :param kwargs: :return: """ interface = Interface.objects.filter(id=interface_id).first() if interface is None: return response_success() body = request.body if not body: return response_success() data = json.loads(body) form = InterfaceForm(data) if form.is_valid(): Interface.objects.filter(id=interface_id).update( **form.cleaned_data) return response_success() else: raise MyException()