def put(self, request, project_id, *args, **kwargs): """ 这个是修改数据 :param request: :param project_id: :param args: :param kwargs: :return: """ project = Project.objects.filter(id=project_id).first() if not project: return response_failed(code=ErrorCode.project, message='数据不存在') body = request.body data = json.loads(body, encoding='utf-8') if not self.update_schema.is_valid(data): return response_failed() data = self.update_schema.validate(data) if not data: # 如果没有传数据,就不需要处理 pass else: Project.objects.filter(id=project_id).update(**data) # 为什么会有三种情况, 因为data的确存在三种情况 # {"name": 'x'} {"description": 'x'} {"description": 'x', "name": 'x'} # Project.objects.filter(id=project_id).update(name='x', description='x') # Project.objects.filter(id=project_id).update(name='x') # Project.objects.filter(id=project_id).update(description='x') project = Project.objects.filter(id=project_id).first() project_dict = model_to_dict(project) return response_success(data=project_dict)
def put(self, request, case_id, *args, **kwargs): """ 这个是修改数据 :param request: :param case_id: :param args: :param kwargs: :return: """ case = TestCase.objects.filter(id=case_id).first() if not case: return response_failed(code=ErrorCode.test_case, message='数据不存在') body = request.body data = json.loads(body, encoding='utf-8') if not self.update_schema.is_valid(data): return response_failed() data = self.update_schema.validate(data) if not data: # 如果没有传数据,就不需要处理 pass else: if data.get('request_body'): data['request_body'] = json.dumps(data['request_body']) if data.get('response_assert'): data['response_assert'] = json.dumps(data['response_assert']) TestCase.objects.filter(id=case_id).update(**data) case = TestCase.objects.filter(id=case_id).first() ret = test_case_model_to_dict(case) return response_success(data=ret)
def put(self, request, service_id, *args, **kwargs): """ 这个是修改数据 :param request: :param service_id: :param args: :param kwargs: :return: """ service = Service.objects.filter(id=service_id).first() if not service: return response_failed(code=ErrorCode.service, message='数据不存在') body = request.body data = json.loads(body, encoding='utf-8') if not self.update_schema.is_valid(data): return response_failed() data = self.update_schema.validate(data) if not data: # 如果没有传数据,就不需要处理 pass else: Service.objects.filter(id=service_id).update(**data) service = Service.objects.filter(id=service_id).first() service_dict = model_to_dict(service) return response_success(data=service_dict)
def get_user_info(request, *args, **kwargs): """ 获取已登录的用户信息 :param request: :param args: :param kwargs: :return: """ user = request.user # 获取当前用户 if not user: return response_failed(code=ErrorCode.auth, message='用户未登录') if user.is_authenticated: # 判断用户是否已经通过校验 return response_success(data={'id': user.id, 'name': user.username}) else: return response_failed(code=ErrorCode.auth, message='用户未登录')
def delete(self, request, task_id, *args, **kwargs): """ 停止任务循环执行 :param request: :param task_id: :param args: :param kwargs: :return: """ task = Task.objects.filter(id=task_id).first() if not task: return response_failed(code=ErrorCode.task, message='数据不存在') data = { "interval_switch": False, "days": 0, "hours": 0, "minutes": 0, "start_time": None } Task.objects.filter(id=task_id).update(**data) job = scheduler.get_job("task" + str(task_id)) if job: scheduler.remove_job("task" + str(task_id)) return response_success()
def post(self, request, task_id, *args, **kwargs): """ 任务循环执行 :param request: :param task_id: :param args: :param kwargs: :return: """ task = Task.objects.filter(id=task_id).first() if not task: return response_failed(code=ErrorCode.task, message='数据不存在') body = request.body data = json.loads(body, encoding='utf-8') if not self.update_schema.is_valid(data): return response_failed() data = self.update_schema.validate(data) if not data: # 如果没有传数据,就不需要处理 pass else: data['interval_switch'] = True Task.objects.filter(id=task_id).update(**data) job = scheduler.get_job("task" + str(task_id)) if job: scheduler.remove_job("task" + str(task_id)) scheduler.add_job(run_task_common, 'interval', args=[task_id], days=data["days"], hours=data["hours"], minutes=data["minutes"], start_date=data["start_time"], id="task" + str(task_id)) return response_success()
def get(self, request, case_id, *args, **kwargs): """ 请求是单个数据 :param request: :param case_id: :param args: :param kwargs: :return: """ case = TestCase.objects.filter(id=case_id).first() if not case: return response_failed(code=ErrorCode.test_case, message='数据不存在') ret = test_case_model_to_dict(case) return response_success(data=ret)
def get(self, request, service_id, *args, **kwargs): """ 请求是单个数据 :param request: :param service_id: :param args: :param kwargs: :return: """ service = Service.objects.filter(id=service_id).first() if not service: return response_failed(code=ErrorCode.service, message='数据不存在') service_dict = model_to_dict(service) return response_success(data=service_dict)
def get(self, request, task_id, *args, **kwargs): """ 请求是单个数据 :param request: :param task_id: :param args: :param kwargs: :return: """ task = Task.objects.filter(id=task_id).first() if not task: return response_failed(code=ErrorCode.task, message='数据不存在') task_dict = model_to_dict(task) return response_success(data=task_dict)
def user_login(request, *args, **kwargs): """ 登录 :param request: :param args: :param kwargs: :return: """ body = request.body data = json.loads(body, encoding='utf-8') if not user_schema.is_valid(data): return response_failed() if User.objects.filter(username=data["name"]).exists(): user = authenticate(username=data["name"], password=data["psw"]) else: user = User.objects.create_user(username=data["name"], password=data["psw"]) if not user: return response_failed(code=ErrorCode.auth, message='登录失败') else: login(request, user) # 登录持久化 return response_success()
def get(self, request, project_id, *args, **kwargs): """ 请求是单个数据 :param request: :param project_id: :param args: :param kwargs: :return: """ project = Project.objects.filter(id=project_id).first() if not project: return response_failed(code=ErrorCode.project, message='数据不存在') project_dict = model_to_dict(project) project_dict['create_time'] = datetime.datetime.strftime( project.create_time, '%Y-%m-%d %H:%M') return response_success(data=project_dict)
def post(self, request, *args, **kwargs): """ 创建数据 :param request: :param project_id: :param args: :param kwargs: :return: """ body = request.body data = json.loads(body, encoding='utf-8') if not self.create_schema.is_valid(data): return response_failed() data = self.create_schema.validate(data) service = Service.objects.create(**data) service_dict = model_to_dict(service) return response_success(data=service_dict)
def post(self, request, task_id, *args, **kwargs): """ 创建数据 :param request: :param task_id: :param args: :param kwargs: :return: """ create_schema = Schema({'test_case_ids': list}) body = request.body data = json.loads(body, encoding='utf-8') if not create_schema.is_valid(data): return response_failed() data = create_schema.validate(data) test_case_ids = data['test_case_ids'] for item in test_case_ids: TaskTestCase.objects.create(task_id=task_id, test_case_id=item) return response_success()
def post(self, request, *args, **kwargs): """ 创建数据 :param request: :param project_id: :param args: :param kwargs: :return: """ body = request.body data = json.loads(body, encoding='utf-8') if not self.debug_schema.is_valid(data): return response_failed() data = self.debug_schema.validate(data) if data.get('request_body'): data['request_body'] = json.dumps(data['request_body']) response = HttpRequest.send_request( data['url'], data['method'], data.get('request_body', {}), data['request_type']) # 4. 进行http请求,获取响应 return response_success(data=response)
def post(self, request, *args, **kwargs): """ 创建数据 :param request: :param project_id: :param args: :param kwargs: :return: """ body = request.body data = json.loads(body, encoding='utf-8') if not self.create_schema.is_valid(data): return response_failed() data = self.create_schema.validate(data) if data.get('request_body'): data['request_body'] = json.dumps(data['request_body']) if data.get('response_assert'): data['response_assert'] = json.dumps(data['response_assert']) case = TestCase.objects.create(**data) case = TestCase.objects.get(id=case.id) case_dict = test_case_model_to_dict(case) return response_success(data=case_dict)