def get_tasks(cls, user, project, no, start, end, page=None, count=None): count = int(count) if count else current_app.config.get( 'COUNT_DEFAULT') page = int( page) if page else current_app.config.get('PAGE_DEFAULT') + 1 results = cls.query.filter( cls.create_user == user if user else '', cls.project_id == project if project else '', cls.task_no.like(f'%{no}%') if no is not None else '', cls._create_time.between(start, end) if start and end else '', cls.delete_time == None, ).with_entities(cls.id, cls.task_no, cls.project_id, cls.create_user, cls.total, cls.success, cls.fail, cls._create_time.label('create_time')).order_by( text('update_time desc')).paginate(page, count) items = [dict(zip(result.keys(), result)) for result in results.items] for item in items: # 获取工程名称 from app.models.project import Project project_name = Project.query.filter_by( id=item['project_id']).first() item['project_name'] = project_name.name # 获取执行人名称 user = manager.user_model.query.filter_by( id=item['create_user']).first() item['username'] = user.username item['create_time'] = int( round(item['create_time'].timestamp() * 1000)) results.items = items data = paging(results) return data
def search_case(cls, name, url, case_group, start, end, cid, method, deal, page=None, count=None): count = int(count) if count else current_app.config.get( 'COUNT_DEFAULT') page = int( page) if page else current_app.config.get('PAGE_DEFAULT') + 1 auths = UserAuth.query.filter_by(user_id=current_user.id, _type=UserAuthEnum.GROUP.value).all() gids = [auth.auth_id for auth in auths] from app.models.CaseGroup import CaseGroup results = cls.query.join(CaseGroup, CaseGroup.id == cls.case_group). \ join(manager.user_model, manager.user_model.id == cls.create_user).filter( cls.id == cid if cid else '', cls._method == method if method else '', cls._deal == deal if deal else '', cls.case_group == case_group if case_group else '', cls.name.like(f'%{name}%') if name is not None else '', cls.url.like(f'%{url}%') if url is not None else '', cls._update_time.between(start, end) if start and end else '', cls.case_group.in_(gids) if current_user.id != 1 else '', cls.delete_time == None, ).with_entities( cls.id, cls.name, cls.info, cls.url, cls._method.label('method'), cls._submit.label('submit'), cls.header, cls.data, cls._deal.label('deal'), cls.condition, cls._type.label('type'), cls.expect.label('expect'), cls._assertion.label('assertion'), cls.case_group.label('case_group'), CaseGroup.name.label('group_name'), manager.user_model.username.label('create_user'), ).order_by( text('Case.update_time desc') ).paginate(page, count) items = [dict(zip(result.keys(), result)) for result in results.items] results.items = items data = paging(results) return data
def user_scheduler(cls, uid, name, page=None, count=None): count = int(count) if count else current_app.config.get( 'COUNT_DEFAULT') page = int( page) if page else current_app.config.get('PAGE_DEFAULT') + 1 if not uid: uid = current_user.id results = cls.query.join(Project, Project.id == cls.project_id).filter( cls.user == uid, Project.name.like(f'%{name}%') if name is not None else '', cls.delete_time == None, ).with_entities( cls.id, cls.scheduler_id, cls.project_id, cls.user, cls.copy_person, cls.send_email, cls.cron, ).order_by(text('Scheduler.update_time desc')).paginate(page, count) jobs = [dict(zip(result.keys(), result)) for result in results.items] # 为job添加next_run_time 和 state 属性 job_id_list, next_run_time_dict = cls.get_job_detail() for job in jobs: if job['scheduler_id'] in job_id_list: job['state'] = True job['next_run_time'] = next_run_time_dict[job['scheduler_id']] # 如果任务被暂停,置状态为未运行 if job['next_run_time'] is None: job['state'] = False else: job['state'] = False job['next_run_time'] = None # 工程名称 job['project_name'] = Project.query.filter_by( id=job['project_id']).first().name # 维护人名称 job['user_name'] = manager.user_model.query.filter_by( id=job['user']).first().username # 抄送人 copy_users_name = [] if job['copy_person']: for u in job['copy_person'].split(','): copy_user = manager.user_model.query.filter_by( id=int(u)).first().username if u else None # 如果用户存在则加入抄送人列表 copy_users_name.append(copy_user) if copy_user else 1 job['copy_person_name'] = copy_users_name results.items = jobs data = paging(results) return data
def user_project(cls, uid, name, page, count): count = int(count) if count else current_app.config.get( 'COUNT_DEFAULT') page = int( page) if page else current_app.config.get('PAGE_DEFAULT') + 1 if not uid: uid = current_user.id results = cls.query.filter( cls.user == uid, cls.name.like(f'%{name}%') if name is not None else '', cls.delete_time == None, ).with_entities( cls.id, cls.name, cls.server, cls.header, cls.info, cls._type.label('type'), cls.running, cls.progress, cls.user, cls.send_email, cls.copy_person, ).order_by(text('Project.running desc'), text('Project.update_time desc')).paginate(page, count) projects = [ dict(zip(result.keys(), result)) for result in results.items ] for project in projects: # 添加user_name属性 user_name = manager.user_model.query.filter_by( id=project['user']).first().username project['user_name'] = user_name # 抄送人 copy_users_name = [] if project['copy_person']: for u in project['copy_person'].split(','): copy_user = manager.user_model.query.filter_by( id=int(u)).first().username if u else None # 如果用户存在则加入抄送人列表 copy_users_name.append(copy_user) if copy_user else 1 project['copy_person_name'] = copy_users_name results.items = projects data = paging(results) return data
def user_case(cls, uid, name, page, count): count = int(count) if count else current_app.config.get( 'COUNT_DEFAULT') page = int( page) if page else current_app.config.get('PAGE_DEFAULT') + 1 from app.models.CaseGroup import CaseGroup if not uid: uid = current_user.id results = cls.query.join(CaseGroup, CaseGroup.id == cls.case_group). \ join(manager.user_model, manager.user_model.id == cls.create_user).filter( cls.create_user == uid, cls.name.like(f'%{name}%') if name is not None else '', cls.delete_time == None, ).with_entities( cls.id, cls.name, cls.info, cls.url, cls._method.label('method'), cls._submit.label('submit'), cls.header, cls.data, cls._deal.label('deal'), cls.condition, cls._type.label('type'), cls.expect.label('expect'), cls._assertion.label('assertion'), cls.case_group.label('case_group'), CaseGroup.name.label('group_name'), manager.user_model.username.label('create_user'), ).order_by( text('Case.update_time desc') ).paginate(page, count) items = [dict(zip(result.keys(), result)) for result in results.items] results.items = items data = paging(results) return data