def init_sys_tables(db_name): """ 初始化系统表Tables, Fields, Sensitivity :return: """ session = get_session() r = session.execute( "select TABLE_NAME from information_schema.TABLES where table_schema = '{}'" .format(db_name)).fetchall() # print(r[0][0]) added = True # 安全开关 if not added: for i in range(len(r)): table_name = r[i][0] new_t = Tables(name=table_name, sensitivity=0) session.add(new_t) session.commit() f = session.execute( "SELECT COLUMN_NAME FROM information_schema.columns\ WHERE table_schema = '{}' AND table_name = '{}'" .format(db_name, table_name)).fetchall() for j in range(len(f)): new_f = Fields(name=f[j][0], table_id=new_t.id, sensitivity=0) session.add(new_f) session.commit() session.close()
def all_tables(db_name): """ 返回当前数据库中所有表名 :return: """ session = get_session() r = session.execute( "select TABLE_NAME from information_schema.TABLES where table_schema = '{}'" .format(db_name)).fetchall() session.close() a = [] for i in range(len(r)): a.append(r[i][0]) return a
def index(): data = { 'tag_list': [ '模块管理', '数据库管理', ], 'current_tag': '模块管理', 'tables': {}, 'show_info': { 'id': '表id', 'description': '描述', 'owner_id': '所属模块', 'status': '状态', 'api_gene': 'API状态', 'sensitivity': '敏感度', }, } db_session = get_session() tables = db_session.query(Tables).all() data['count'] = len(tables) for table in tables: data['tables'][table.name] = {'fields': []} for field in table.fields: f = { 'id': field.id, 'name': field.name, 'table_id': field.table_id, 'sensitive': field.sensitivity, } data['tables'][table.name]['fields'].append(f) data['tables'][table.name]['id'] = table.id data['tables'][table.name]['name'] = table.name data['tables'][table.name]['owner_id'] = table.owner_id data['tables'][table.name]['status'] = table.status data['tables'][table.name]['api_gene'] = table.api_gene data['tables'][table.name]['sensitivity'] = table.sensitivity # fields = db_session.query(Fields).all() # for field in fields: # f = { # 'id': field.id, # 'name': field.name, # 'table_id': field.table_id, # 'sensitive': field.sensitivity, # } # data['tables'][field.table].append(f) return render_template('manager.html', data=data)
def create_app(mode): app = Flask(__name__) config = Config('conf.json') config['current_mod'] = mode config['init'] = True app.service_config = config # mode配置需要在db_engine产生前完成 from DataService.models import engine, get_session from DataService.api_manager import api_manager from DataService.table_manager import table_manager # 切换工作目录 os.chdir(config['working_dir']) # 设置全局db_session app.db_session = get_session() # 设置全局table_manager app.table_manager = table_manager() # flask-restless Session = sessionmaker(autocommit=False, autoflush=False, bind=engine) mysession = scoped_session(Session) restless_manager = APIManager( app, session=mysession) # 全局preprocessors postprocessors无效 原因不明 app.restless_manager = restless_manager api_manager.init_api_from_db(restless_manager) # url屏蔽 @app.before_request def intercept(): if request.path in app.service_config['api']['intercept_urls']: return general_error(403, 'request url is intercepted') from DataService.app.api import api app.register_blueprint(api, url_prefix='/api') # 设置全局api_manager sys_api_manager = api_manager(app) app.api_manager = sys_api_manager from DataService.app.manager import manager app.register_blueprint(manager, url_prefix='/manager') return app
def gene_file_pos(): """ 生成当前Tables file_pos :return: """ session = get_session() tables = session.query(Tables).all() t_manager = table_manager() for table in tables: if table.name in ['alembic_version']: continue cl = t_manager.get_table_info(table.name) if cl: table.file_pos = cl.to_json() session.commit() else: raise ValueError('{} do not match'.format(table.name))
from DataService.api_manager import api_manager from DataService.utils.config_parser import Config from DataService.errors import * from DataService.table_manager import table_manager import os app = Flask(__name__) config = Config('conf.json') app.service_config = config # 切换工作目录 os.chdir(config['working_dir']) # 设置全局db_session app.db_session = get_session() # 设置全局table_manager app.table_manager = table_manager() # flask-restless Session = sessionmaker(autocommit=False, autoflush=False, bind=engine) mysession = scoped_session(Session) restless_manager = APIManager( app, session=mysession) # 全局preprocessors postprocessors无效 原因不明 app.restless_manager = restless_manager api_manager.init_api_from_db(restless_manager) # url屏蔽 @app.before_request