3v324v23 commited on
Commit
af291ee
·
1 Parent(s): 6a2c6af

Add Pydantic validator to handle empty trade_date strings

Browse files
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
- for r in records:
79
- try:
80
- # 处理trade_date:如果是空字符串或None,使用默认日期
81
- if not r.trade_date or (isinstance(r.trade_date, str) and r.trade_date.strip() == ''):
82
- trade_date = datetime(2020, 1, 1)
83
- else:
84
- trade_date = r.trade_date
85
-
86
- items.append(TradeRecordResponse(
87
- record_id=r.record_id,
88
- source_record_id=r.source_record_id,
89
- source_country=r.source_country,
90
- trade_direction=r.trade_direction,
91
- trade_date=trade_date,
92
- importer_name=r.importer_name or "",
93
- exporter_name=r.exporter_name or "",
94
- hs_code=r.hs_code or "",
95
- product_name=r.product_name or "",
96
- amount=r.amount,
97
- currency=r.currency or "",
98
- weight=r.weight,
99
- weight_unit=r.weight_unit or "",
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