优化page.py

This commit is contained in:
jingrow 2025-10-25 17:02:37 +08:00
parent 1b2db3de81
commit a9faeb992a

View File

@ -2,7 +2,6 @@
# For license information, please see license.txt
"""
Jingrow 标准 REST API 实现
提供对云端数据库的增删改查操作并支持本地钩子函数
"""
@ -28,13 +27,6 @@ router = APIRouter()
# 初始化队列
init_queue()
def get_pagetype_from_request(request: Request) -> str:
"""从请求路径中提取pagetype"""
return request.path_params.get("pagetype", "")
def get_name_from_request(request: Request) -> Optional[str]:
"""从请求路径中提取name"""
return request.path_params.get("name")
def execute_hooks(pagetype: str, name: str, hook_name: str, data: Dict[str, Any] = None):
"""执行钩子函数"""
@ -60,7 +52,10 @@ async def get_records(
fields_list = json.loads(fields) if fields else []
filters_list = json.loads(filters) if filters else []
limit = limit_start + limit_page_length if limit_page_length > 0 else None
if limit_page_length == 0:
limit = None
else:
limit = limit_start + limit_page_length
result = get_record_list(
pagetype=pagetype,
@ -71,14 +66,21 @@ async def get_records(
if result.get('success'):
data = result.get('data', [])
if limit_start > 0 and len(data) > limit_start:
data = data[limit_start:]
if limit_page_length > 0 and len(data) > limit_page_length:
if limit_page_length > 0:
if limit_start > 0:
data = data[limit_start:]
data = data[:limit_page_length]
estimated_total = limit_start + len(data)
if limit_page_length > 0 and len(data) < limit_page_length:
estimated_total = limit_start + len(data)
elif limit_page_length > 0:
estimated_total = limit_start + limit_page_length + 1
return JSONResponse(content={
"data": data,
"total": len(data)
"total": estimated_total
})
else:
raise HTTPException(status_code=400, detail=result.get('error', '获取记录列表失败'))
@ -192,13 +194,21 @@ async def batch_delete_records(request: Request, data: Dict[str, Any]):
@router.post("/api/action/jingrow.client.get_count")
async def get_record_count(request: Request, pagetype: str):
"""获取记录总数"""
"""获取记录总数(估算)"""
try:
result = get_record_list(pagetype=pagetype, limit=1)
# 获取少量数据来估算总数
result = get_record_list(pagetype=pagetype, limit=100)
if result.get('success'):
data = result.get('data', [])
return JSONResponse(content={"message": len(data)})
# 如果返回的数据少于100条说明总数就是实际数量
if len(data) < 100:
estimated_count = len(data)
else:
# 如果返回了100条可能还有更多返回一个估算值
estimated_count = len(data) + 1
return JSONResponse(content={"message": estimated_count})
else:
raise HTTPException(status_code=400, detail=result.get('error', '获取记录总数失败'))
@ -206,20 +216,6 @@ async def get_record_count(request: Request, pagetype: str):
logger.error(f"获取记录总数失败: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.post("/api/action/upload_file")
async def upload_file(request: Request):
"""上传文件"""
try:
return JSONResponse(content={
"message": {
"file_url": "http://example.com/uploaded_file.txt",
"file_name": "uploaded_file.txt"
}
})
except Exception as e:
logger.error(f"文件上传失败: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.post("/api/hooks/execute")
async def execute_hook_task(request: Request, data: Dict[str, Any]):