async def update(t_id: int, task: models.TaskInCase): try: task_obj = await models.Task.get(id=t_id) cases_obj = [await models.Case.get(id=case) for case in task.case_list] del task.case_list job_time = "待定" async with in_transaction(): await models.Task.filter(id=t_id).update(**task.dict(exclude_unset=True)) if task.is_timer: try: job = scheduler.modify_job( str(t_id), func=run_case, args=( t_id,), cron=task.cron) except Exception as e: print(e) job = scheduler.new_job( str(t_id), func=run_case, args=( t_id,), cron=task.cron) job_time = job.next_run_time else: try: scheduler.delete_job(str(task_obj.id)) except Exception as e: print(e) # 清除该对象与case的关系 await task_obj.cases.clear() # 添加关系 await task_obj.cases.add(*cases_obj) return core.Success(message=f"修改成功,下次运行时间{job_time}", data=await models.Task_Pydantic.from_queryset_single(models.Task.get(id=t_id))) except Exception as e: return core.Fail(message=f"修改失败.{e}")
async def select(e_id: int): try: data = await models.Env_Pydantic.from_queryset_single( models.Env.get(id=e_id)) return core.Success(data=data) except Exception as e: return core.Fail(message=f"查看详情失败.{e}")
async def create(env: models.EnvIn_Pydantic): try: env_obj = await models.Env.create(**env.dict(exclude_unset=True)) return core.Success( data=await models.Env_Pydantic.from_tortoise_orm(env_obj)) except Exception as e: return core.Fail(message=f"创建失败.{e}")
async def create(interface: models.InterfaceIn_Pydantic): try: interface_obj = await models.Interface.create(**interface.dict( exclude_unset=True)) return core.Success(data=await models.Interface_Pydantic. from_tortoise_orm(interface_obj)) except Exception as e: return core.Fail(message="创建失败.")
async def update_code(script: core.Code): # 验证是否可以被执行 try: exec(script.code) await write_file('util/extend.py', script.code) return core.Success() except Exception as e: return core.Fail(message=f"更新失败.{e}")
async def update(e_id: int, env: models.EnvIn_Pydantic): try: await models.Env.filter(id=e_id).update(**env.dict(exclude_unset=True)) return core.Success( data=await models.Env_Pydantic.from_queryset_single( models.Env.get(id=e_id))) except Exception as e: return core.Fail(message=f"更新失败.{e}")
async def login(user: core.Login): try: user_obj = await models.User.get(username=user.username) if user_obj and core.verify_password(user.password, user_obj.password): return core.Success(data=core.Token( token=core.create_access_token({"sub": user_obj.username}))) except Exception as e: print(e) return core.Fail(message="用户名或密码错误")
async def delete(t_id: int): task_obj = await models.Task.filter(id=t_id).delete() print(task_obj == True) if task_obj: try: scheduler.delete_job(str(t_id)) except Exception as e: print(e) return core.Success() return core.Fail(message="任务不存在.")
async def create(project: models.ProjectIn_Pydantic): """ :param project: name 字段唯一 :return: """ try: project_obj = await models.Project.create(**project.dict(exclude_unset=True)) # from_tortoise_orm 从 数据表中序列化, 针对 模型类对象 return core.Success(data=await models.Project_Pydantic.from_tortoise_orm(project_obj)) except Exception as e: return core.Fail(message="项目已存在.")
async def create(task: models.TaskInCase): """ :param task: name 字段唯一 :return: """ # try: # # 获取case对象,如果没有将抛出异常 # cases_obj = [await models.Case.get(id=case) for case in task.case_list] # del task.case_list # # # 定时任务, 事务 async with in_transaction(): # # if task.is_timer: # trigger = scheduler.verif_cron(task.cron) # if trigger: # task_obj = await models.Task.create(**task.dict(exclude_unset=True)) # # 添加到多对多关系中 # await task_obj.cases.add(*cases_obj) # # job = scheduler.new_job(str(task_obj.id), func=run_case, args=( # task_obj.id,), trigger=trigger) # return core.Success(message=f"创建成功,下次运行的时间{job.next_run_time}") # else: # return core.Fail(message="corn表达式设置有误") # else: # task_obj = await models.Task.create(**task.dict(exclude_unset=True)) # # 添加到多对多关系中 # await task_obj.cases.add(*cases_obj) # return core.Success(data=await models.Task_Pydantic.from_tortoise_orm(task_obj)) # except Exception as e: # print(e) # return core.Fail(message="任务已存在.") try: # 事务来实现 cases_obj = [await models.Case.get(id=case) for case in task.case_list] del task.case_list job_time = "待定" async with in_transaction(): task_obj = await models.Task.create(**task.dict(exclude_unset=True)) if task.is_timer: job = scheduler.new_job(str(task_obj.id), func=run_case, args=( task_obj.id,), cron=task.cron) job_time = job.next_run_time await task_obj.cases.add(*cases_obj) return core.Success(message=f"创建成功,下次运行{job_time}") except Exception as e: return core.Fail(message=f"创建失败.{e}")
async def delete(r_id: int): interface_obj = await models.Interface.filter(id=r_id).delete() if interface_obj: return core.Success() return core.Fail(message="接口不存在.")
async def delete(e_id: int): env_obj = await models.Env.filter(id=e_id).delete() if env_obj: return core.Success() return core.Fail(message="环境不存在.")
async def delete(p_id: int): project_obj = await models.Project.filter(id=p_id).delete() if project_obj: return core.Success() return core.Fail(message="项目不存在.")
async def delete(r_id: int): report_obj = await models.Report.filter(id=r_id).delete() if report_obj: return core.Success() return core.Fail(message="报告不存在.")
async def delete(c_id: int): case_obj = await models.Case.filter(id=c_id).delete() if case_obj: return core.Success() return core.Fail(message="用例不存在.")