mirror of
https://github.com/stardrophere/InsightRadar.git
synced 2026-06-06 01:57:51 +08:00
refresh
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
# app/schemas/schemas.py
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
from typing import Optional
|
||||
from datetime import datetime
|
||||
|
||||
# 枚举
|
||||
from app.models.models import SourceType
|
||||
|
||||
|
||||
# ==========================================
|
||||
# InfoSource (信息源) 相关的 Schemas
|
||||
# ==========================================
|
||||
|
||||
class InfoSourceBase(BaseModel):
|
||||
"""
|
||||
信息源的基础 Schema,包含各个操作通用的字段。
|
||||
使用 Field() 可以为 Swagger UI 接口文档提供明确的注释和校验规则。
|
||||
"""
|
||||
source_name: str = Field(..., max_length=100, description="信息源名称,例如:微博热搜、知乎热榜", examples=["微博热搜"])
|
||||
source_type: SourceType = Field(default=SourceType.API, description="抓取方式枚举")
|
||||
home_url: Optional[str] = Field(default=None, max_length=255, description="平台主页或接口的基础URL", examples=["weibo"])
|
||||
is_enabled: bool = Field(default=True, description="是否启用该信息源的定时抓取")
|
||||
|
||||
|
||||
class InfoSourceCreate(InfoSourceBase):
|
||||
"""
|
||||
创建信息源 (POST) 时接收的请求体。
|
||||
目前直接继承 Base 即可,如果以后有“只有创建时才能传的字段”,可以写在这里。
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class InfoSourceUpdate(BaseModel):
|
||||
"""
|
||||
更新信息源 (PATCH/PUT) 时接收的请求体。
|
||||
更新时,所有的字段都应该是可选的 (Optional),因为前端可能只想改其中一个字段。
|
||||
"""
|
||||
source_name: Optional[str] = Field(default=None, max_length=100)
|
||||
source_type: Optional[SourceType] = None
|
||||
home_url: Optional[str] = Field(default=None, max_length=255)
|
||||
is_enabled: Optional[bool] = None
|
||||
|
||||
|
||||
class InfoSourceResponse(InfoSourceBase):
|
||||
"""
|
||||
返回给前端的响应体格式 (GET 或 POST/PATCH 成功后的返回)。
|
||||
包含了数据库自动生成的 ID 和时间戳字段。
|
||||
"""
|
||||
id: int = Field(..., description="数据库主键ID")
|
||||
created_at: datetime = Field(..., description="创建时间")
|
||||
updated_at: datetime = Field(..., description="最后一次更新时间")
|
||||
|
||||
# 核心配置:告诉 Pydantic,允许传入 SQLAlchemy 的 ORM 类实例并自动提取属性
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
|
||||
# ==========================================
|
||||
# 分页响应包装器 (泛型设计,方便以后复用)
|
||||
# ==========================================
|
||||
from typing import Generic, TypeVar, List
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
class PaginatedResponse(BaseModel, Generic[T]):
|
||||
"""
|
||||
通用的列表响应格式,带总数统计,方便前端分页。
|
||||
"""
|
||||
total: int = Field(..., description="符合条件的数据总条数")
|
||||
data: List[T] = Field(..., description="当前页的数据列表")
|
||||
Reference in New Issue
Block a user