|
@@ -1,5 +1,6 @@
|
|
|
import json
|
|
import json
|
|
|
from datetime import datetime
|
|
from datetime import datetime
|
|
|
|
|
+from pathlib import Path
|
|
|
from typing import Optional, Dict, Any, List
|
|
from typing import Optional, Dict, Any, List
|
|
|
from pydantic import BaseModel, field_validator
|
|
from pydantic import BaseModel, field_validator
|
|
|
|
|
|
|
@@ -8,7 +9,7 @@ from pydantic import BaseModel, field_validator
|
|
|
|
|
|
|
|
class CardImageResponse(BaseModel):
|
|
class CardImageResponse(BaseModel):
|
|
|
"""用于API响应的图片数据模型 (主键为 id)"""
|
|
"""用于API响应的图片数据模型 (主键为 id)"""
|
|
|
- id: int # 原 image_id
|
|
|
|
|
|
|
+ id: int
|
|
|
card_id: int
|
|
card_id: int
|
|
|
image_type: str
|
|
image_type: str
|
|
|
image_name: Optional[str] = None
|
|
image_name: Optional[str] = None
|
|
@@ -21,6 +22,17 @@ class CardImageResponse(BaseModel):
|
|
|
class Config:
|
|
class Config:
|
|
|
from_attributes = True
|
|
from_attributes = True
|
|
|
|
|
|
|
|
|
|
+ @field_validator('image_path', mode='before')
|
|
|
|
|
+ @classmethod
|
|
|
|
|
+ def format_image_path(cls, v:str):
|
|
|
|
|
+ """将绝对文件路径转换为 'Data/filename.jpg' 格式的相对URL路径"""
|
|
|
|
|
+ if not isinstance(v, str) or not v:
|
|
|
|
|
+ return v
|
|
|
|
|
+ p = Path(v)
|
|
|
|
|
+ relative_path = f"{p.parent.name}/{p.name}"
|
|
|
|
|
+
|
|
|
|
|
+ return relative_path
|
|
|
|
|
+
|
|
|
@field_validator('detection_json', 'modified_json', mode='before')
|
|
@field_validator('detection_json', 'modified_json', mode='before')
|
|
|
@classmethod
|
|
@classmethod
|
|
|
def parse_json_string(cls, v):
|
|
def parse_json_string(cls, v):
|
|
@@ -36,7 +48,7 @@ class CardImageResponse(BaseModel):
|
|
|
|
|
|
|
|
class CardDetailResponse(BaseModel):
|
|
class CardDetailResponse(BaseModel):
|
|
|
"""用于响应单个卡牌详细信息的模型 (主键为 id)"""
|
|
"""用于响应单个卡牌详细信息的模型 (主键为 id)"""
|
|
|
- id: int # 原 card_id
|
|
|
|
|
|
|
+ id: int
|
|
|
card_name: Optional[str] = None
|
|
card_name: Optional[str] = None
|
|
|
created_at: datetime
|
|
created_at: datetime
|
|
|
updated_at: datetime
|
|
updated_at: datetime
|