File tree Expand file tree Collapse file tree 4 files changed +13
-22
lines changed Expand file tree Collapse file tree 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]:
4242    async  def  get_columns (model : str ) ->  list [str ]:
4343        if  model  not  in settings .DATA_PERMISSION_MODELS :
4444            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 ])
4946        model_columns  =  [
5047            key  for  key  in  model_ins .__table__ .columns .keys () if  key  not  in settings .DATA_PERMISSION_COLUMN_EXCLUDE 
5148        ]
Original file line number Diff line number Diff line change @@ -60,10 +60,7 @@ def filter_data_permission(request: Request) -> ColumnElement[bool]:
6060        rule_model  =  rule .model 
6161        if  rule_model  not  in settings .DATA_PERMISSION_MODELS :
6262            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 ])
6764        model_columns  =  [
6865            key  for  key  in  model_ins .__table__ .columns .keys () if  key  not  in settings .DATA_PERMISSION_COLUMN_EXCLUDE 
6966        ]
Original file line number Diff line number Diff line change @@ -169,7 +169,7 @@ class Settings(BaseSettings):
169169    DATA_PERMISSION_MODELS : dict [
170170        str , str 
171171    ] =  {  # 允许进行数据过滤的 SQLA 模型,它必须以模块字符串的方式定义(它应该只用于前台数据,这里只是为了演示) 
172-         'Api' : 'backend.app.admin .model.Api' ,
172+         'Api' : 'backend.plugin.casbin .model.Api' ,
173173    }
174174    DATA_PERMISSION_COLUMN_EXCLUDE : list [str ] =  [  # 排除允许进行数据过滤的 SQLA 模型列 
175175        'id' ,
Original file line number Diff line number Diff line change 55from  functools  import  lru_cache 
66from  typing  import  Any 
77
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 
1810
1911
2012@lru_cache (maxsize = 512 ) 
@@ -35,7 +27,12 @@ def dynamic_import_data_model(module_path: str) -> Any:
3527    :param module_path: 
3628    :return: 
3729    """ 
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 = '数据模型列动态解析失败,请联系系统超级管理员' )
4138    return  ins 
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments