Spaces:
Runtime error
Runtime error
Fix datetime handling in trade.py to handle both datetime objects and strings
Browse files- apps/api/main_hf.py +4 -4
- apps/api/routers/trade.py +40 -1
apps/api/main_hf.py
CHANGED
|
@@ -15,8 +15,8 @@ from fastapi.middleware.cors import CORSMiddleware
|
|
| 15 |
from fastapi.staticfiles import StaticFiles
|
| 16 |
from fastapi.responses import FileResponse
|
| 17 |
|
| 18 |
-
# 只导入
|
| 19 |
-
from apps.api.routers import
|
| 20 |
|
| 21 |
# 获取项目根目录
|
| 22 |
BASE_DIR = Path(__file__).resolve().parent.parent.parent
|
|
@@ -90,8 +90,8 @@ async def health_check():
|
|
| 90 |
"db_path": str(db_path.absolute())
|
| 91 |
}
|
| 92 |
|
| 93 |
-
# 只包含
|
| 94 |
-
app.include_router(
|
| 95 |
|
| 96 |
if __name__ == "__main__":
|
| 97 |
import uvicorn
|
|
|
|
| 15 |
from fastapi.staticfiles import StaticFiles
|
| 16 |
from fastapi.responses import FileResponse
|
| 17 |
|
| 18 |
+
# 只导入核心trade查询路由
|
| 19 |
+
from apps.api.routers import trade
|
| 20 |
|
| 21 |
# 获取项目根目录
|
| 22 |
BASE_DIR = Path(__file__).resolve().parent.parent.parent
|
|
|
|
| 90 |
"db_path": str(db_path.absolute())
|
| 91 |
}
|
| 92 |
|
| 93 |
+
# 只包含核心trade查询路由
|
| 94 |
+
app.include_router(trade.router, prefix="/api/v1/trade", tags=["Trade Search"])
|
| 95 |
|
| 96 |
if __name__ == "__main__":
|
| 97 |
import uvicorn
|
apps/api/routers/trade.py
CHANGED
|
@@ -160,7 +160,46 @@ async def _search_with_postgres(
|
|
| 160 |
result = await db.execute(stmt)
|
| 161 |
records = result.scalars().all()
|
| 162 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
return PaginatedResponse(
|
| 164 |
total=total,
|
| 165 |
-
items=
|
| 166 |
)
|
|
|
|
| 160 |
result = await db.execute(stmt)
|
| 161 |
records = result.scalars().all()
|
| 162 |
|
| 163 |
+
# 转换为响应格式,处理datetime对象
|
| 164 |
+
items = []
|
| 165 |
+
for r in records:
|
| 166 |
+
# 处理日期:可能是datetime对象或字符串
|
| 167 |
+
trade_date_value = r.trade_date
|
| 168 |
+
if isinstance(trade_date_value, datetime):
|
| 169 |
+
trade_date = trade_date_value
|
| 170 |
+
elif isinstance(trade_date_value, str):
|
| 171 |
+
if not trade_date_value or trade_date_value.strip() == '':
|
| 172 |
+
trade_date = datetime(2020, 1, 1)
|
| 173 |
+
else:
|
| 174 |
+
try:
|
| 175 |
+
trade_date = datetime.fromisoformat(trade_date_value.replace(' ', 'T'))
|
| 176 |
+
except:
|
| 177 |
+
trade_date = datetime(2020, 1, 1)
|
| 178 |
+
else:
|
| 179 |
+
trade_date = datetime(2020, 1, 1)
|
| 180 |
+
|
| 181 |
+
items.append(TradeRecordResponse(
|
| 182 |
+
record_id=r.record_id,
|
| 183 |
+
source_record_id=r.source_record_id,
|
| 184 |
+
source_country=r.source_country,
|
| 185 |
+
trade_direction=r.trade_direction,
|
| 186 |
+
trade_date=trade_date,
|
| 187 |
+
importer_name=r.importer_name or "",
|
| 188 |
+
exporter_name=r.exporter_name or "",
|
| 189 |
+
hs_code=r.hs_code,
|
| 190 |
+
product_name=r.product_name,
|
| 191 |
+
amount=r.amount,
|
| 192 |
+
currency=r.currency,
|
| 193 |
+
weight=r.weight,
|
| 194 |
+
weight_unit=r.weight_unit,
|
| 195 |
+
origin_country=r.origin_country,
|
| 196 |
+
destination_country=r.destination_country,
|
| 197 |
+
departure_port=r.departure_port,
|
| 198 |
+
arrival_port=r.arrival_port,
|
| 199 |
+
transport_mode=r.transport_mode
|
| 200 |
+
))
|
| 201 |
+
|
| 202 |
return PaginatedResponse(
|
| 203 |
total=total,
|
| 204 |
+
items=items
|
| 205 |
)
|