Files
Wu Clan bd45022a59 Update role-based data permissions (#465)
* Add department data operation permissions

* Update sys models

* Add role department many-to-many relationship

* Format RBAC code

* update codes

* add update role depts api

* add comments

* Update the implementation

* update dept arg

* debug front

* Update the model file naming

* Refactor the data perms

* Add data permission rule

* Optimize the details

* Implement data filtering

* finish
2024-11-23 21:23:52 +08:00

48 lines
1.1 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import importlib
from functools import lru_cache
from typing import Any
from backend.common.exception import errors
def parse_module_str(module_path: str) -> tuple:
"""
Parse a module string into a Python module and class/function.
:param module_path:
:return:
"""
module_name, class_or_func = module_path.rsplit('.', 1)
return module_name, class_or_func
@lru_cache(maxsize=512)
def import_module_cached(module_name: str) -> Any:
"""
缓存导入模块
:param module_name:
:return:
"""
return importlib.import_module(module_name)
def dynamic_import(module_path: str) -> Any:
"""
动态导入
:param module_path:
:return:
"""
module_name, obj_name = parse_module_str(module_path)
try:
module = import_module_cached(module_name)
class_or_func = getattr(module, obj_name)
return class_or_func
except (ImportError, AttributeError):
raise errors.ServerError(msg=f'数据模型 {module_name} 动态导入失败,请联系系统超级管理员')