def role_menus_data(role_code): """ 角色菜单数据加载 :param role_code: 参数 :return: role_name, z_nodes """ try: z_nodes = [] # 1. 查出所有的菜单 menus = Menu.query.all() # 2. 查出拥有当前角色权限的所有角色菜单 role = Role.query.filter(Role.role_code == role_code).first() role_menus = [] for it in role.role_menus.all(): role_menus.append(it.menu_code) for menu in menus: parent_code = menu.parent_code menu_code = menu.menu_code dt = { "id": menu_code, "pId": parent_code, "name": menu.menu_name + " [" + menu_code + "]" } if parent_code: dt["open"] = True if menu_code in role_menus: dt["checked"] = True z_nodes.append(dt) return role.role_name, z_nodes except Exception as e: LOG.error("角色菜单数据加载 - 异常: {}".format(e)) raise HandleError(ResponseEnum.FAILURE.value, e)
def role_resources_data(role_code): """ 角色资源数据加载 :param role_code: 参数 :return: role_name, z_nodes """ try: z_nodes = [] # 1. 查出所有的资源 resources = Resource.query.all() # 2. 查出拥有当前角色权限的所有角色资源 role = Role.query.filter(Role.role_code == role_code).first() role_resources = [] for it in role.role_resources.all(): role_resources.append(it.resource_code) for resource in resources: parent_code = resource.parent_code resource_code = resource.resource_code dt = { "id": resource_code, "pId": parent_code, "name": resource.resource_name + " [" + resource_code + "]" } if parent_code: dt["open"] = True if resource_code in role_resources: dt["checked"] = True z_nodes.append(dt) return role.role_name, z_nodes except Exception as e: LOG.error("角色资源数据加载 - 异常: {}".format(e)) raise HandleError(ResponseEnum.FAILURE.value, e)
def resource_users_save(resource_code, user_ids): """ 资源用户保存 :param resource_code: 资源编号 :param user_ids: 用户ID List :return: SUCCESS """ try: UserResources.query.filter( UserResources.resource_code == resource_code).delete() user_resources = [] for user_id in user_ids: user_resource = UserResources( resource_code=resource_code, user_id=user_id, last_date=datetime.datetime.utcnow() + datetime.timedelta(hours=8)) user_resources.append(user_resource) if user_resources: db.session.add_all(user_resources) db.session.commit() return "SUCCESS" except Exception as e: LOG.error("资源用户保存 - 异常:{}".format(e)) raise HandleError(ResponseEnum.FAILURE.value, e)
def resource_roles_save(resource_code, role_codes): """ 资源角色保存 :param resource_code: 资源编号 :param role_codes: 角色编号List :return: SUCCESS """ try: RoleResources.query.filter( RoleResources.resource_code == resource_code).delete() role_resources = [] for role_code in role_codes: role_resource = RoleResources( resource_code=resource_code, role_code=role_code, last_date=datetime.datetime.utcnow() + datetime.timedelta(hours=8)) role_resources.append(role_resource) if role_resources: db.session.add_all(role_resources) db.session.commit() return "SUCCESS" except Exception as e: LOG.error("资源角色保存 - 异常:{}".format(e)) raise HandleError(ResponseEnum.FAILURE.value, e)
def menu_delete(ids): """ 菜单删除 :param ids: 删除ID :return: 响应结果 """ try: row = 0 if ids: file_name_list = ids.split(",") for item in file_name_list: rs = Menu.query.filter(Menu.id == item).first() if rs: is_exist_children = Menu.query.filter(Menu.parent_code == rs.menu_code).all() if len(is_exist_children) > 0: raise HandleError(ResponseEnum.FAILURE.value, "当前菜单[{}]存在子级菜单,请先删除所有子级菜单!" .format(rs.menu_code)) # 删除关联的表 RoleMenus.query.filter(RoleMenus.menu_code == rs.menu_code).delete() UserMenus.query.filter(UserMenus.menu_code == rs.menu_code).delete() db.session.delete(rs) db.session.commit() row += 1 return row except HandleError as e: LOG.error("删除菜单 - 异常:{}".format(e)) raise e except Exception as e: LOG.error("删除菜单 - 异常:{}".format(e)) raise HandleError(ResponseEnum.FAILURE.value, e)
def resource_list(param): """ 资源 - List filter_by用于查询简单的列名,不支持比较运算符; filter比filter_by的功能更强大,支持比较运算符,支持or_、in_等语法; :param param: 查询参数 :return: 响应结果 """ try: filters = ResourceQueryParam.param_list(param) resources = Resource.query.filter(*filters).order_by( Resource.resource_code.asc()).all() rows = [] for it in resources: it = it.__dict__ if "_sa_instance_state" in it: del it["_sa_instance_state"] # JS 前端处理 # if "last_date" in it: # it["last_date"] = it["last_date"].strftime("%Y-%m-%d %H:%M:%S") rows.append(it) return rows except Exception as e: LOG.error("资源List加载 - 异常: {}".format(e)) raise HandleError(ResponseEnum.FAILURE.value, e)
def user_menus_data(user_id): """ 用户菜单数据加载 :param user_id: 参数 :return: user.nickname, z_nodes """ try: z_nodes = [] # 1. 查出所有的菜单 menus = Menu.query.all() # 2. 查出拥有当前用户权限的所有用户菜单 user = User.query.filter(User.id == user_id).first() user_menus = [] for it in user.user_menus.all(): user_menus.append(it.menu_code) for menu in menus: parent_code = menu.parent_code menu_code = menu.menu_code dt = {"id": menu_code, "pId": parent_code, "name": menu.menu_name + " [" + menu_code + "]"} if parent_code: dt["open"] = True if menu_code in user_menus: dt["checked"] = True z_nodes.append(dt) return user.nickname, z_nodes except Exception as e: LOG.error("用户菜单数据加载 - 异常: {}".format(e)) raise HandleError(ResponseEnum.FAILURE.value, e)
def user_add(user): """ 用户新增 :param user: 数据 :return: 响应结果 """ try: rs = User.query.filter(User.nickname == user.nickname).first() if rs: raise VerifyError(ResponseEnum.PARAM_VALIDATE_ERROR.value, {"nickname": "[{}] 该昵称已被注册!".format(user.nickname)}) rs = User.query.filter(User.email == user.email).first() if rs: raise VerifyError(ResponseEnum.PARAM_VALIDATE_ERROR.value, {"email": "[{}] 该邮箱已被注册!".format(user.email)}) sc = Security(application["server"]["PRIVATE_KEY"]) random_password = generate_random_str(8) # 发送邮件 user_add_notification(user, random_password) user.password = sc.encrypt(random_password).replace("\n", "") user.is_available = 1 user.last_date = datetime.datetime.utcnow() + datetime.timedelta(hours=8) user.version = 1 db.session.add(user) db.session.commit() user.password = None return user except VerifyError as e: LOG.error("参数校验 - 异常:{}".format(e)) raise e except Exception as e: LOG.error("用户新增 - 异常:{}".format(e)) raise HandleError(ResponseEnum.FAILURE.value, e)
def edit_article(editor_txt, file_name): """ 文章编辑 :param editor_txt: 内容 :param file_name: 文件名 :return: 响应结果 """ try: md_posts_path_ = application["server"]["md_posts_path"] write_file(file_name, ".md", editor_txt, md_posts_path_) post = frontmatter.loads(editor_txt).metadata article = Article() article.title = post["title"] article.date = post["date"] article.categories = post["categories"] article.author = post["author"] article.tags = post["tags"] if mongo_db.db.articles.find_one({"_id": file_name}): mongo_db.db.articles.update({"_id": file_name}, {"$set": article.__dict__}) else: article.id = file_name article.file_name = file_name article.file_path = md_posts_path_ + SysEnum.SEPARATOR.value + file_name + ".md" mongo_db.db.articles.insert(article.__dict__) # 是否生成静态网页 generate_static_website(application, LOG) return "SUCCESS" except Exception as e: LOG.error("文章编辑 - 异常:{}".format(e)) raise HandleError(ResponseEnum.FAILURE.value, e)
def user_resources_data(user_id): """ 用户资源数据加载 :param user_id: 参数 :return: user.nickname, z_nodes """ try: z_nodes = [] # 1. 查出所有的资源 resources = Resource.query.all() # 2. 查出拥有当前用户权限的所有用户资源 user = User.query.filter(User.id == user_id).first() user_resources = [] for it in user.user_resources.all(): user_resources.append(it.resource_code) for resource in resources: parent_code = resource.parent_code resource_code = resource.resource_code dt = {"id": resource_code, "pId": parent_code, "name": resource.resource_name + " [" + resource_code + "]"} if parent_code: dt["open"] = True if resource_code in user_resources: dt["checked"] = True z_nodes.append(dt) return user.nickname, z_nodes except Exception as e: LOG.error("用户资源数据加载 - 异常: {}".format(e)) raise HandleError(ResponseEnum.FAILURE.value, e)
def user_delete(ids): """ 用户删除 :param ids: 删除ID :return: 响应结果 """ try: row = 0 if ids: file_name_list = ids.split(",") for item in file_name_list: rs = User.query.filter(User.id == item).first() if rs: # 删除关联的表 UserMenus.query.filter(UserMenus.user_id == rs.id).delete() UserResources.query.filter(UserResources.user_id == rs.id).delete() UserRoles.query.filter(UserRoles.user_id == rs.id).delete() db.session.delete(rs) db.session.commit() row += 1 return row except Exception as e: LOG.error("删除用户 - 异常:{}".format(e)) raise HandleError(ResponseEnum.FAILURE.value, e)
def sql_close(db_type, connect): """ Sql Close :param db_type: Databases type :param object connect: Mysql connect to close. """ if connect: connect.close() LOG.info("{} [数据库连接关闭] {} connect to close.".format( datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), db_type))
def menu_details(id): """ 菜单详情 :param id: 主键 :return: 响应结果 """ try: return Menu.query.filter(Menu.id == id).first() except Exception as e: LOG.error("查看菜单详情 - 异常:{}".format(e)) raise HandleError(ResponseEnum.FAILURE.value, e)
def read_about_me(): """ 读取关于我Md文件 :return: 内容 """ try: return read_file(application["server"]["md_about_path"] + SysEnum.SEPARATOR.value + "index.md") except Exception as e: LOG.error("读取关于我Md文件 - 异常:{}".format(e)) raise HandleError(ResponseEnum.FAILURE.value, e)
def resource_details(id): """ 资源详情 :param id: 主键 :return: 响应结果 """ try: return Resource.query.filter(Resource.id == id).first() except Exception as e: LOG.error("查看资源详情 - 异常:{}".format(e)) raise HandleError(ResponseEnum.FAILURE.value, e)
def read_article(file_name): """ 读取文章Md文件 :param file_name: 文件名 :return: 内容 """ try: return read_file(application["server"]["md_posts_path"] + SysEnum.SEPARATOR.value + file_name + ".md") except Exception as e: LOG.error("读取文章Md文件 - 异常:{}".format(e)) raise HandleError(ResponseEnum.FAILURE.value, e)
def user_details(id): """ 用户详情 :param id: 主键 :return: 响应结果 """ try: user = User.query.filter(User.id == id).first() user.password = None return user except Exception as e: LOG.error("查看用户详情 - 异常:{}".format(e)) raise HandleError(ResponseEnum.FAILURE.value, e)
def read_yml(path=None): """ This's read yml config file. :param path: Yml config file path. :type path: str or None :return: Yml object :rtype: object """ if path: with open(path, "r", encoding="UTF-8") as yml: return yaml.load(yml.read(), yaml.Loader) else: LOG.error("yml config file path is None ...") return None
def read_json(path=None): """ This's read JSON config file. :param path: JSON config file path. :type path: str or None :return: JSON object :rtype: object """ if path: with open(path, "r", encoding="UTF-8") as js: return json.loads(js.read()) else: LOG.error("JSON config file path is None ...") return None
def modify_about_me(editor_txt): """ 修改关于我Md信息 :param editor_txt: 内容 """ try: write_file("index", ".md", editor_txt, application["server"]["md_about_path"]) # 是否生成静态网页 generate_static_website(application, LOG) return "SUCCESS" except Exception as e: LOG.error("修改关于我Md信息 - 异常:{}".format(e)) raise HandleError(ResponseEnum.FAILURE.value, e)
def menu_add(menu): """ 菜单新增 :param menu: 数据 :return: 响应结果 """ try: menu.last_date = datetime.datetime.utcnow() + datetime.timedelta(hours=8) menu.version = 1 db.session.add(menu) db.session.commit() return menu except Exception as e: LOG.error("菜单新增 - 异常:{}".format(e)) raise HandleError(ResponseEnum.FAILURE.value, e)
def resource_add(resource): """ 资源新增 :param resource: 数据 :return: 响应结果 """ try: resource.last_date = datetime.datetime.utcnow() + datetime.timedelta( hours=8) resource.version = 1 db.session.add(resource) db.session.commit() return resource except Exception as e: LOG.error("资源新增 - 异常:{}".format(e)) raise HandleError(ResponseEnum.FAILURE.value, e)
def role_users_data(role_code): """ 角色用户数据加载 :param role_code: 参数 :return: role_name, users, role_users """ try: # 1. 查出所有的用户 users = User.query.all() # 2. 查出拥有当前角色权限的所有角色用户 role = Role.query.filter(Role.role_code == role_code).first() role_users = [] for it in role.role_users.all(): role_users.append({"user_id": it.id}) return role.role_name, users, role_users except Exception as e: LOG.error("角色用户数据加载 - 异常: {}".format(e)) raise HandleError(ResponseEnum.FAILURE.value, e)
def user_roles_data(user_id): """ 用户角色数据加载 :param user_id: 参数 :return: user.nickname, roles, user_roles """ try: # 1. 查出所有的角色 roles = Role.query.all() # 2. 查出拥有当前用户权限的所有用户角色 user = User.query.filter(User.id == user_id).first() user_roles = [] for it in user.user_roles.all(): user_roles.append({"role_code": it.role_code}) return user.nickname, roles, user_roles except Exception as e: LOG.error("用户角色数据加载 - 异常: {}".format(e)) raise HandleError(ResponseEnum.FAILURE.value, e)
def menu_roles_data(menu_code): """ 菜单角色数据加载 :param menu_code: 参数 :return: menu_name, roles, menu_roles """ try: # 1. 查出所有的角色 roles = Role.query.all() # 2. 查出拥有当前菜单权限的所有菜单角色 menu = Menu.query.filter(Menu.menu_code == menu_code).first() menu_roles = [] for it in menu.menu_roles.all(): menu_roles.append({"role_code": it.role_code}) return menu.menu_name, roles, menu_roles except Exception as e: LOG.error("菜单角色数据加载 - 异常: {}".format(e)) raise HandleError(ResponseEnum.FAILURE.value, e)
def menu_users_data(menu_code): """ 菜单用户数据加载 :param menu_code: 参数 :return: menu_name, users, menu_users """ try: # 1. 查出所有的用户 users = User.query.all() # 2. 查出拥有当前菜单权限的所有菜单用户 menu = Menu.query.filter(Menu.menu_code == menu_code).first() menu_users = [] for it in menu.menu_users.all(): menu_users.append({"user_id": it.id}) return menu.menu_name, users, menu_users except Exception as e: LOG.error("菜单用户数据加载 - 异常: {}".format(e)) raise HandleError(ResponseEnum.FAILURE.value, e)
def resource_roles_data(resource_code): """ 资源角色数据加载 :param resource_code: 参数 :return: resource_name, roles, resource_roles """ try: # 1. 查出所有的角色 roles = Role.query.all() # 2. 查出拥有当前资源权限的所有资源角色 resource = Resource.query.filter( Resource.resource_code == resource_code).first() resource_roles = [] for it in resource.resource_roles.all(): resource_roles.append({"role_code": it.role_code}) return resource.resource_name, roles, resource_roles except Exception as e: LOG.error("资源角色数据加载 - 异常: {}".format(e)) raise HandleError(ResponseEnum.FAILURE.value, e)
def user_is_disable(user_id, is_available): """ 是否禁用 :param user_id: 用户ID :param is_available: 禁用状态 :return: 响应结果 """ try: # 根据ID查询 original_user = User.query.filter(User.id == user_id).first() original_user.id = user_id original_user.is_available = is_available original_user.last_date = datetime.datetime.utcnow() + datetime.timedelta(hours=8) original_user.version += 1 db.session.commit() original_user.password = None return original_user except Exception as e: LOG.error("是否禁用处理 - 异常:{}".format(e)) raise HandleError(ResponseEnum.FAILURE.value, e)
def resource_page(param): """ 资源列表加载 :param param: 查询参数 :return: 响应结果 """ try: filters = ResourceQueryParam.param_list(param) resources = Resource.query.filter(*filters).order_by(text(param.sort_name + " " + param.sort_order)) \ .paginate(param.page_number, param.page_size, False) rows = [] for it in resources.items: it = it.__dict__ if "_sa_instance_state" in it: del it["_sa_instance_state"] rows.append(it) return PageInfo(total=resources.total, rows=rows) except Exception as e: LOG.error("资源表加载 - 异常: {}".format(e)) raise HandleError(ResponseEnum.FAILURE.value, e)
def role_edit(role): """ 角色编辑 :param role: 数据 :return: 响应结果 """ try: # 根据ID查询 original_role = Role.query.filter(Role.id == role.id).first() original_role.role_code = role.role_code original_role.role_name = role.role_name original_role.describe = role.describe original_role.last_date = datetime.datetime.utcnow( ) + datetime.timedelta(hours=8) original_role.version += 1 db.session.commit() return original_role except Exception as e: LOG.error("角色编辑 - 异常:{}".format(e)) raise HandleError(ResponseEnum.FAILURE.value, e)