Spaces:
Runtime error
Runtime error
Add Pydantic validator to handle empty trade_date strings
Browse files- apps/api/routers/trade_hf.py +23 -32
- apps/api/schemas/trade.py +9 -2
apps/api/routers/trade_hf.py
CHANGED
|
@@ -74,38 +74,29 @@ async def search_trade_records(
|
|
| 74 |
records = result.scalars().all()
|
| 75 |
|
| 76 |
# 转换为响应格式
|
| 77 |
-
items = [
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
origin_country=r.origin_country or "",
|
| 101 |
-
destination_country=r.destination_country or "",
|
| 102 |
-
departure_port=r.departure_port or "",
|
| 103 |
-
arrival_port=r.arrival_port or "",
|
| 104 |
-
transport_mode=r.transport_mode or ""
|
| 105 |
-
))
|
| 106 |
-
except Exception as e:
|
| 107 |
-
print(f"Error processing record {r.record_id}: trade_date={repr(r.trade_date)}, error={e}")
|
| 108 |
-
raise
|
| 109 |
|
| 110 |
return PaginatedResponse(
|
| 111 |
items=items,
|
|
|
|
| 74 |
records = result.scalars().all()
|
| 75 |
|
| 76 |
# 转换为响应格式
|
| 77 |
+
items = [
|
| 78 |
+
TradeRecordResponse(
|
| 79 |
+
record_id=r.record_id,
|
| 80 |
+
source_record_id=r.source_record_id,
|
| 81 |
+
source_country=r.source_country,
|
| 82 |
+
trade_direction=r.trade_direction,
|
| 83 |
+
trade_date=r.trade_date,
|
| 84 |
+
importer_name=r.importer_name or "",
|
| 85 |
+
exporter_name=r.exporter_name or "",
|
| 86 |
+
hs_code=r.hs_code or "",
|
| 87 |
+
product_name=r.product_name or "",
|
| 88 |
+
amount=r.amount,
|
| 89 |
+
currency=r.currency or "",
|
| 90 |
+
weight=r.weight,
|
| 91 |
+
weight_unit=r.weight_unit or "",
|
| 92 |
+
origin_country=r.origin_country or "",
|
| 93 |
+
destination_country=r.destination_country or "",
|
| 94 |
+
departure_port=r.departure_port or "",
|
| 95 |
+
arrival_port=r.arrival_port or "",
|
| 96 |
+
transport_mode=r.transport_mode or ""
|
| 97 |
+
)
|
| 98 |
+
for r in records
|
| 99 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
return PaginatedResponse(
|
| 102 |
items=items,
|
apps/api/schemas/trade.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
-
from pydantic import BaseModel, Field
|
| 2 |
-
from typing import List, Optional, TypeVar, Generic
|
| 3 |
from datetime import datetime
|
| 4 |
|
| 5 |
class TradeQueryRequest(BaseModel):
|
|
@@ -40,6 +40,13 @@ class TradeRecordResponse(BaseModel):
|
|
| 40 |
arrival_port: Optional[str]
|
| 41 |
transport_mode: Optional[str]
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
class Config:
|
| 44 |
from_attributes = True
|
| 45 |
|
|
|
|
| 1 |
+
from pydantic import BaseModel, Field, field_validator
|
| 2 |
+
from typing import List, Optional, TypeVar, Generic, Union
|
| 3 |
from datetime import datetime
|
| 4 |
|
| 5 |
class TradeQueryRequest(BaseModel):
|
|
|
|
| 40 |
arrival_port: Optional[str]
|
| 41 |
transport_mode: Optional[str]
|
| 42 |
|
| 43 |
+
@field_validator('trade_date', mode='before')
|
| 44 |
+
@classmethod
|
| 45 |
+
def validate_trade_date(cls, v):
|
| 46 |
+
if not v or (isinstance(v, str) and v.strip() == ''):
|
| 47 |
+
return datetime(2020, 1, 1)
|
| 48 |
+
return v
|
| 49 |
+
|
| 50 |
class Config:
|
| 51 |
from_attributes = True
|
| 52 |
|