1010from sqlalchemy import and_ , select
1111from starlette .responses import JSONResponse
1212
13- from apps .chat .curd .chat import list_chats , get_chat_with_records , create_chat , rename_chat , \
13+ from apps .chat .curd .chat import delete_chat_with_user , get_chart_data_with_user , get_chat_predict_data_with_user , list_chats , get_chat_with_records , create_chat , rename_chat , \
1414 delete_chat , get_chat_chart_data , get_chat_predict_data , get_chat_with_records_with_data , get_chat_record_by_id , \
15- format_json_data , format_json_list_data , get_chart_config , list_recent_questions ,get_chat as get_chat_exec
15+ format_json_data , format_json_list_data , get_chart_config , list_recent_questions ,get_chat as get_chat_exec , rename_chat_with_user
1616from apps .chat .models .chat_model import CreateChat , ChatRecord , RenameChat , ChatQuestion , AxisObj , QuickCommand , \
1717 ChatInfo , Chat , ChatFinishStep
1818from apps .chat .task .llm import LLMService
@@ -52,7 +52,7 @@ def inner():
5252 return await asyncio .to_thread (inner )
5353
5454
55- @router .get ("/record/{chat_record_id}/data" , summary = f"{ PLACEHOLDER_PREFIX } get_chart_data" )
55+ """ @router.get("/record/{chat_record_id}/data", summary=f"{PLACEHOLDER_PREFIX}get_chart_data")
5656async def chat_record_data(session: SessionDep, chat_record_id: int):
5757 def inner():
5858 data = get_chat_chart_data(chat_record_id=chat_record_id, session=session)
@@ -67,10 +67,27 @@ def inner():
6767 data = get_chat_predict_data(chat_record_id=chat_record_id, session=session)
6868 return format_json_list_data(data)
6969
70+ return await asyncio.to_thread(inner) """
71+
72+ @router .get ("/record/{chat_record_id}/data" , summary = f"{ PLACEHOLDER_PREFIX } get_chart_data" )
73+ async def chat_record_data (session : SessionDep , current_user : CurrentUser , chat_record_id : int ):
74+ def inner ():
75+ data = get_chart_data_with_user (chat_record_id = chat_record_id , session = session , current_user = current_user )
76+ return format_json_data (data )
77+
7078 return await asyncio .to_thread (inner )
7179
7280
73- @router .post ("/rename" , response_model = str , summary = f"{ PLACEHOLDER_PREFIX } rename_chat" )
81+ @router .get ("/record/{chat_record_id}/predict_data" , summary = f"{ PLACEHOLDER_PREFIX } get_chart_predict_data" )
82+ async def chat_predict_data (session : SessionDep , current_user : CurrentUser , chat_record_id : int ):
83+ def inner ():
84+ data = get_chat_predict_data_with_user (chat_record_id = chat_record_id , session = session , current_user = current_user )
85+ return format_json_list_data (data )
86+
87+ return await asyncio .to_thread (inner )
88+
89+
90+ """ @router.post("/rename", response_model=str, summary=f"{PLACEHOLDER_PREFIX}rename_chat")
7491@system_log(LogConfig(
7592 operation_type=OperationType.UPDATE,
7693 module=OperationModules.CHAT,
@@ -83,10 +100,24 @@ async def rename(session: SessionDep, chat: RenameChat):
83100 raise HTTPException(
84101 status_code=500,
85102 detail=str(e)
86- )
103+ ) """
87104
105+ @router .post ("/rename" , response_model = str , summary = f"{ PLACEHOLDER_PREFIX } rename_chat" )
106+ @system_log (LogConfig (
107+ operation_type = OperationType .UPDATE ,
108+ module = OperationModules .CHAT ,
109+ resource_id_expr = "chat.id"
110+ ))
111+ async def rename (session : SessionDep , current_user : CurrentUser , chat : RenameChat ):
112+ try :
113+ return rename_chat_with_user (session = session , current_user = current_user , rename_object = chat )
114+ except Exception as e :
115+ raise HTTPException (
116+ status_code = 500 ,
117+ detail = str (e )
118+ )
88119
89- @router .delete ("/{chart_id}/{brief}" , response_model = str , summary = f"{ PLACEHOLDER_PREFIX } delete_chat" )
120+ """ @router.delete("/{chart_id}/{brief}", response_model=str, summary=f"{PLACEHOLDER_PREFIX}delete_chat")
90121@system_log(LogConfig(
91122 operation_type=OperationType.DELETE,
92123 module=OperationModules.CHAT,
@@ -100,8 +131,23 @@ async def delete(session: SessionDep, chart_id: int, brief: str):
100131 raise HTTPException(
101132 status_code=500,
102133 detail=str(e)
103- )
134+ ) """
104135
136+ @router .delete ("/{chart_id}/{brief}" , response_model = str , summary = f"{ PLACEHOLDER_PREFIX } delete_chat" )
137+ @system_log (LogConfig (
138+ operation_type = OperationType .DELETE ,
139+ module = OperationModules .CHAT ,
140+ resource_id_expr = "chart_id" ,
141+ remark_expr = "brief"
142+ ))
143+ async def delete (session : SessionDep , current_user : CurrentUser , chart_id : int , brief : str ):
144+ try :
145+ return delete_chat_with_user (session = session , current_user = current_user , chart_id = chart_id )
146+ except Exception as e :
147+ raise HTTPException (
148+ status_code = 500 ,
149+ detail = str (e )
150+ )
105151
106152@router .post ("/start" , response_model = ChatInfo , summary = f"{ PLACEHOLDER_PREFIX } start_chat" )
107153@require_permissions (permission = SqlbotPermission (type = 'ds' , keyExpression = "create_chat_obj.datasource" ))
@@ -391,14 +437,18 @@ def _err(_e: Exception):
391437
392438
393439@router .get ("/record/{chat_record_id}/excel/export" , summary = f"{ PLACEHOLDER_PREFIX } export_chart_data" )
394- async def export_excel (session : SessionDep , chat_record_id : int , trans : Trans ):
440+ async def export_excel (session : SessionDep , current_user : CurrentUser , chat_record_id : int , trans : Trans ):
395441 chat_record = session .get (ChatRecord , chat_record_id )
396442 if not chat_record :
397443 raise HTTPException (
398444 status_code = 500 ,
399445 detail = f"ChatRecord with id { chat_record_id } not found"
400446 )
401-
447+ if chat_record .create_by != current_user .id :
448+ raise HTTPException (
449+ status_code = 500 ,
450+ detail = f"ChatRecord with id { chat_record_id } not Owned by the current user"
451+ )
402452 is_predict_data = chat_record .predict_record_id is not None
403453
404454 _origin_data = format_json_data (get_chat_chart_data (chat_record_id = chat_record_id , session = session ))
0 commit comments