1- from fastapi import FastAPI , APIRouter , Depends , HTTPException , Query , Path
1+ from fastapi import FastAPI , APIRouter , Depends , HTTPException , Query , Path , Header
22from sqlalchemy .orm import Session
33from typing import List , Dict , Any , Optional , Annotated
4+ import base64
5+ from pathlib import Path
6+ from data_celery .utils import get_project_root
47
58from data_server .database .session import get_sync_session
69
2124
2225
2326
24- @router .post ("/ " , summary = "create_operator" )
27+ @router .post ("" , summary = "create_operator" )
2528def create_operator_api (
2629 operator_data : OperatorCreateRequest ,
2730 db : Session = Depends (get_sync_session )
@@ -36,7 +39,7 @@ def create_operator_api(
3639 db .close ()
3740
3841
39- @router .get ("/ " , summary = "GET_LIST_OF_OPERATORS" )
42+ @router .get ("" , summary = "GET_LIST_OF_OPERATORS" )
4043def read_operators_api (
4144 skip : int = 0 ,
4245 limit : int = 100 ,
@@ -45,7 +48,27 @@ def read_operators_api(
4548
4649 try :
4750 operators = get_operators (db , skip , limit )
48- return response_success (data = operators , msg = "获取算子列表成功" )
51+ operators_data = []
52+ project_root = get_project_root ()
53+ for op in operators :
54+ op_dict = op .__dict__
55+ pic_base64 = None
56+ mime_type = None
57+ if op .icon :
58+ try :
59+ filename = Path (op .icon ).name
60+ image_path = project_root / 'attach' / 'operator' / filename
61+ if image_path .exists () and image_path .is_file ():
62+ with open (image_path , "rb" ) as image_file :
63+ encoded_string = base64 .b64encode (image_file .read ()).decode ("utf-8" )
64+ pic_base64 = encoded_string
65+ except Exception :
66+ # Ignore errors for individual images
67+ pass
68+ op_dict ['pic_base64' ] = pic_base64
69+ operators_data .append (op_dict )
70+
71+ return response_success (data = operators_data , msg = "获取算子列表成功" )
4972 except Exception as e :
5073 return response_fail (msg = f"获取算子列表失败: { str (e )} " )
5174 finally :
@@ -126,7 +149,7 @@ def get_operator_config_select_option_by_id_api(
126149 db .close ()
127150
128151
129- @router .post ("/config_select_options/ " , summary = "添加下拉框选项" )
152+ @router .post ("/config_select_options" , summary = "添加下拉框选项" )
130153def create_operator_config_select_option_api (
131154 option : OperatorConfigSelectOptionsCreate ,
132155 db : Session = Depends (get_sync_session )
@@ -148,14 +171,30 @@ def get_operators_grouped_by_type_api(
148171
149172 try :
150173 grouped_operators = get_operators_grouped_by_type (db )
174+ project_root = get_project_root ()
175+ for group in grouped_operators :
176+ for op in group ['list' ]:
177+ pic_base64 = None
178+ icon = op .get ('icon' )
179+ if icon :
180+ try :
181+ filename = Path (icon ).name
182+ image_path = project_root / 'attach' / 'operator' / filename
183+ if image_path .exists () and image_path .is_file ():
184+ with open (image_path , "rb" ) as image_file :
185+ encoded_string = base64 .b64encode (image_file .read ()).decode ("utf-8" )
186+ pic_base64 = encoded_string
187+ except Exception :
188+ pass
189+ op ['pic_base64' ] = pic_base64
151190 return response_success (data = grouped_operators , msg = "获取分组算子列表成功" )
152191 except Exception as e :
153192 return response_fail (msg = f"获取分组算子列表失败: { str (e )} " )
154193 finally :
155194 db .close ()
156195
157196# find_operator_by_uuid_orgs
158- @router .get ("/types/grouped-by-condition/ " , summary = "根据算子分类和权限返回算子数据" )
197+ @router .get ("/types/grouped-by-condition" , summary = "根据算子分类和权限返回算子数据" )
159198def get_operators_grouped_by_condition_api (
160199 payload : Dict = Depends (get_validated_token_payload ),
161200 db : Session = Depends (get_sync_session ),
@@ -174,8 +213,28 @@ def get_operators_grouped_by_condition_api(
174213 return response_fail ("Token中缺少用户信息 (uuid)" )
175214
176215 grouped_operators = get_operators_grouped_by_condition (db , user_id , paths )
216+ project_root = get_project_root ()
217+ for group in grouped_operators :
218+ for op in group ['list' ]:
219+ pic_base64 = None
220+ icon = op .get ('icon' )
221+ if icon :
222+ try :
223+ filename = Path (icon ).name
224+ image_path = project_root / 'attach' / 'operator' / filename
225+ if image_path .exists () and image_path .is_file ():
226+ with open (image_path , "rb" ) as image_file :
227+ encoded_string = base64 .b64encode (image_file .read ()).decode ("utf-8" )
228+ pic_base64 = encoded_string
229+ except Exception :
230+ pass
231+ op ['pic_base64' ] = pic_base64
177232 return response_success (data = grouped_operators , msg = "获取分组算子列表成功" )
178233 except Exception as e :
179234 return response_fail (msg = f"获取分组算子列表失败: { str (e )} " )
180235 finally :
181236 db .close ()
237+
238+ @router .get ("/isAdmin/torf" )
239+ def get_isAdmin_true_or_false (isadmin : str = Header (..., alias = "isadmin" , description = "是否管理员" )):
240+ return response_success (data = {"isadmin" :isadmin })
0 commit comments