"""Request/response schemas for templates (模板管理).""" from datetime import datetime from decimal import Decimal from pydantic import BaseModel, ConfigDict, Field from app.models.template import Template from app.models.template_title_placement import TemplateTitlePlacement from app.schemas.common import TypographyMixin class TemplateTitlePlacementInput(BaseModel): """One placement of a library title inside a template. The client sends the whole ordered selection on create and on update; the server replaces the stored rows with it. ``sort`` is a plain ascending integer and is the *only* thing that decides render order. """ title_id: int sort: int = Field(default=0, ge=-1_000_000, le=1_000_000) class TemplateTitlePlacementRead(TypographyMixin, BaseModel): """A template's placement, flattened with the library row it points at. Persistence keeps the two apart — a placement row plus the library row it references — but a client rendering an outline wants one flat record, so the typography is denormalised into the response. That is also what keeps the outline in sync: there is only ever one copy of a title's name and styling, in the library. """ id: int title_id: int sort: int name: str level: int font_size: Decimal font_color: str @classmethod def from_model(cls, item: TemplateTitlePlacement) -> "TemplateTitlePlacementRead": """Flatten a placement row and its library row into one record.""" return cls( id=item.id, title_id=item.title_id, sort=item.sort, name=item.title.name, level=item.title.level, font_size=item.title.font_size, font_color=item.title.font_color, ) class TemplateBase(BaseModel): """Shared body of the create/update payloads.""" name: str = Field(min_length=1, max_length=255) abstract: str | None = None class TemplateCreate(TemplateBase): """Payload for ``POST /templates``. The selection is free: any subset, any order, repeats allowed. The server stores it as given and orders it by ``sort`` on read. """ placements: list[TemplateTitlePlacementInput] = Field(default_factory=list) class TemplateUpdate(BaseModel): """Payload for ``PATCH /templates/{id}``. ``placements`` is a full replacement when present — omit it to leave the selection untouched. """ name: str | None = Field(default=None, min_length=1, max_length=255) abstract: str | None = None placements: list[TemplateTitlePlacementInput] | None = None class TemplateListItem(BaseModel): """A template as it appears in the list table — no placement rows.""" model_config = ConfigDict(from_attributes=True) id: int name: str abstract: str | None placement_count: int created_at: datetime updated_at: datetime class TemplateRead(BaseModel): """A template with its outline, already ordered by ``sort``.""" model_config = ConfigDict(from_attributes=True) id: int name: str abstract: str | None #: The outline, ordered by ``sort`` — the ORM relationship sets that #: ordering, so this list is display-ready with no client-side sorting. placements: list[TemplateTitlePlacementRead] created_at: datetime updated_at: datetime @classmethod def from_model(cls, template: Template) -> "TemplateRead": """Build the response from a template and its placement rows.""" return cls( id=template.id, name=template.name, abstract=template.abstract, placements=[ TemplateTitlePlacementRead.from_model(item) for item in template.placements ], created_at=template.created_at, updated_at=template.updated_at, )