63 lines
1.5 KiB
Python
63 lines
1.5 KiB
Python
from datetime import datetime
|
|
from typing import Optional, List, Generic, T
|
|
|
|
from ninja import Schema
|
|
from pydantic import Field
|
|
|
|
|
|
# --- 假设的关联模型 Schema ---
|
|
class AuthorSchema(Schema):
|
|
id: int
|
|
username: str
|
|
|
|
|
|
class SectionSchema(Schema):
|
|
id: int
|
|
title: str # 假设 MainMenu 有 name 字段
|
|
|
|
|
|
class AttachmentSchema(Schema):
|
|
id: int
|
|
file: str # 返回文件 URL 路径,如 "articles/attachments/2026/04/03/file.pdf"
|
|
|
|
|
|
class PaginatedResponseSchema(Schema, Generic[T]):
|
|
items: List[T]
|
|
total: int
|
|
page: int
|
|
per_page: int
|
|
total_pages: int
|
|
|
|
|
|
# --- Article 主 Schema ---
|
|
class ArticleSchema(Schema):
|
|
id: int
|
|
title: str
|
|
content: str
|
|
cover: Optional[str] = None # 图片 URL 路径
|
|
is_contribution: bool
|
|
is_published: bool
|
|
created_at: datetime
|
|
updated_at: datetime
|
|
|
|
# 关联字段
|
|
author: Optional[AuthorSchema] = None
|
|
section: Optional[SectionSchema] = None
|
|
attachments: List[AttachmentSchema] = Field(default_factory=list)
|
|
|
|
|
|
# --- 创建文章用的 Schema (不含只读字段) ---
|
|
class ArticleCreateSchema(Schema):
|
|
title: str
|
|
section_id: Optional[int] = None # 外键通过 ID 传递
|
|
content: str
|
|
cover: Optional[str] = None # 注意:文件上传通常需单独处理,此处仅为文本路径示例
|
|
is_contribution: bool = False
|
|
is_published: bool = True
|
|
|
|
|
|
# --- 更新文章用的 Schema ---
|
|
class ArticleUpdateSchema(ArticleCreateSchema):
|
|
title: Optional[str] = None
|
|
content: Optional[str] = None
|