mirror of
https://github.com/fastapi-practices/fastapi-best-architecture.git
synced 2026-09-21 21:15:13 +00:00
Optimize data permission logic and usage (#947)
* Optimize data permission rules and usage * Update get data permission models * Update date permission filter * Optimize the target model logic * Upgrade dependencies to use latest features * Remove model warnings * Fix the latest feature issues * Fix the sqlalchemy Table class import * Fix the sqlalchemy Table class compatibility
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
import importlib
|
||||
import inspect
|
||||
import os.path
|
||||
|
||||
from functools import lru_cache
|
||||
from typing import Any, TypeVar
|
||||
|
||||
import sqlalchemy as sa
|
||||
|
||||
from backend.common.exception import errors
|
||||
from backend.common.log import log
|
||||
|
||||
@@ -37,7 +40,7 @@ def dynamic_import_data_model(module_path: str) -> type[T]:
|
||||
raise errors.ServerError(msg='数据模型列动态解析失败,请联系系统超级管理员')
|
||||
|
||||
|
||||
def get_model_objects(module_path: str) -> list[type] | None:
|
||||
def get_model_objects(module_path: str) -> list[object] | None:
|
||||
"""
|
||||
获取模型对象
|
||||
|
||||
@@ -47,15 +50,43 @@ def get_model_objects(module_path: str) -> list[type] | None:
|
||||
try:
|
||||
module = import_module_cached(module_path)
|
||||
except ModuleNotFoundError:
|
||||
log.warning(f'模块 {module_path} 中不包含模型对象')
|
||||
return None
|
||||
except Exception:
|
||||
raise
|
||||
except Exception as e:
|
||||
raise e from None
|
||||
|
||||
classes = []
|
||||
|
||||
for _name, obj in inspect.getmembers(module):
|
||||
if inspect.isclass(obj) and module_path in obj.__module__:
|
||||
if (inspect.isclass(obj) and module_path in obj.__module__) or (
|
||||
isinstance(obj, sa.Table) and obj.metadata is not None
|
||||
):
|
||||
classes.append(obj)
|
||||
|
||||
return classes
|
||||
|
||||
|
||||
def get_app_models() -> list[object]:
|
||||
"""获取 app 所有模型类"""
|
||||
from backend.core.path_conf import BASE_PATH
|
||||
|
||||
app_path = BASE_PATH / 'app'
|
||||
list_dirs = os.listdir(app_path)
|
||||
|
||||
apps = [d for d in list_dirs if os.path.isdir(os.path.join(app_path, d)) and d != '__pycache__']
|
||||
|
||||
objs = []
|
||||
for app in apps:
|
||||
module_path = f'backend.app.{app}.model'
|
||||
model_objs = get_model_objects(module_path)
|
||||
if model_objs:
|
||||
objs.extend(model_objs)
|
||||
|
||||
return objs
|
||||
|
||||
|
||||
@lru_cache
|
||||
def get_all_models() -> list[object]:
|
||||
"""获取所有模型类"""
|
||||
from backend.plugin.tools import get_plugin_models
|
||||
|
||||
return get_app_models() + get_plugin_models()
|
||||
|
||||
Reference in New Issue
Block a user