Exemple #1
0
def set_info():
    if UtilHelper.isGet():
        return UtilHelper.renderPopView("home/user/profile/set_info.html", {"info": CurrentUserService.getInfo() })
    req = request.values
    name = req.get("name", "").strip()
    email = req.get("email", "").strip()
    if not ValidateHelper.validLength(name, 1, 10):
        return UtilHelper.renderErrJSON("请输入符合规范的姓名~~")

    if not ValidateHelper.validEMail(email):
        return UtilHelper.renderErrJSON("请输入符合规范的邮箱~~")

    info = CurrentUserService.getInfo()
    if not info:
        return UtilHelper.renderErrJSON( CommonConstant.SYSTEM_DEFAULT_ERROR )

    has_in = User.query.filter(User.email == email, User.id != info.id ).first()
    if has_in:
        return UtilHelper.renderErrJSON("该邮箱已存在,请换一个再试~~")

    info.name = name
    info.email = email
    db.session.add(info)
    db.session.commit()
    return UtilHelper.renderSucJSON()
Exemple #2
0
def home_news():
    query = UserNews.query.filter_by(
        uid=CurrentUserService.getUid(),
        status=CommonConstant.default_status_false)
    total = query.count()
    list = query.order_by(UserNews.id.desc()).all()
    data = []
    if list:
        for item in list:
            tmp_content = "<h5 class='text-danger'>标题:%s</h5><br/>%s<br/>时间:%s" % (
                item.title, item.content, item.created_time)
            tmp_data = {
                "id":
                item.id,
                "title":
                item.title,
                "content":
                tmp_content,
                "created_time":
                DateHelper.getFormatDate(DateHelper.str2Date(
                    item.created_time),
                                         format="%m-%d %H:%M"),
            }
            data.append(tmp_data)
    content = UtilHelper.renderView("home/index/news.html", {
        "list": data,
        "total": total
    })
    return UtilHelper.renderSucJSON({"total": total, "content": content})
Exemple #3
0
def cate_set():
    if UtilHelper.isGet():
        req = request.values
        id = int(req['id']) if ('id' in req and req['id']) else 0
        info = None
        if id > 0:
            info = JobCategory.query.filter_by(id=id).first()

        return UtilHelper.renderPopView("home/job/cate/set.html",
                                        {"info": info})

    req = request.values
    id = int(req['id']) if ('id' in req and req['id']) else 0
    name = req.get("name", "").strip()

    if not ValidateHelper.validLength(name, 1, 30):
        return UtilHelper.renderErrJSON("请输入符合规范的名称~~")

    info = JobCategory.query.filter_by(id=id).first()

    if info:
        model_server = info
    else:
        model_server = JobCategory()

    model_server.name = name
    db.session.add(model_server)
    db.session.commit()
    return UtilHelper.renderSucJSON()
Exemple #4
0
def config_set():
    if UtilHelper.isGet():
        req = request.values
        id = int(req['id']) if ('id' in req and req['id']) else 0
        info = None
        if id > 0:
            info = Sysconfig.query.filter_by(id=id).first()

        return UtilHelper.renderPopView("home/config/set.html", {"info": info})

    req = request.values
    id = int(req['id']) if ('id' in req and req['id']) else 0
    k_val = req.get("k_val", "").strip()

    if not ValidateHelper.validLength(k_val, 1):
        return UtilHelper.renderErrJSON("请输入符合规范的值~~")

    info = Sysconfig.query.filter_by(id=id).first()

    if info:
        model_config = info
    else:
        model_config = Sysconfig()

    model_config.k_val = k_val
    db.session.add(model_config)
    db.session.commit()
    return UtilHelper.renderSucJSON()
