def post(self, request, pk, *args, **kwargs): """ 单个任务增加接口 { "interfaces": [1, 2, 3, 4] } :param request: :param pk: 任务id :param args: :param kwargs: :return: """ body = request.body params = json.loads(body) interfaces = params.get("interfaces", []) if not isinstance(interfaces, list): raise MyException() try: task = Task.objects.get(id=pk) except Task.DoesNotExist: raise MyException("任务不存在") for i in interfaces: try: Interface.objects.get(id=1) except Interface.DoesNotExist: raise MyException("接口不存在") TaskInterface.objects.create(task_id=task.id, interfaces_id=i)
def get(self, request, *args, **kwargs): # common.logger.info("我是日志") # raise MyException("test啊") token = request.META.get("HTTP_TOKEN", None) print(token) if token is None: raise MyException("用户未登录") else: try: session = Session.objects.get(pk=token) except Session.DoesNotExist: raise MyException("用户session失效") else: # django的session固定获取用户的id user_id = session.get_decoded().get('_auth_user_id', None) if user_id is None: raise MyException("用户id已失效") try: user = User.objects.get(pk=user_id) except User.DoesNotExist: raise MyException("用户不存在") else: return common.respone_success({ "username": user.username, "user_id": user.id })
def run_mock(request, pk): if not pk: raise MyException() mock = Mock.objects.get(id=pk) if request.method == mock.method: return JsonResponse(mock.response, safe=False) else: raise MyException("mock 不存在")
def put(self, request, *args, **kwargs): body = request.body params = json.loads(body) form = UserForm(params) result = form.is_valid() if result: user = authenticate(username=form.cleaned_data["username"], password=str(form.cleaned_data["password"])) if user: login(request, user) return common.response_success() else: raise MyException("登录失败") else: print(form.errors.as_json()) raise MyException()
def post(self, request, *args, **kwargs): body = request.body params = json.loads(body) form = UserForm(params) result = form.is_valid() if result: user = User.objects.create_user( username=form.cleaned_data['username'], password=form.cleaned_data['password']) if user: login(request, user) return common.response_success() else: raise MyException("注册失败") else: print(form.errors.as_json()) raise MyException()
def get(self, request, *args, **kwargs): user = request.user if user.is_authenticated: return common.response_success({ "username": user.username, "id": user.id }) else: raise MyException("用户未登录")
def post(self, request, *args, **kwargs): """单个服务""" body = request.body params = json.loads(body) form = UserForm(params) result = form.is_valid() if result: user = User.objects.create_user( username=form.cleaned_data["username"], password=form.changed_data["password"]) if user: login(request, user) session = request.session.session_key return common.response_success({"session": session}) else: raise MyException("注册失败") else: print(form.errors.as_json()) raise MyException()
def put(self, request, *args, **kwargs): body = request.body params = json.loads(body) form = UserForm(params) result = form.is_valid() if result: user = authenticate(username=params["username"], password=str(params["password"])) if user: login(request, user) session = request.session.session_key return common.respone_success({ "msg": "登录成功", "session": session }) else: raise MyException("登录失败") else: print(form.errors.as_json()) raise MyException(form.errors.as_json())
def delete(self, request, pk, *args, **kwargs): """ 停止任务 :param request: :param pk: :param args: :param kwargs: :return: """ try: task = Task.objects.get(id=pk) task.status = TASK_STATUS[2][0] task.save() except Task.DoesNotExist: raise MyException("任务不存在") return common.response_success()
def put(self, request, pk, *args, **kwargs): """ 更新单个服务 """ body = request.body params = json.loads(body) form = ServiceForm(params) # 参数校验 result = form.is_valid() if result: Service.objects.filter(id=pk).update( name=form.cleaned_data['name'], description=form.cleaned_data['description'], parent=form.cleaned_data['parent']) else: print(form.errors.as_json()) raise MyException() return common.response_success()
def post(self, request, *args, **kwargs): """ 创建服务 """ body = request.body params = json.loads(body) form = ServiceForm(params) result = form.is_valid() if result: service = Service.objects.create( name=form.cleaned_data['name'], description=form.cleaned_data['description'], parent=form.cleaned_data['parent'] ) if service: return common.response_success() else: raise MyException("创建失败") else: print(form.errors.as_json()) return common.response_failed()
def put(self, request, pk, *args, **kwargs): """ 更新单个 mock :param request: :param pk: :param args: :param kwargs: :return: """ body = request.body params = json.loads(body) form = MockForm(params) # 参数校验 result = form.is_valid() if request: Mock.objects.filter(id=pk).update(**form.cleaned_data) else: print(form.errors.as_json()) raise MyException() return common.response_success()
def delete(self, request, pk, *args, **kwargs): """ 任务删除接口 { task_interface_id: 1, } :param request: :param pk: task的 id :param args: :param kwargs: :return: """ body = request.body params = json.loads(body) task_interface_id = params.get("task_interface_id", None) if task_interface_id is None: raise MyException() TaskInterface.objects.filter(id=task_interface_id).delete() return common.response_success()
def post(self, request, *args, **kwargs): """ 创建 mock :param request: :param args: :param kwargs: :return: """ body = request.body params = json.loads(body) form = MockForm(params) result = form.is_valid() if result: mock = Mock.objects.create(**form.cleaned_data) if mock: return common.response_success() else: raise MyException("创建失败") else: print(form.errors.as_json()) return common.response_failed()
def post(self, request, pk, *args, **kwargs): """ 执行任务 :param request: :param pk: :param args: :param kwargs: :return: """ try: task = Task.objects.get(id=pk) except Task.DoesNotExist: raise MyException("任务不存在") body = request.body params = json.loads(body) def fun(obj, arg): runner = RunTaskUtils(obj, arg) runner.run() t = threading.Thread(target=fun, args=(task, params)) t.start() return common.response_success()