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})
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()
def home_release(): release_list = [ { "title": "V1.3 上线", "date": "2020-11-01", "content": "监控常驻job内存消耗<br/>强制杀死job" }, { "title": "V1.2 上线", "date": "2020-10-20", "content": "增加常驻Job<br/>核心调度Job日志使用logrotate管理" }, { "title": "V1.1 上线", "date": "2020-10-14", "content": "增加Job分类管理,每个Job需要所属组<br/>增加钉钉和企业微信报警配置" }, { "title": "V1.0 上线", "date": "2020-10-03", "content": "Job管理调度平台 正式上线<br/>服务器管理、Job管理、Job工具、异常监控" }, { "title": "V0.1", "date": "2020-09-22", "content": "基础功能(员工管理、RBAC、日志管理、站内信、网址之家等)基于个人开源Python3 CMS,<a target='_blank' href='http://dcenter.jixuejima.cn/#/flask/v2/readme'>详情点击了解</a>" } ] return UtilHelper.renderView("home/index/release.html",{ "list" : release_list, "count" : len( release_list ) })
def error_exception( e ): err_msg = getErrMsg() AppLogService.addErrLog( err_msg ) if UtilHelper.isAjax(): return UtilHelper.renderErrJSON( "系统错误,错误原因:%s<br/>%s"%( str( e.__class__).replace("<",""),str( e ) ) ) return UtilHelper.renderView("home/error/error.html", {"status": 500, "msg": err_msg })
def home_index(): date = DateHelper.getCurrentTime(fmt="%Y-%m-%d") job_count = JobList.query.filter_by(is_del=CommonConstant.default_status_false).count() server_count = JobServer.query.filter_by(status=CommonConstant.default_status_true).count() alert_count = JobAlertList.query.filter(JobAlertList.created_time.between(date, date + " 23:59:59")).count() cate_map = ModelHelper.getDictFilterField( JobCategory ) cat_job_map = {} cat_job_list = JobList.query.with_entities(JobList.cate_id ,func.count( JobList.id) )\ .filter_by(is_del=CommonConstant.default_status_false)\ .group_by(JobList.cate_id).all() if cat_job_list: for _item in cat_job_list: cat_job_map[ _item[0] ] = _item[1] type_job_map = {} job_type_map = CommonConstant.job_type_map type_job_list = JobList.query.with_entities(JobList.job_type, func.count(JobList.id)) \ .filter_by(is_del=CommonConstant.default_status_false) \ .group_by(JobList.job_type).all() if type_job_list: for _item in type_job_list: type_job_map[ _item[0] ] = _item[1] return UtilHelper.renderView("home/index/index.html", { "job_count": job_count, "server_count": server_count, "alert_count": alert_count, 'cate_map' : cate_map, 'cat_job_map' : cat_job_map, 'job_type_map' : job_type_map, 'type_job_map' : type_job_map, })
def home_news(): req = request.values kw = req.get("kw", "").strip() status = int( req.get("status",CommonConstant.default_status_neg_99) ) page = int(req.get("p", 1)) query = UserNews.query.filter_by( uid = CurrentUserService.getUid() ) if kw: query = query.filter(UserNews.title.ilike('%{}%'.format(kw))) if status > CommonConstant.default_status_neg_99: query = query.filter_by( status = status ) page_params = { "total": query.count(), "page_size": CommonConstant.PAGE_SIZE, "page": page, "display": CommonConstant.PAGE_DISPLAY } pages = UtilHelper.iPagination(page_params) offset = (page - 1) * CommonConstant.PAGE_SIZE limit = CommonConstant.PAGE_SIZE * page list = query.order_by(UserNews.id.desc())[offset:limit] sc = { 'kw': kw, 'status': status } return UtilHelper.renderView("home/user/profile/news.html",{"list": list,"pages":pages,"sc":sc })
def dept_index(): dept_list = Role.query.order_by(Role.pid.asc()).all() list = [] if dept_list: dept_data = {} for item in dept_list: if not item.pid and item.id not in dept_data: dept_data[item.id] = { "self": item, "sub": [] } if item.pid: dept_data[item.pid]['sub'].append( item ) for item in dept_data.values(): list.append( item['self'] ) list.extend( item['sub']) set_flag = RBACService.checkPageRelatePrivilege("set") ops_flag = RBACService.checkPageRelatePrivilege("ops") return UtilHelper.renderView("home/rbac/dept/index.html", { "list": list, "set_flag": set_flag, "ops_flag": ops_flag, } )
def grant_index(): req = request.values page = int(req.get("p", 1)) query = Action.query page_params = { "total": query.count(), "page_size": CommonConstant.PAGE_SIZE, "page": page, "display": CommonConstant.PAGE_DISPLAY } pages = UtilHelper.iPagination(page_params) offset = (page - 1) * CommonConstant.PAGE_SIZE limit = CommonConstant.PAGE_SIZE * page list = query.order_by(Action.id.desc())[offset:limit] sc = {} set_flag = RBACService.checkPageRelatePrivilege("set") ops_flag = RBACService.checkPageRelatePrivilege("ops") return UtilHelper.renderView("home/rbac/grant/index.html", { "list": list, "pages": pages, "sc": sc, "set_flag": set_flag, "ops_flag": ops_flag, } )
def grant_index(): req = request.values kw = req.get("kw", "").strip() page = int(req.get("p", 1)) query = Action.query if kw: query = query.filter( or_(Action.name.ilike('%{}%'.format(kw)), Action.level1_name.ilike('%{}%'.format(kw)), Action.level2_name.ilike('%{}%'.format(kw)))) page_params = { "total": query.count(), "page_size": CommonConstant.PAGE_SIZE, "page": page, "display": CommonConstant.PAGE_DISPLAY } pages = UtilHelper.iPagination(page_params) offset = (page - 1) * CommonConstant.PAGE_SIZE limit = CommonConstant.PAGE_SIZE * page list = query.order_by(Action.id.desc())[offset:limit] sc = {"kw": kw} set_flag = RBACService.checkPageRelatePrivilege("set") ops_flag = RBACService.checkPageRelatePrivilege("ops") return UtilHelper.renderView( "home/rbac/grant/index.html", { "list": list, "pages": pages, "sc": sc, "set_flag": set_flag, "ops_flag": ops_flag, })
def log_error(): req = request.values page = int(req.get("p", 1)) date_from = req.get("date_from", DateHelper.getCurrentTime(fmt="%Y-%m-%d")) date_to = req.get("date_to", DateHelper.getCurrentTime(fmt="%Y-%m-%d")) query = AppErrLog.query.filter( AppErrLog.created_time.between(date_from, date_to + " 23:59:59")) page_params = { "total": query.count(), "page_size": CommonConstant.PAGE_SIZE, "page": page, "display": CommonConstant.PAGE_DISPLAY, } pages = UtilHelper.iPagination(page_params) offset = (page - 1) * CommonConstant.PAGE_SIZE limit = CommonConstant.PAGE_SIZE * page list = query.order_by(AppErrLog.id.desc())[offset:limit] sc = {'date_from': date_from, 'date_to': date_to} return UtilHelper.renderView("home/log/error.html", { "list": list, "pages": pages, "sc": sc })
def tools_index(): list = JobList.query.filter_by( run_status=CommonConstant.default_status_pos_2).order_by( JobList.id.desc()).all() return UtilHelper.renderView("home/job/tools/index.html", { "list": list, "current": "index" })
def job_info(): req = request.values id = int(req['id']) if ('id' in req and req['id']) else 0 info = JobList.query.filter_by(id=id).first() if not info: return redirect(GlobalUrlService.buildHomeUrl("/job/index/index")) info = ModelHelper.model2Dict(info) server_info = JobServer.query.filter_by(id=info['server_id']).first() cate_info = JobCategory.query.filter_by(id=info['cate_id']).first() server_env_map = CommonConstant.server_env_map run_status_map = CommonConstant.run_status_map info['next_run_time'] = DateHelper.getDateOnTimestamps( info['next_run_time'], '%Y-%m-%d %H:%M') info['env_name'] = server_env_map.get(info['env_id']) info['run_status_desc'] = run_status_map.get(info['run_status']) info['job_status_desc'] = job_status_map.get(info['status']) info['server_name'] = server_info.name info['cate_name'] = cate_info.name if cate_info else '' info['run_interval_desc'] = DateHelper.formatBeautyTime( info['run_interval'] * 60) user_map = ModelHelper.getDictFilterField( User, select_field=User.id, id_list=[info['owner_uid'], info['relate_uid']]) ##获取最近5天运行记录 log_list = JobRunLog.query.filter_by(job_id=id).order_by( JobRunLog.id.desc())[0:5] log_data = [] if log_list: for item in log_list: tmp_data = ModelHelper.model2Dict(item) tmp_data['status_desc'] = CommonConstant.job_log_status_map[ tmp_data['status']] tmp_data['duration'] = "" if DateHelper.getCurrentTime(date=tmp_data['end_time'] ) == CommonConstant.DEFAULT_DATETIME: tmp_data['end_time'] = "未知" tmp_data['duration'] = time.time() - time.mktime( tmp_data['start_time'].timetuple()) else: tmp_data['duration'] = tmp_data['end_time'].timestamp( ) - tmp_data['start_time'].timestamp() tmp_data['duration'] = DateHelper.formatBeautyTime( tmp_data['duration']) log_data.append(tmp_data) return UtilHelper.renderView( "home/job/index/info.html", { "info": info, "log_list": log_data, "user_map": user_map, "job_level_map": CommonConstant.job_level_map, })
def Login(): if request.method == "GET": if g.current_user: return redirect(GlobalUrlService.buildHomeUrl("/")) return UtilHelper.renderView("home/user/login_2.html") resp = {'code': 200, 'msg': '操作成功~~', 'data': {}} req = request.values email = req['email'] if 'email' in req else '' pwd = req['pwd'] if 'pwd' in req else '' if email is None or len(email) < 1: resp['code'] = -1 resp['msg'] = "请输入正确的邮箱~~" return jsonify(resp) if pwd is None or len(pwd) < 1: resp['code'] = -1 resp['msg'] = "请输入正确的邮箱密码~~" return jsonify(resp) user_info = User.query.filter_by( email=email, status=CommonConstant.default_status_true).first() if not user_info: resp['code'] = -1 resp['msg'] = "你好,未注册的邮箱,请找系统管理员先注册用户~~" return jsonify(resp) try: at_idx = email.index("@") smtp_obj = smtplib.SMTP_SSL("smtp." + email[(at_idx + 1):], 465) smtp_obj.set_debuglevel(1) smtp_obj.login(email, pwd) smtp_obj.close() except Exception: resp['code'] = -1 resp['msg'] = "登录失败,请核对邮箱和密码是否对应~~" return jsonify(resp) next_url = GlobalUrlService.buildHomeUrl("/") response = make_response( json.dumps({ 'code': 200, 'msg': '登录成功~~', 'data': { "next_url": next_url } })) response.set_cookie( CommonConstant.AUTH_COOKIE_NAME, '%s#%s' % (CurrentUserService.userAuthToken(user_info), user_info.id), 60 * 60 * 24 * 120) # 保存120天 return response
def server_index(): req = request.values kw = req.get("kw", "").strip() page = int(req.get("p", 1)) query = JobServer.query if kw: query = query.filter(JobServer.name.ilike('%{}%'.format(kw))) page_params = { "total": query.count(), "page_size": CommonConstant.PAGE_SIZE, "page": page, "display": CommonConstant.PAGE_DISPLAY } pages = UtilHelper.iPagination(page_params) offset = (page - 1) * CommonConstant.PAGE_SIZE limit = CommonConstant.PAGE_SIZE * page list = query.order_by(JobServer.id.desc())[offset:limit] data = [] server_env_map = CommonConstant.server_env_map if list: for item in list: tmp_data = ModelHelper.model2Dict(item) tmp_env_ids = (item.env).strip( CommonConstant.special_strike).split( CommonConstant.special_strike) tmp_env_list = [] for _env_id in tmp_env_ids: if int(_env_id) not in server_env_map.keys(): continue tmp_env_list.append(server_env_map.get(int(_env_id))) tmp_data['env'] = ",".join(tmp_env_list) data.append(tmp_data) sc = {'kw': kw} set_flag = RBACService.checkPageRelatePrivilege("set") ops_flag = RBACService.checkPageRelatePrivilege("ops") return UtilHelper.renderView( "home/job/server/index.html", { "list": data, "pages": pages, "server_env_map": CommonConstant.server_env_map, "sc": sc, "set_flag": set_flag, "ops_flag": ops_flag })
def tools_alert(): req = request.values page = int(req.get("p", 1)) date_from = req.get("date_from", DateHelper.getCurrentTime(fmt="%Y-%m-%d")) date_to = req.get("date_to", DateHelper.getCurrentTime(fmt="%Y-%m-%d")) status = int(req.get("status", CommonConstant.default_status_neg_99)) query = JobAlertList.query.filter( JobAlertList.created_time.between(date_from, date_to + " 23:59:59")) if status > CommonConstant.default_status_neg_99: query = query.filter_by(status=status) page_params = { "total": query.count(), "page_size": CommonConstant.PAGE_SIZE, "page": page, "display": CommonConstant.PAGE_DISPLAY, } pages = UtilHelper.iPagination(page_params) offset = (page - 1) * CommonConstant.PAGE_SIZE limit = CommonConstant.PAGE_SIZE * page list = query.order_by(JobAlertList.id.desc())[offset:limit] data = [] if list: job_ids = ModelHelper.getFieldList(list, "job_id") job_map = ModelHelper.getDictFilterField(JobList, select_field=JobList.id, id_list=job_ids.sort()) for item in list: tmp_data = ModelHelper.model2Dict(item) tmp_job_info = ModelHelper.model2Dict( job_map.get(tmp_data['job_id'])) tmp_data['status_desc'] = CommonConstant.common_status_map4[ tmp_data['status']] tmp_data['job_name'] = tmp_job_info['name'] data.append(tmp_data) sc = {'date_from': date_from, 'date_to': date_to, 'status': status} return UtilHelper.renderView( "home/job/tools/alert.html", { "list": data, "pages": pages, "sc": sc, "status_map": CommonConstant.common_status_map4, "current": "alert" })
def staff_index(): req = request.values kw = req.get("kw", "").strip() page = int(req.get("p", 1)) query = User.query if kw: query = query.filter( or_( User.name.ilike( '%{}%'.format(kw) ) ,User.email.ilike( '%{}%'.format(kw) ) )) page_params = { "total": query.count(), "page_size": CommonConstant.PAGE_SIZE, "page": page, "display": CommonConstant.PAGE_DISPLAY, } pages = UtilHelper.iPagination(page_params) offset = (page - 1) * CommonConstant.PAGE_SIZE limit = CommonConstant.PAGE_SIZE * page list = query.order_by( User.id.desc() )[offset:limit] dept_map = ModelHelper.getDictFilterField( Role ) sc = { 'kw': kw } set_flag = RBACService.checkPageRelatePrivilege("set") ops_flag = RBACService.checkPageRelatePrivilege("ops") return UtilHelper.renderView( "home/rbac/staff/index.html", { "list": list, "pages": pages, "dept_map" : dept_map, "sc": sc, "set_flag": set_flag, "ops_flag": ops_flag, })
def gii_index(): return UtilHelper.renderView("/home/tools/gii/index.html")
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({}, "权限分配成功~~")
def home_index(): return UtilHelper.renderView( "home/user/profile/index.html",{ "info":CurrentUserService.getInfo() } )
def tools_log(): status_map = CommonConstant.job_log_status_map req = request.values page = int(req.get("p", 1)) date_from = req.get("date_from", DateHelper.getCurrentTime(fmt="%Y-%m-%d")) date_to = req.get("date_to", DateHelper.getCurrentTime(fmt="%Y-%m-%d")) status = int(req.get("status", CommonConstant.default_status_neg_99)) job_id = int(req.get("job_id", CommonConstant.default_status_false)) query = JobRunLog.query.filter( JobRunLog.created_time.between(date_from, date_to + " 23:59:59")) if job_id: query = query.filter_by(job_id=job_id) if status > CommonConstant.default_status_neg_99: query = query.filter_by(status=status) page_params = { "total": query.count(), "page_size": CommonConstant.PAGE_SIZE, "page": page, "display": CommonConstant.PAGE_DISPLAY, } pages = UtilHelper.iPagination(page_params) offset = (page - 1) * CommonConstant.PAGE_SIZE limit = CommonConstant.PAGE_SIZE * page list = query.order_by(JobRunLog.id.desc())[offset:limit] data = [] if list: job_ids = ModelHelper.getFieldList(list, "job_id") job_map = ModelHelper.getDictFilterField(JobList, select_field=JobList.id, id_list=job_ids.sort()) for item in list: tmp_data = ModelHelper.model2Dict(item) tmp_job_info = ModelHelper.model2Dict( job_map.get(tmp_data['job_id'])) tmp_data['status_desc'] = status_map[tmp_data['status']] tmp_data['job_name'] = tmp_job_info['name'] tmp_data['duration'] = "" if DateHelper.getCurrentTime(date=tmp_data['end_time'] ) == CommonConstant.DEFAULT_DATETIME: tmp_data['end_time'] = "未知" tmp_data['duration'] = time.time() - time.mktime( tmp_data['start_time'].timetuple()) else: tmp_data['duration'] = (tmp_data['end_time'].timestamp() - tmp_data['start_time'].timestamp()) tmp_data['duration'] = DateHelper.formatBeautyTime( tmp_data['duration']) data.append(tmp_data) sc = { 'date_from': date_from, 'date_to': date_to, 'status': status, 'job_id': job_id } return UtilHelper.renderView( "home/job/tools/log.html", { "list": data, "pages": pages, "sc": sc, "status_map": status_map, "current": "log" })
def error_404(e): err_msg = getErrMsg() AppLogService.addErrLog(err_msg) return UtilHelper.renderView( "home/error/error.html",{ "status":404,"msg":"很抱歉!,您访问的页面不存在 ~~" } )
def error_502(e): err_msg = getErrMsg() AppLogService.addErrLog(err_msg) return UtilHelper.renderView("home/error/error.html")
def error_ban(): req = request.values msg = req.get("msg", "").strip() return UtilHelper.renderView("/home/error/ban.html", {"msg": msg})
def home_index(): return UtilHelper.renderView("home/index/index.html")
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})
def job_index(): req = request.values cate_id = int(req.get("cate_id", CommonConstant.default_status_false)) owner_uid = int(req.get("owner_uid", CurrentUserService.getUid())) env_id = int(req.get("env_id", CommonConstant.default_status_false)) server_id = int(req.get("server_id", CommonConstant.default_status_false)) status = int(req.get("status", CommonConstant.default_status_neg_99)) display_status = int( req.get("display_status", CommonConstant.default_status_neg_99)) job_type = int(req.get("job_type", CommonConstant.default_status_neg_99)) job_level = int(req.get("job_level", CommonConstant.default_status_neg_99)) kw = req.get("kw", "").strip() page = int(req.get("p", 1)) query = JobList.query if RBACService.checkDataPrivilege("all"): pass else: query = query.filter_by(owner_uid=CurrentUserService.getUid()) if cate_id: query = query.filter_by(cate_id=cate_id) if env_id: query = query.filter_by(env_id=env_id) if owner_uid: query = query.filter_by(owner_uid=owner_uid) if server_id: query = query.filter_by(server_id=server_id) if status > CommonConstant.default_status_neg_99: query = query.filter_by(status=status) if display_status > CommonConstant.default_status_neg_99: query = query.filter_by(is_del=display_status) if job_type > CommonConstant.default_status_neg_99: query = query.filter_by(job_type=job_type) if job_level > CommonConstant.default_status_neg_99: query = query.filter_by(job_level=job_level) if kw: if kw.isdigit(): query = query.filter_by(id=int(kw)) else: query = query.filter( or_(JobList.name.ilike('%{}%'.format(kw)), JobList.command.ilike('%{}%'.format(kw)))) page_params = { "total": query.count(), "page_size": CommonConstant.PAGE_SIZE, "page": page, "display": CommonConstant.PAGE_DISPLAY } pages = UtilHelper.iPagination(page_params) offset = (page - 1) * CommonConstant.PAGE_SIZE limit = CommonConstant.PAGE_SIZE * page list = query.order_by(JobList.id.desc())[offset:limit] data = [] server_map = ModelHelper.getDictFilterField(JobServer) cate_map = ModelHelper.getDictFilterField(JobCategory) staff_map = ModelHelper.getDictFilterField(User) server_env_map = CommonConstant.server_env_map run_status_map = CommonConstant.run_status_map job_type_map = CommonConstant.job_type_map job_level_map = CommonConstant.job_level_map if list: for item in list: tmp_data = ModelHelper.model2Dict(item) tmp_server_info = ModelHelper.model2Dict( server_map.get(tmp_data['server_id'])) tmp_cate_info = ModelHelper.model2Dict( cate_map.get(tmp_data['cate_id'])) tmp_owner_staff_info = ModelHelper.model2Dict( staff_map.get(tmp_data['owner_uid'])) tmp_relate_staff_info = ModelHelper.model2Dict( staff_map.get(tmp_data['relate_uid'])) tmp_data['next_run_time'] = DateHelper.getDateOnTimestamps( tmp_data['next_run_time'], '%Y-%m-%d %H:%M') tmp_data['env_name'] = server_env_map.get(tmp_data['env_id']) tmp_data['run_status_desc'] = run_status_map.get( tmp_data['run_status']) tmp_data['job_status_desc'] = job_status_map.get( tmp_data['status']) tmp_data['server_name'] = tmp_server_info.get("name") tmp_data['cate_name'] = tmp_cate_info.get("name", '') tmp_data['owner_name'] = tmp_owner_staff_info.get("name", '') tmp_data['relate_name'] = tmp_relate_staff_info.get("name", '') tmp_data['run_interval_desc'] = DateHelper.formatBeautyTime( tmp_data['run_interval'] * 60) data.append(tmp_data) sc = { 'kw': kw, 'cate_id': cate_id, 'owner_uid': owner_uid, 'env_id': env_id, 'server_id': server_id, 'status': status, 'display_status': display_status, 'job_type': job_type, 'job_level': job_level, } set_flag = RBACService.checkPageRelatePrivilege("set") ops_flag = RBACService.checkPageRelatePrivilege("ops") return UtilHelper.renderView( "home/job/index/index.html", { "list": data, "pages": pages, "job_status_map": job_status_map, "server_env_map": server_env_map, "server_map": server_map, "staff_map": staff_map, "cate_map": cate_map, "display_status_map": display_status_map, "job_type_map": job_type_map, "job_level_map": job_level_map, "sc": sc, "set_flag": set_flag, "ops_flag": ops_flag, })
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()
def error_500(e): err_msg = getErrMsg() AppLogService.addErrLog(err_msg) return UtilHelper.renderView("home/error/error.html",{ "status":500,"msg":"服务器错误" })