Exemple #5
0
def staff_set():
    if UtilHelper.isGet() :
        req = request.values
        id = int( req['id'] ) if ( 'id' in req and req['id'] )else 0
        info = None
        if id > 0:
            info = User.query.filter_by( id=id ).first()

        ##部门
        dept_list = Role.query.order_by( Role.pid.asc() ).all()
        dept_data = {}
        if dept_list:
            for item in dept_list:
                if not item.pid  and item.id not in dept_data:
                    dept_data[ item.id ] = {
                        "name": item.name,
                        "sub" : []
                    }

                if item.pid:
                    dept_data[item.pid]['sub'].append( { "id":item.id,"name": item.name } )


        return UtilHelper.renderPopView( "home/rbac/staff/set.html",{ "info":info,"dept_list":dept_data }  )

    req = request.values
    id = int(req['id']) if ('id' in req and req['id']) else 0
    name = req.get("name","").strip()
    email = req.get("email","").strip()
    role_id = int( req.get("role_id",0 ) )


    if not ValidateHelper.validLength( name,1,10 ):
        return UtilHelper.renderErrJSON("请输入符合规范的姓名~~")

    if not ValidateHelper.validEMail( email ):
        return UtilHelper.renderErrJSON("请输入符合规范的邮箱~~")

    if role_id < 1:
        return UtilHelper.renderErrJSON("请选择部门(顶级部门不可选择)~~")

    has_in = User.query.filter(User.email == email, User.id != id).first()
    if has_in:
        return UtilHelper.renderErrJSON("该邮箱已存在,请换一个再试~~")

    info = User.query.filter_by(id=id).first()
    if info:
        model_user = info
    else:
        model_user = User()

    if not model_user.salt:
        model_user.salt = CurrentUserService.geneSalt()

    model_user.name = name
    model_user.email = email
    model_user.role_id = role_id
    db.session.add( model_user )
    db.session.commit()
    return UtilHelper.renderSucJSON()
Exemple #6
0
def gii_job():
    default_path_prefix = "/jobs/tasks/"
    if UtilHelper.isGet():

        return UtilHelper.renderView(
            "/home/tools/gii/job.html",
            {"default_path_prefix": default_path_prefix})

    req = request.values

    filename = req.get("filename", "").strip()
    path = req.get("path", "").strip()
    note = req.get("note", "").strip()

    ##后面这里的数据库做成选择的,因为有多数据库的可能
    folder_path = app.root_path + default_path_prefix + path
    #不存在就新建
    if not os.path.exists(folder_path):
        os.makedirs(folder_path)

    task_path = folder_path + "/" + filename + ".py"

    module_task = ((path + "/") if path else '') + filename
    tips = 'python {0}/manage_job.py runjob -m {1}'.format(
        app.root_path, module_task)

    try:
        content = '''
# -*- coding: utf-8 -*-
import logging
from flask.logging import default_handler
from jobs.BaseJob import BaseJob
from application import app

\'\'\'
{0}
{1}
\'\'\'
class JobTask( BaseJob ):
    def __init__(self):
        ## 设置Job使用debug模式
        app.config['DEBUG'] = True
        logging_format = logging.Formatter(
            '%(levelname)s %(asctime)s %(filename)s:%(funcName)s L%(lineno)s %(message)s')
        default_handler.setFormatter(logging_format)

    def run(self, params):
        app.logger.info( "执行命令是:{0}" )
        app.logger.info( "这是自动生成的job" )
        return True        
        '''.format(note, tips)

        f = open(task_path, "w", encoding="utf-8")
        f.write(content)
        f.flush()
        f.close()
    except Exception as e:
        pass

    return UtilHelper.renderSucJSON()
