()
添加函数文件
添加函数文件
添加函数文件
def add_func_file():
    """ 添加函数文件 """
    data = request.json
    name = data.get('name')
    higher_id = data.get('higherId')
    status = data.get('status')
    ids = data.get('id')
    if not name:
        return jsonify({'msg': '名称不能为空', 'status': 0})
    num = auto_num(data.get('num'), FuncFile)
    if ids:
        old_data = FuncFile.query.filter_by(id=ids).first()
        if status == 1:
            os.rename('{}/{}'.format(FUNC_ADDRESS, old_data.name), '{}/{}'.format(FUNC_ADDRESS, name))
        old_data.name = name
        old_data.num = num
        old_data.higher_id = higher_id
        db.session.commit()
        return jsonify({'msg': '修改成功', 'status': 1})
    else:
        if os.path.exists('{}/{}'.format(FUNC_ADDRESS, name)):
            return jsonify({'msg': '文件名已存在', 'status': 0})
        _new = FuncFile(name=name, higher_id=higher_id, num=num, status=status, user_id=current_user.id)
        db.session.add(_new)
        db.session.commit()
        if status == 1:
            with open('{}/{}'.format(FUNC_ADDRESS, name ), 'w', encoding='utf8') as f:
                pass

        return jsonify({'msg': '新建成功', 'status': 1, 'id': _new.id, 'higher_id': _new.higher_id, })
def add_func_file ( ) : data = request . json name = data . get ( 'name' ) higher_id = data . get ( 'higherId' ) status = data . get ( 'status' ) ids = data . get ( 'id' ) if not name : return jsonify ( { 'msg' : '名称不能为空', 'status':  0 ) num = auto_num ( data . get ( 'num' ) , FuncFile ) if ids : old_data = FuncFile . query . filter_by ( id = ids ) . first ( ) if status == 1 : os . rename ( '{}/{}' . format ( FUNC_ADDRESS , old_data . name ) , '{}/{}' . format ( FUNC_ADDRESS , name ) ) old_data . name = name old_data . num = num old_data . higher_id = higher_id db . session . commit ( ) return jsonify ( { 'msg' : '修改成功', 'statu s : 1}) else : if os . path . exists ( '{}/{}' . format ( FUNC_ADDRESS , name ) ) : return jsonify ( { 'msg' : '文件名已存在', 'status':  0 ) _new = FuncFile ( name = name , higher_id = higher_id , num = num , status = status , user_id = current_user . id ) db . session . add ( _new ) db . session . commit ( ) if status == 1 : with open ( '{}/{}' . format ( FUNC_ADDRESS , name ) , 'w' , encoding = 'utf8' ) as f : pass return jsonify ( { 'msg' : '新建成功', 'statu s : 1, 'id ' _ ew.i d  'hi g he r id': _new.h i her_ i d, })