File tree 4 files changed +13
-22
lines changed
4 files changed +13
-22
lines changed Original file line number Diff line number Diff line change @@ -42,10 +42,7 @@ async def get_models() -> list[str]:
42
42
async def get_columns (model : str ) -> list [str ]:
43
43
if model not in settings .DATA_PERMISSION_MODELS :
44
44
raise errors .NotFoundError (msg = '数据模型不存在' )
45
- try :
46
- model_ins = dynamic_import_data_model (settings .DATA_PERMISSION_MODELS [model ])
47
- except (ImportError , AttributeError ):
48
- raise errors .ServerError (msg = f'数据模型 { model } 动态导入失败,请联系系统超级管理员' )
45
+ model_ins = dynamic_import_data_model (settings .DATA_PERMISSION_MODELS [model ])
49
46
model_columns = [
50
47
key for key in model_ins .__table__ .columns .keys () if key not in settings .DATA_PERMISSION_COLUMN_EXCLUDE
51
48
]
Original file line number Diff line number Diff line change @@ -60,10 +60,7 @@ def filter_data_permission(request: Request) -> ColumnElement[bool]:
60
60
rule_model = rule .model
61
61
if rule_model not in settings .DATA_PERMISSION_MODELS :
62
62
raise errors .NotFoundError (msg = '数据规则模型不存在' )
63
- try :
64
- model_ins = dynamic_import_data_model (settings .DATA_PERMISSION_MODELS [rule_model ])
65
- except (ImportError , AttributeError ):
66
- raise errors .ServerError (msg = f'数据模型 { rule_model } 动态导入失败,请联系系统超级管理员' )
63
+ model_ins = dynamic_import_data_model (settings .DATA_PERMISSION_MODELS [rule_model ])
67
64
model_columns = [
68
65
key for key in model_ins .__table__ .columns .keys () if key not in settings .DATA_PERMISSION_COLUMN_EXCLUDE
69
66
]
Original file line number Diff line number Diff line change @@ -169,7 +169,7 @@ class Settings(BaseSettings):
169
169
DATA_PERMISSION_MODELS : dict [
170
170
str , str
171
171
] = { # 允许进行数据过滤的 SQLA 模型,它必须以模块字符串的方式定义(它应该只用于前台数据,这里只是为了演示)
172
- 'Api' : 'backend.app.admin .model.Api' ,
172
+ 'Api' : 'backend.plugin.casbin .model.Api' ,
173
173
}
174
174
DATA_PERMISSION_COLUMN_EXCLUDE : list [str ] = [ # 排除允许进行数据过滤的 SQLA 模型列
175
175
'id' ,
Original file line number Diff line number Diff line change 5
5
from functools import lru_cache
6
6
from typing import Any
7
7
8
-
9
- def module_parse (module_path : str ) -> tuple :
10
- """
11
- Parse a python module string into a python module and class/function.
12
-
13
- :param module_path:
14
- :return:
15
- """
16
- module_path , class_or_func = module_path .rsplit ('.' , 1 )
17
- return module_path , class_or_func
8
+ from backend .common .exception import errors
9
+ from backend .common .log import log
18
10
19
11
20
12
@lru_cache (maxsize = 512 )
@@ -35,7 +27,12 @@ def dynamic_import_data_model(module_path: str) -> Any:
35
27
:param module_path:
36
28
:return:
37
29
"""
38
- module_path , class_or_func = module_parse (module_path )
39
- module = import_module_cached (module_path )
40
- ins = getattr (module , class_or_func )
30
+ module_path , class_or_func = module_path .rsplit ('.' , 1 )
31
+
32
+ try :
33
+ module = import_module_cached (module_path )
34
+ ins = getattr (module , class_or_func )
35
+ except (ImportError , AttributeError ) as e :
36
+ log .error (e )
37
+ raise errors .ServerError (msg = '数据模型列动态解析失败,请联系系统超级管理员' )
41
38
return ins
You can’t perform that action at this time.
0 commit comments