Exemple #7
0
def grant_set():
    if UtilHelper.isGet():
        req = request.values
        id = int(req['id']) if ('id' in req and req['id']) else 0
        act = req.get("act", "").strip()
        info = None
        if id > 0:
            info = Action.query.filter_by(id=id).first()

        weight_list = [1]
        weight_list.extend(range(5, 80, 5))

        if act == "copy" and info:
            info.id = 0
        return UtilHelper.renderPopView("home/rbac/grant/set.html", {
            "info": info,
            "weight_list": weight_list,
        })

    req = request.values
    id = int(req['id']) if ('id' in req and req['id']) else 0
    level1_name = req.get("level1_name", "").strip()
    level2_name = req.get("level2_name", "").strip()
    name = req.get("name", "").strip()
    url = req.get("url", "").strip()
    level1_weight = int(req.get("level1_weight", "1").strip())
    level2_weight = int(req.get("level2_weight", "1").strip())
    weight = int(req.get("weight", "1").strip())

    if not ValidateHelper.validLength(level1_name, 1, 20):
        return UtilHelper.renderErrJSON("请输入符合规范的一级菜单名称~~")

    if not ValidateHelper.validLength(level2_name, 1, 20):
        return UtilHelper.renderErrJSON("请输入符合规范的二级菜单名称~~")

    if not ValidateHelper.validLength(name, 1, 20):
        return UtilHelper.renderErrJSON("请输入符合规范的权限名称~~")

    info = Action.query.filter_by(id=id).first()
    if info:
        model_action = info
    else:
        model_action = Action()

    model_action.level1_name = level1_name
    model_action.level2_name = level2_name
    model_action.name = name
    model_action.url = url.replace("\r\n", ",")
    model_action.level1_weight = level1_weight
    model_action.level2_weight = level2_weight
    model_action.weight = weight

    db.session.add(model_action)
    db.session.commit()
    return UtilHelper.renderSucJSON()
Exemple #8
0
def server_set():
    if UtilHelper.isGet():
        req = request.values
        id = int(req['id']) if ('id' in req and req['id']) else 0
        info = None
        env_list = []
        if id > 0:
            info = JobServer.query.filter_by(id=id).first()
            env_list = (info.env).strip(CommonConstant.special_strike).split(
                CommonConstant.special_strike)

        weight_list = []
        weight_list.extend(range(1, 10))

        return UtilHelper.renderPopView(
            "home/job/server/set.html", {
                "info": info,
                "server_env_map": CommonConstant.server_env_map,
                "weight_list": weight_list,
                "env_list": env_list,
            })

    req = request.values
    id = int(req['id']) if ('id' in req and req['id']) else 0
    name = req.get("name", "").strip()
    env = request.form.getlist("env[]")
    note = req.get("note", "").strip()
    weight = int(req.get("weight", "1").strip())

    if not ValidateHelper.validLength(name, 1, 30):
        return UtilHelper.renderErrJSON("请输入符合规范的名称~~")

    if len(env) < 1:
        return UtilHelper.renderErrJSON("请选择支持环境~~")

    info = JobServer.query.filter_by(id=id).first()

    if info:
        model_server = info
    else:
        model_server = JobServer()

    model_server.name = name
    model_server.env = "{0}{1}{0}".format(
        CommonConstant.special_strike,
        (CommonConstant.special_strike).join(env),
        CommonConstant.special_strike)
    model_server.note = note
    model_server.weight = weight
    db.session.add(model_server)
    db.session.commit()
    return UtilHelper.renderSucJSON()
Exemple #9
0
def news_ops():
    req = request.values
    id = int(req['id']) if ('id' in req and req['id']) else 0
    if not id:
        return UtilHelper.renderErrJSON( CommonConstant.SYSTEM_DEFAULT_ERROR )


    info = UserNews.query.filter_by( id = id,uid = CurrentUserService.getUid() ).first()
    if not info:
        return UtilHelper.renderErrJSON( "指定站内信不存在" )

    info.status = CommonConstant.default_status_true
    db.session.add( info )
    db.session.commit()
    return UtilHelper.renderSucJSON()
