()
任务添加、修改
任务添加、修改
任务添加、修改
def add_task():
    """ 任务添加、修改 """
    data = request.json
    project_id = data.get('projectId')
    if not project_id:
        return jsonify({'msg': '请选择项目', 'status': 0})
    set_ids = data.get('setIds')
    case_ids = data.get('caseIds')
    task_id = data.get('id')
    num = auto_num(data.get('num'), Task, project_id=project_id)
    name = data.get('name')
    task_type = 'cron'
    to_email = data.get('toEmail')
    send_email = data.get('sendEmail')
    password = data.get('password')
    send_email_status = data.get('sendEmailStatus')
    # 0 0 1 * * *
    if not (not to_email and not send_email and not password) and not (to_email and send_email and password):
        return jsonify({'msg': '发件人、收件人、密码3个必须都为空，或者都必须有值', 'status': 0})

    time_config = data.get('timeConfig')
    if len(time_config.strip().split(' ')) != 6:
        return jsonify({'msg': 'cron格式错误', 'status': 0})

    if task_id:
        old_task_data = Task.query.filter_by(id=task_id).first()
        if Task.query.filter_by(task_name=name).first() and name != old_task_data.task_name:
            return jsonify({'msg': '任务名字重复', 'status': 0})
        else:
            old_task_data.project_id = project_id
            old_task_data.set_id = json.dumps(set_ids)
            old_task_data.case_id = json.dumps(case_ids)
            old_task_data.task_name = name
            old_task_data.task_type = task_type
            old_task_data.task_to_email_address = to_email
            old_task_data.task_send_email_address = send_email
            old_task_data.email_password = password
            old_task_data.num = num
            old_task_data.send_email_status = send_email_status
            if old_task_data.status != '创建' and old_task_data.task_config_time != time_config:
                scheduler.reschedule_job(str(task_id), trigger='cron', **change_cron(time_config))  # 修改任务
                old_task_data.status = '启动'

            old_task_data.task_config_time = time_config
            db.session.commit()
            return jsonify({'msg': '修改成功', 'status': 1})
    else:

        if Task.query.filter_by(task_name=name).first():
            return jsonify({'msg': '任务名字重复', 'status': 0})
        else:
            new_task = Task(task_name=name,
                            project_id=project_id,
                            set_id=json.dumps(set_ids),
                            case_id=json.dumps(case_ids),
                            email_password=password,
                            task_type=task_type,
                            task_to_email_address=to_email,
                            task_send_email_address=send_email,
                            task_config_time=time_config,
                            send_email_status=send_email_status,
                            num=num)
            db.session.add(new_task)
            db.session.commit()
            return jsonify({'msg': '新建成功', 'status': 1})
def add_task ( ) : data = request . json project_id = data . get ( 'projectId' ) if not project_id : return jsonify ( { 'msg' : '请选择项目', 'status' : 0}) set_ids = data . get ( 'setIds' ) case_ids = data . get ( 'caseIds' ) task_id = data . get ( 'id' ) num = auto_num ( data . get ( 'num' ) , Task , project_id = project_id ) name = data . get ( 'name' ) task_type = 'cron' to_email = data . get ( 'toEmail' ) send_email = data . get ( 'sendEmail' ) password = data . get ( 'password' ) send_email_status = data . get ( 'sendEmailStatus' ) # 0 0 1 * * * if not ( not to_email and not send_email and not password ) and not ( to_email and send_email and password ) : return jsonify ( { 'msg' : '发件人、收件人、密码3个必须都为空，或者都必须有值', 'status': 0}) time_config = data . get ( 'timeConfig' ) if len ( time_config . strip ( ) . split ( ' ' ) ) != 6 : return jsonify ( { 'msg' : 'cron格式错误', 'statu s : 0}) if task_id : old_task_data = Task . query . filter_by ( id = task_id ) . first ( ) if Task . query . filter_by ( task_name = name ) . first ( ) and name != old_task_data . task_name : return jsonify ( { 'msg' : '任务名字重复', 'status':  0 ) else : old_task_data . project_id = project_id old_task_data . set_id = json . dumps ( set_ids ) old_task_data . case_id = json . dumps ( case_ids ) old_task_data . task_name = name old_task_data . task_type = task_type old_task_data . task_to_email_address = to_email old_task_data . task_send_email_address = send_email old_task_data . email_password = password old_task_data . num = num old_task_data . send_email_status = send_email_status if old_task_data . status != '创建' and old task_data.tas k _config_time !=  im _config: scheduler . reschedule_job ( str ( task_id ) , trigger = 'cron' , * * change_cron ( time_config ) ) # 修改任务 old_task_data . status = '启动' old_task_data . task_config_time = time_config db . session . commit ( ) return jsonify ( { 'msg' : '修改成功', 'statu s : 1}) else : if Task . query . filter_by ( task_name = name ) . first ( ) : return jsonify ( { 'msg' : '任务名字重复', 'status':  0 ) else : new_task = Task ( task_name = name , project_id = project_id , set_id = json . dumps ( set_ids ) , case_id = json . dumps ( case_ids ) , email_password = password , task_type = task_type , task_to_email_address = to_email , task_send_email_address = send_email , task_config_time = time_config , send_email_status = send_email_status , num = num ) db . session . add ( new_task ) db . session . commit ( ) return jsonify ( { 'msg' : '新建成功', 'statu s : 1})