Exemple #10
0
def link_set():
    if UtilHelper.isGet():
        req = request.values
        id = int(req['id']) if ('id' in req and req['id']) else 0
        info = None
        if id > 0:
            info = Link.query.filter_by(id=id).first()
        return UtilHelper.renderPopView(
            "home/link/set.html", {
                "info": info,
                "type_map": CommonConstant.link_type_map
            })

    req = request.values
    id = int(req['id']) if ('id' in req and req['id']) else 0
    type = int(req.get("type", 0).strip())
    title = req.get("title", "").strip()
    url = req.get("url", "").strip()
    weight = int(req.get("weight", 1).strip())

    if type < 1:
        return UtilHelper.renderErrJSON("请选择分类~~")

    if not ValidateHelper.validLength(title, 1, 30):
        return UtilHelper.renderErrJSON("请输入符合规范的标题~~")

    if not ValidateHelper.validUrl(url):
        return UtilHelper.renderErrJSON("请输入符合规范的网址~~")

    info = Link.query.filter_by(id=id).first()
    if info:
        model_link = info
    else:
        model_link = Link()

    model_link.type = type
    model_link.title = title
    model_link.url = url
    model_link.weight = weight
    db.session.add(model_link)
    db.session.commit()
    return UtilHelper.renderSucJSON()
Exemple #11
0
def dept_set():
    if UtilHelper.isGet() :
        req = request.values
        id = int( req['id'] ) if ( 'id' in req and req['id'] )else 0
        info = None
        query_role_list = Role.query.filter_by( pid = CommonConstant.default_status_false )
        if id > 0:
            info = Role.query.filter_by( id=id ).first()
            query_role_list = query_role_list.filter( Role.id != id )

        role_list = query_role_list.order_by( Role.id.desc() ).all()

        return UtilHelper.renderPopView( "home/rbac/dept/set.html",{ "info":info ,"role_list": role_list}  )

    req = request.values
    id = int(req['id']) if ('id' in req and req['id']) else 0
    name = req.get("name","").strip()
    pid = int( req.get("pid","0").strip() )


    if not ValidateHelper.validLength( name,1,10 ):
        return UtilHelper.renderErrJSON("请输入符合规范的姓名~~")

    info = Role.query.filter_by(id=id).first()
    if info:
        model_role = info
        #还不能选择自己的子节点

        if id == pid:
            return UtilHelper.renderErrJSON("不能勾选自己为所属部门哦~~")
    else:
        model_role = Role()

    model_role.name = name
    model_role.pid = pid
    db.session.add( model_role )
    db.session.commit()
    return UtilHelper.renderSucJSON()
Exemple #12
0
def grant_ops():
    req = request.values
    id = int(req['id']) if ('id' in req and req['id']) else 0
    act = req.get("act", "").strip()
    allow_act = ['del', 'recovery']
    if not id:
        return UtilHelper.renderErrJSON(CommonConstant.SYSTEM_DEFAULT_ERROR)

    if act not in allow_act:
        return UtilHelper.renderErrJSON(CommonConstant.SYSTEM_DEFAULT_ERROR)

    info = Action.query.filter_by(id=id).first()
    if not info:
        return UtilHelper.renderErrJSON("指定权限不存在")

    if act == "del":
        info.status = CommonConstant.default_status_false
    elif act == "recovery":
        info.status = CommonConstant.default_status_true

    db.session.add(info)
    db.session.commit()
    return UtilHelper.renderSucJSON()
Exemple #13
0
def grant_assign():
    if UtilHelper.isGet():
        req = request.values
        role_id = int(req.get("role_id", 0))
        role_pid = int(req.get("role_pid", 0))
        '''
        取出来所有的一级部门
        '''
        p_role_list = Role.query.filter_by(pid=CommonConstant.default_status_false)\
            .order_by( Role.id.asc() ).all()

        if not role_pid and p_role_list:  #如果没有父部门,那就选择一个
            role_pid = p_role_list[0].id

        sub_role_list = Role.query.filter_by( pid = role_pid)\
            .order_by( Role.id.asc() ).all()

        if not role_id and sub_role_list:
            role_id = sub_role_list[0].id

        user_list = User.query.filter_by(
            status=CommonConstant.default_status_true, role_id=role_id).all()

        action_list = Action.query.filter_by( status = CommonConstant.default_status_true )\
            .order_by( Action.level1_weight.desc(),Action.level2_weight.desc(),Action.weight.desc() ).all()

        action_data = {}
        if action_list:
            for item in action_list:
                tmp_level1_key = item.level1_name
                tmp_level2_key = item.level2_name
                if tmp_level1_key not in action_data:
                    action_data[tmp_level1_key] = {
                        "name": tmp_level1_key,
                        "counter": 0,
                        "sub": {}
                    }

                if tmp_level2_key not in action_data[tmp_level1_key]['sub']:
                    action_data[tmp_level1_key]['sub'][tmp_level2_key] = {
                        "name": tmp_level2_key,
                        "counter": 0,
                        "act_list": []
                    }

                action_data[tmp_level1_key]['counter'] += 1
                action_data[tmp_level1_key]['sub'][tmp_level2_key][
                    'counter'] += 1

                tmp_data = {
                    "id": item.id,
                    "name": item.name,
                    "is_important": item.is_important
                }
                action_data[tmp_level1_key]['sub'][tmp_level2_key][
                    'act_list'].append(tmp_data)

        owned_act = RoleAction.query.filter_by(
            role_id=role_id, status=CommonConstant.default_status_true).all()
        owned_act_ids = ModelHelper.getFieldList(owned_act, 'action_id')

        sc = {
            "role_id": role_id,
            "role_pid": role_pid,
        }

        return UtilHelper.renderView(
            "home/rbac/grant/assign.html", {
                "list": action_data,
                "p_role_list": p_role_list,
                "sub_role_list": sub_role_list,
                "user_list": user_list,
                "owned_act_ids": owned_act_ids,
                "sc": sc
            })

    req = request.values
    role_id = int(req.get("role_id", 0))
    action_ids = request.form.getlist("action_ids[]")
    action_ids = list(map(int, action_ids))
    if not role_id or role_id < 1:
        return UtilHelper.renderErrJSON("请选择部门在分配权限~~")

    info = Role.query.filter_by(id=role_id).first()
    if not info:
        return UtilHelper.renderErrJSON("请选择部门在分配权限 -2~~")
    ###分配权限逻辑还是挺复杂的
    ## 已有的权限
    owned_act = RoleAction.query.filter_by(role_id=role_id).all()
    owned_act_ids = ModelHelper.getFieldList(owned_act, 'action_id')
    '''
    找出删除的权限(生产环境数据库没有删除权限)
    假如已有的权限集合是A,界面传递过得权限集合是B
    权限集合A当中的某个权限不在权限集合B当中,就应该删除
    计算差集
    '''
    delete_act_ids = list(set(owned_act_ids) - set(action_ids))
    if delete_act_ids:
        RoleAction.query.filter( RoleAction.role_id == role_id,RoleAction.action_id.in_( delete_act_ids ) )\
            .update({ "status":CommonConstant.default_status_false },synchronize_session = False)
        db.session.commit()
    '''
    找出添加的权限
    假如已有的权限集合是A,界面传递过得权限集合是B
    权限集合B当中的某个权限不在权限集合A当中,就应该添加
    计算差集
    '''
    add_act_ids = list(set(action_ids) - set(owned_act_ids))
    if add_act_ids:
        for _action_id in add_act_ids:
            _model_role_action = RoleAction()
            _model_role_action.role_id = role_id
            _model_role_action.action_id = _action_id
            db.session.add(_model_role_action)
        db.session.commit()
    '''
    找出需要更新的权限(生产环境数据库没有删除权限)
    假如已有的权限集合是A,界面传递过得权限集合是B
    权限集合B当中的某个权限也在在权限集合A当中,就应该更新
    计算补集
    '''
    update_act_ids = list(set(owned_act_ids).intersection(set(action_ids)))
    if update_act_ids:
        RoleAction.query.filter(RoleAction.role_id == role_id, RoleAction.action_id.in_(update_act_ids)) \
            .update({"status": CommonConstant.default_status_true}, synchronize_session=False)
        db.session.commit()

    return UtilHelper.renderSucJSON({}, "权限分配成功~~")
Exemple #14
0
def news_batch_ops():
    UserNews.query.filter_by( uid = CurrentUserService.getUid() ).update({"status": CommonConstant.default_status_true })
    db.session.commit()
    return UtilHelper.renderSucJSON()
Exemple #15
0
def job_ops():
    req = request.values
    id = int(req['id']) if ('id' in req and req['id']) else 0
    act = req.get("act", "").strip()
    allow_act = [
        'del', 'recovery', 'system_not_run', 'system_run', 'run_next', 'kill'
    ]
    if not id:
        return UtilHelper.renderErrJSON(CommonConstant.SYSTEM_DEFAULT_ERROR +
                                        " -1")

    if act not in allow_act:
        return UtilHelper.renderErrJSON(CommonConstant.SYSTEM_DEFAULT_ERROR +
                                        " -2")

    info = JobList.query.filter_by(id=id).first()
    if not info:
        return UtilHelper.renderErrJSON("指定数据不存在")

    ##在增加一个判断,除了负责人和相关人外,还有管理严,其他人不能操作
    allow_ops_uids = [info.owner_uid, info.relate_uid]
    if not CurrentUserService.isRoot() and CurrentUserService.getUid(
    ) not in allow_ops_uids:
        return UtilHelper.renderErrJSON("非Job负责人或者相关人禁止操作")

    if act == "kill" and info.run_status != CommonConstant.default_status_pos_2:
        return UtilHelper.renderErrJSON("强制杀死的job必须正在运行")

    if act != "kill" and info.run_status == CommonConstant.default_status_pos_2:
        return UtilHelper.renderErrJSON("正在运行的Job不能操作")

    if act == "del":
        info.is_del = CommonConstant.default_status_true
    elif act == "recovery":
        info.is_del = CommonConstant.default_status_false
    elif act == "system_not_run":
        info.status = CommonConstant.default_status_false
    elif act == "system_run":
        #如果可以调度了,是不是要把调度时间改成下一个周期
        info.status = CommonConstant.default_status_true
        info.run_status = CommonConstant.default_status_true
        if int(info.job_type
               ) == CommonConstant.default_status_pos_2:  # 常驻Job,他停止之后下一分钟直接运行
            next_date = datetime.datetime.now() + datetime.timedelta(minutes=1)
            next_date = next_date.replace(second=0)
            tmp_next_time = int(time.mktime(next_date.timetuple()))
        else:
            tmp_next_time = info.next_run_time + int(
                math.ceil((time.time() - info.next_run_time) /
                          (info.run_interval * 60)) * info.run_interval * 60)
        info.next_run_time = tmp_next_time
    elif act == "run_next":

        if not info.status:
            return UtilHelper.renderErrJSON("Job 未调度,无法立即调度")

        ##下一分钟
        next_date = datetime.datetime.now() + datetime.timedelta(minutes=1)
        next_date = next_date.replace(second=0)
        info.next_run_time = int(time.mktime(next_date.timetuple()))
        info.run_status = CommonConstant.default_status_true
    elif act == "kill":
        ##这里是杀死job的判断
        has_not_handle = JobKillQueue.query.filter_by(
            job_id=info.id,
            status=CommonConstant.default_status_neg_2).count()
        if has_not_handle:
            return UtilHelper.renderErrJSON("该Job还有未执行的杀死任务,请等上一个执行完毕之后在点击~~")

        model_kill = JobKillQueue()
        model_kill.job_id = info.id
        model_kill.server_id = info.server_id
        db.session.add(model_kill)

    db.session.add(info)
    db.session.commit()
    return UtilHelper.renderSucJSON()
Exemple #16
0
def gii_model():
    default_path_prefix = "/common/models/"
    if UtilHelper.isGet():
        db.reflect()
        #dict_keys 转 list
        table_list = list(db.metadata.tables.keys())
        table_list.sort()
        return UtilHelper.renderView("/home/tools/gii/model.html", {
            "default_path_prefix": default_path_prefix,
            "table_list": table_list
        })

    req = request.values

    table = req.get("table", "").strip()
    path = req.get("path", "").strip()

    ##后面这里的数据库做成选择的,因为有多数据库的可能
    engine_uri = app.config.get('SQLALCHEMY_DATABASE_URI', '')
    folder_path = app.root_path + default_path_prefix + path
    #不存在就新建
    if not os.path.exists(folder_path):
        os.makedirs(folder_path)

    model_name = (table.replace("-",
                                " ").replace("_",
                                             " ").title()).replace(" ", "")
    model_path = folder_path + "/" + model_name + ".py"

    #2>&1 标准错误重定向到标准输出, --noinflect 不把复数处理成单数 例如 user_news 会变成 UserNew
    cmd = 'flask-sqlacodegen "{0}" --noinflect --tables {1} --outfile "{2}"  --flask 2>&1'.format(
        engine_uri, table, model_path)
    print(cmd)
    p = os.popen(cmd)
    out_list = p.readlines()
    p.close()
    if out_list and len(out_list) > 0:
        return UtilHelper.renderErrJSON("失败原因:" + "<br/>".join(out_list))

    ##为了不破坏扩展,实现正则替换,读文件,按行替换
    if not os.path.exists(model_path):
        return UtilHelper.renderErrJSON("model文件不存在,无法执行替换~~")
    try:
        f = open(model_path)
        ret = []
        ret.append("# coding: utf-8\n")
        ret.append("from application import db\n\n")
        ignore_kws = [
            "from flask_sqlalchemy", "from sqlalchemy", "SQLAlchemy",
            "coding: utf-8"
        ]
        line_break_kws = ["\n", "\r\n"]
        for line in f.readlines():
            tmp_break_flag = False
            for _ignore_kw in ignore_kws:
                if _ignore_kw in line:
                    tmp_break_flag = True
                    break

            if tmp_break_flag:
                continue

            if line in line_break_kws:
                continue
            ret.append(line)
        f.close()

        ##最后加一些常用方法
        common_funcs = '''
    def __init__(self, **items):
        for key in items:
            if hasattr(self, key):
                setattr(self, key, items[key])
        '''
        f = open(model_path, "w", encoding="utf-8")
        f.write("".join(ret) + common_funcs)
        f.flush()
        f.close()
    except Exception as e:
        pass

    return UtilHelper.renderSucJSON()
Exemple #17
0
def job_set():
    if UtilHelper.isGet():
        req = request.values
        id = int(req['id']) if ('id' in req and req['id']) else 0
        act = req.get("act", "").strip()
        info = {'owner_uid': CurrentUserService.getUid()}
        if id > 0:
            info = JobList.query.filter_by(id=id).first()
            info = ModelHelper.model2Dict(info)
            info['next_run_time'] = DateHelper.getDateOnTimestamps(
                info['next_run_time'], '%Y-%m-%d %H:%M')

        if act == "copy":
            info['id'] = 0
            info['status'] = 0
            info['name'] = "【复制】" + info['name']

        server_list = JobServer.query.order_by(JobServer.id.desc()).all()
        user_list = User.query.order_by(User.id.desc()).all()
        cate_list = JobCategory.query.order_by(JobCategory.id.desc()).all()

        return UtilHelper.renderView(
            "home/job/index/set.html", {
                "info": info,
                "server_list": server_list,
                "user_list": user_list,
                "cate_list": cate_list,
                "server_env_map": CommonConstant.server_env_map,
                "job_type_map": CommonConstant.job_type_map,
                "job_status_map": job_status_map,
                "job_level_map": CommonConstant.job_level_map,
            })

    req = request.values
    id = int(req['id']) if ('id' in req and req['id']) else 0
    cate_id = int(req.get("cate_id", "0").strip())
    name = req.get("name", "").strip()
    env_id = int(req.get("env_id", "0").strip())
    server_id = int(req.get("server_id", "0").strip())
    owner_uid = int(req.get("owner_uid", "0").strip())
    relate_uid = int(req.get("relate_uid", "0").strip())
    command = req.get("command", "").strip()
    command_kill = req.get("command_kill", "").strip()
    job_type = int(req.get("job_type", "0").strip())
    job_level = int(req.get("job_level", "1").strip())
    status = int(req.get("status", "0").strip())
    next_run_time = req.get("next_run_time", "").strip()
    run_interval = int(req.get("run_interval", "0").strip())
    threshold_down = int(req.get("threshold_down", "0").strip())
    threshold_up = int(req.get("threshold_up", "0").strip())
    note = req.get("note", "").strip()

    if cate_id < 1:
        return UtilHelper.renderErrJSON("请选择所属分类~~")

    if not ValidateHelper.validLength(name, 1, 15):
        return UtilHelper.renderErrJSON("请输入符合规范的名称~~")

    if env_id < 1:
        return UtilHelper.renderErrJSON("请选择运行环境~~")

    if server_id < 1:
        return UtilHelper.renderErrJSON("请选择运行服务器~~")

    if owner_uid < 1:
        return UtilHelper.renderErrJSON("请选择Job负责人~~")

    if relate_uid < 1:
        return UtilHelper.renderErrJSON("请选择Job相关人~~")

    if not ValidateHelper.validLength(command, 5):
        return UtilHelper.renderErrJSON("请输入Job命令~~")

    if job_type < 1:
        return UtilHelper.renderErrJSON("请选择Job类型~~")

    if not ValidateHelper.validDate(next_run_time,
                                    r'^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$'):
        return UtilHelper.renderErrJSON("请选择调度时间~~")

    if job_type != CommonConstant.default_status_pos_2:
        if run_interval < 1:
            return UtilHelper.renderErrJSON("请输入运行间隔~~")

        if threshold_down < 0:
            return UtilHelper.renderErrJSON("请输入预估最短运行时长~~")

        if threshold_up < 1:
            return UtilHelper.renderErrJSON("请输入预估最长运行时长~~")
    else:
        run_interval = threshold_down = threshold_up = 0

    info = JobList.query.filter_by(id=id).first()

    if info:
        model_job = info
        ##正在运行的Job不能编辑运行时间
        if info.run_status == CommonConstant.default_status_pos_2:
            if info.command != command:
                return UtilHelper.renderErrJSON("正在运行的Job不能修改运行命令~~")

            if info.job_type != job_type:
                return UtilHelper.renderErrJSON("正在运行的Job不能修改类型~~")

            if info.status != status:
                return UtilHelper.renderErrJSON("正在运行的Job不能修改调度状态~~")

            if info.next_run_time != DateHelper.getTimestamps(next_run_time +
                                                              ":00"):
                return UtilHelper.renderErrJSON("正在运行的Job不能修改调度时间~~")

    else:
        model_job = JobList()

    ##只有需要调度的才需要判断时间
    if model_job.run_status != CommonConstant.default_status_pos_2 and status \
            and DateHelper.getTimestamps(next_run_time + ":00") < time.time():
        return UtilHelper.renderErrJSON("调度时间不能小于当前时间~~")

    model_job.name = name
    model_job.env_id = env_id
    model_job.server_id = server_id
    model_job.owner_uid = owner_uid
    model_job.relate_uid = relate_uid
    model_job.job_type = job_type
    model_job.job_level = job_level
    model_job.cate_id = cate_id
    model_job.command = command
    model_job.command_kill = command_kill
    model_job.next_run_time = DateHelper.getTimestamps(next_run_time + ":00")
    model_job.run_interval = run_interval
    model_job.threshold_up = threshold_up
    model_job.threshold_down = threshold_down
    model_job.note = note
    model_job.status = status
    if status and (not info or not info.run_status):
        model_job.run_status = CommonConstant.default_status_true

    db.session.add(model_job)
    db.session.commit()

    return UtilHelper.renderSucJSON({"id": model_job.id})