refactor: call a heading a title, and its use a placement

`template_field_library` and `template_field` were one word apart and meant
opposite kinds of thing — a catalogue of rows versus one row — and neither name
said what a row actually is. Every row of the library is a **heading**: a piece
of text like 「1. Introduction」 with a level and typography, which is what the
reader sees at the top of a paragraph; and every row of the join table is that
heading *placed* in a template, `sort` included.

    template_field_library  -> template_title
    template_field          -> template_title_placement
    field_id (the column)   -> title_id
    TemplateFieldLibrary    -> TemplateTitle
    TemplateField           -> TemplateTitlePlacement

The join table keeps its own name rather than becoming `_link`, because it is
not a bare link: it carries `sort`, the only place display order is stored, and
it deliberately allows the same title twice in one template. `_link` would
describe two foreign keys and hide the column the ordering rule rests on.

Carried through every level, because a half-renamed layer is how the confusing
pair got there in the first place: HTTP paths (`/template-field-library` ->
`/template-titles`), the template payload (`fields`/`field_id` ->
`placements`/`title_id`, and `field_count` -> `placement_count`), the Pydantic
schemas, the CRUD modules, the seed script, the frontend API module and types,
and `docs/OVERVIEW.md`, which now states the naming rules outright.

Two deliberate exceptions:

* `paper_template_filed_sort` is untouched — it is a column of the paper module
  and is spelled as the feature was specified.
* The MCP tools keep their agent-facing names (`field_list`, `field_ids`, and
  the `fields`/`field_id` keys in their responses). Agents already call them by
  those names; the storage names they read underneath have changed.

The migration renames in place (data preserved: 4 templates, 45 titles, 63
placements) and is written to tolerate a database where part of it already ran,
which is not hypothetical — the first attempt renamed the tables and then failed
on the column, because Alembic's MySQL dialect wants an explicit type for
`alter_column` and will not rename a column without one. TiDB carries the
foreign key along with both the renamed table and the renamed column, rehearsed
against the cluster first, and it stays enforced.

Verified: seed re-runs clean, the paper smoke test's 45 checks and the MCP
smoke test's 47 pass, `vue-tsc` and the build are clean, and the UI was driven
in a headless browser through the template list, its preview drawer, the title
library page, the template form (which now posts `placements` with `title_id`)
and the paper document with no console errors.
This commit is contained in:
2026-09-22 16:55:02 +08:00
parent 3732e36e64
commit 199e328b48
40 changed files with 837 additions and 663 deletions
+2 -2
View File
@@ -5,6 +5,6 @@ models. They own the commit: a route calls one function and gets back either a
persisted object or ``None``.
"""
from app.crud import paper, template, template_field_library
from app.crud import paper, template, template_title
__all__ = ["paper", "template", "template_field_library"]
__all__ = ["paper", "template", "template_title"]
+16 -16
View File
@@ -37,7 +37,7 @@ from app.models import (
PaperSentence,
PaperSentenceReference,
Template,
TemplateField,
TemplateTitlePlacement,
)
from app.schemas.paper import (
CitationInput,
@@ -112,8 +112,8 @@ def _template_paragraph_count_column():
is one paragraph, not two. A paper with no template counts 0.
"""
return (
select(func.count(func.distinct(TemplateField.sort)))
.where(TemplateField.template_id == Paper.template_id)
select(func.count(func.distinct(TemplateTitlePlacement.sort)))
.where(TemplateTitlePlacement.template_id == Paper.template_id)
.correlate(Paper)
.scalar_subquery()
)
@@ -219,7 +219,7 @@ def read(paper: Paper) -> PaperRead:
sentence_count=len(paper.sentences),
paragraph_count=len({s.paper_template_filed_sort for s in paper.sentences}),
template_paragraph_count=(
len({placement.sort for placement in paper.template.items})
len({placement.sort for placement in paper.template.placements})
if paper.template is not None
else 0
),
@@ -301,15 +301,15 @@ def build_document(db: Session, paper: Paper) -> PaperDocumentRead:
# ``sort, id`` — is the one that renders the heading. The reader is told,
# because two fields sharing a position is far more often a mistake than a
# plan.
placement_by_sort: dict[int, TemplateField] = {}
placement_by_sort: dict[int, TemplateTitlePlacement] = {}
tied: dict[int, list[str]] = defaultdict(list)
for item in paper.template.items if paper.template is not None else []:
for item in paper.template.placements if paper.template is not None else []:
if item.sort in placement_by_sort:
tied[item.sort].append(item.field.name)
tied[item.sort].append(item.title.name)
else:
placement_by_sort[item.sort] = item
for sort, names in sorted(tied.items()):
head = placement_by_sort[sort].field.name
head = placement_by_sort[sort].title.name
warnings.append(
f"模板在第 {sort} 段放了多个字段({head}、{'、'.join(names)}),"
"正文按第一条显示。"
@@ -322,7 +322,7 @@ def build_document(db: Session, paper: Paper) -> PaperDocumentRead:
if paper.template is None and paper.sentences:
warnings.append("这篇论文还没有选择模板,段落没有对应标题,全部按未设定显示。")
if paper.template is not None and not paper.template.items:
if paper.template is not None and not paper.template.placements:
warnings.append("所用模板还没有任何段落字段,论文暂时没有结构。")
# --- 3. the union, in ascending position order --------------------------
@@ -334,7 +334,7 @@ def build_document(db: Session, paper: Paper) -> PaperDocumentRead:
for position in positions:
placement = placement_by_sort.get(position)
field = placement.field if placement is not None else None
title = placement.title if placement is not None else None
# Re-sorted rather than trusted: the relationship already orders rows,
# but ordering is a contract of this response, so it is stated here too.
@@ -352,12 +352,12 @@ def build_document(db: Session, paper: Paper) -> PaperDocumentRead:
paragraph = ParagraphRead(
paper_template_filed_sort=position,
template_field_id=placement.id if placement is not None else None,
field_id=field.id if field is not None else None,
name=field.name if field is not None else None,
level=field.level if field is not None else 1,
font_size=float(field.font_size) if field is not None else None,
font_color=field.font_color if field is not None else None,
placement_id=placement.id if placement is not None else None,
title_id=title.id if title is not None else None,
name=title.name if title is not None else None,
level=title.level if title is not None else 1,
font_size=float(title.font_size) if title is not None else None,
font_color=title.font_color if title is not None else None,
matched=placement is not None,
sentences=sentences,
)
+33 -29
View File
@@ -1,4 +1,4 @@
"""Data access for templates and their ordered field selections."""
"""Data access for templates and their ordered title selections."""
from collections.abc import Sequence
@@ -6,8 +6,8 @@ from sqlalchemy import func, or_, select
from sqlalchemy.orm import Session
from app.crud.filters import LIKE_ESCAPE, like_pattern
from app.models import Template, TemplateFieldLibrary, TemplateField
from app.schemas.template import TemplateListItem, TemplateFieldInput
from app.models import Template, TemplateTitle, TemplateTitlePlacement
from app.schemas.template import TemplateListItem, TemplateTitlePlacementInput
def _conditions(keyword: str | None) -> list:
@@ -23,11 +23,11 @@ def _conditions(keyword: str | None) -> list:
]
def _field_count_column():
def _placement_count_column():
"""A correlated ``COUNT`` of the template's placement rows."""
return (
select(func.count(TemplateField.id))
.where(TemplateField.template_id == Template.id)
select(func.count(TemplateTitlePlacement.id))
.where(TemplateTitlePlacement.template_id == Template.id)
.correlate(Template)
.scalar_subquery()
)
@@ -40,7 +40,7 @@ def list_templates(
page: int = 1,
page_size: int = 20,
) -> tuple[list[TemplateListItem], int]:
"""Return one page of templates with their field counts, plus the total.
"""Return one page of templates with their placement counts, plus the total.
The outline itself is left out: a table row needs the count, not the rows.
Recently edited templates come first, since that is what a user returns to.
@@ -52,7 +52,7 @@ def list_templates(
) or 0
stmt = (
select(Template, _field_count_column().label("field_count"))
select(Template, _placement_count_column().label("placement_count"))
.where(*conditions)
.order_by(Template.updated_at.desc(), Template.id.desc())
.offset((page - 1) * page_size)
@@ -64,11 +64,11 @@ def list_templates(
id=template.id,
name=template.name,
abstract=template.abstract,
field_count=field_count,
placement_count=placement_count,
created_at=template.created_at,
updated_at=template.updated_at,
)
for template, field_count in db.execute(stmt).all()
for template, placement_count in db.execute(stmt).all()
]
return items, total
@@ -76,9 +76,9 @@ def list_templates(
def get(db: Session, template_id: int) -> Template | None:
"""Return one template with its outline already loaded, or ``None``.
``items`` (ordered by ``sort``) and each item's ``field`` are both
configured for eager loading on the relationships, so this is a fixed
number of queries rather than one per field.
``placements`` (ordered by ``sort``) and each placement's ``title`` are
both configured for eager loading on the relationships, so this is a fixed
number of queries rather than one per title.
"""
return db.get(Template, template_id)
@@ -91,24 +91,29 @@ def name_taken(db: Session, name: str, *, exclude_id: int | None = None) -> bool
return bool(db.scalar(stmt))
def missing_field_ids(db: Session, field_ids: Sequence[int]) -> list[int]:
"""Which of ``field_ids`` do not exist in the field library.
def missing_title_ids(db: Session, title_ids: Sequence[int]) -> list[int]:
"""Which of ``title_ids`` do not exist in the title library.
Returning the offenders rather than a boolean lets the API name them, which
is the difference between a usable error and "invalid request".
"""
wanted = set(field_ids)
wanted = set(title_ids)
if not wanted:
return []
found = set(
db.scalars(select(TemplateFieldLibrary.id).where(TemplateFieldLibrary.id.in_(wanted))).all()
db.scalars(select(TemplateTitle.id).where(TemplateTitle.id.in_(wanted))).all()
)
return sorted(wanted - found)
def _build_items(fields: Sequence[TemplateFieldInput]) -> list[TemplateField]:
def _build_placements(
placements: Sequence[TemplateTitlePlacementInput],
) -> list[TemplateTitlePlacement]:
"""Materialise the client's selection into placement rows."""
return [TemplateField(field_id=item.field_id, sort=item.sort) for item in fields]
return [
TemplateTitlePlacement(title_id=item.title_id, sort=item.sort)
for item in placements
]
def create(
@@ -116,11 +121,11 @@ def create(
*,
name: str,
abstract: str | None,
fields: Sequence[TemplateFieldInput],
placements: Sequence[TemplateTitlePlacementInput],
) -> Template:
"""Insert a template together with its ordered selection."""
template = Template(name=name, abstract=abstract)
template.items = _build_items(fields)
template.placements = _build_placements(placements)
db.add(template)
db.commit()
db.refresh(template)
@@ -134,9 +139,9 @@ def update(
name: str | None = None,
abstract: str | None = None,
abstract_provided: bool = False,
fields: Sequence[TemplateFieldInput] | None = None,
placements: Sequence[TemplateTitlePlacementInput] | None = None,
) -> Template:
"""Apply a partial update. ``fields=None`` leaves the selection untouched.
"""Apply a partial update. ``placements=None`` leaves the selection untouched.
``abstract_provided`` distinguishes "clear the abstract" from "leave it" —
both arrive as ``None`` in the payload, and only the caller knows which the
@@ -146,13 +151,12 @@ def update(
template.name = name
if abstract_provided:
template.abstract = abstract
if fields is not None:
# delete-orphan removes the dropped rows on flush. TiDB does not
# enforce ON DELETE CASCADE, so this ORM-level cascade is the only
# thing cleaning up the join table.
template.items.clear()
if placements is not None:
# delete-orphan removes the dropped rows on flush. The ORM cascade is
# what cleans up the join table, whether or not the database would.
template.placements.clear()
db.flush()
template.items.extend(_build_items(fields))
template.placements.extend(_build_placements(placements))
db.commit()
db.refresh(template)
-125
View File
@@ -1,125 +0,0 @@
"""Data access for the reusable template-field library (字段管理)."""
from collections.abc import Sequence
from sqlalchemy import Select, func, select
from sqlalchemy.orm import Session
from app.crud.filters import LIKE_ESCAPE, like_pattern
from app.models import TemplateField, TemplateFieldLibrary
from app.schemas.template_field_library import (
TemplateFieldLibraryCreate,
TemplateFieldLibraryUpdate,
)
def _conditions(keyword: str | None, level: int | None) -> list:
"""Translate the list filters into SQLAlchemy predicates."""
conditions = []
if keyword:
conditions.append(
TemplateFieldLibrary.name.like(like_pattern(keyword), escape=LIKE_ESCAPE)
)
if level is not None:
conditions.append(TemplateFieldLibrary.level == level)
return conditions
def _ordered(stmt: Select) -> Select:
"""Apply the library's canonical browse order.
Grouped by ``level`` first so the picker reads as an outline, then by
insertion order so a field stays where the user put it. Deliberately *not*
ordered by ``name``: names carry hand-written numbering ("1.", "10.",
"2."), and string ordering would scramble it.
"""
return stmt.order_by(TemplateFieldLibrary.level.asc(), TemplateFieldLibrary.id.asc())
def list_fields(
db: Session,
*,
keyword: str | None = None,
level: int | None = None,
page: int = 1,
page_size: int = 20,
) -> tuple[list[TemplateFieldLibrary], int]:
"""Return one page of the field library, plus the unpaged total."""
conditions = _conditions(keyword, level)
total = db.scalar(
select(func.count(TemplateFieldLibrary.id)).where(*conditions)
) or 0
stmt = _ordered(
select(TemplateFieldLibrary)
.where(*conditions)
.offset((page - 1) * page_size)
.limit(page_size)
)
return list(db.scalars(stmt).all()), total
def get(db: Session, field_id: int) -> TemplateFieldLibrary | None:
"""Return one field, or ``None``."""
return db.get(TemplateFieldLibrary, field_id)
def get_many(db: Session, field_ids: Sequence[int]) -> list[TemplateFieldLibrary]:
"""Return every field whose id is in ``field_ids`` (missing ids ignored)."""
if not field_ids:
return []
stmt = select(TemplateFieldLibrary).where(TemplateFieldLibrary.id.in_(list(field_ids)))
return list(db.scalars(stmt).all())
def usage_counts(db: Session, field_ids: Sequence[int]) -> dict[int, int]:
"""Count how many *distinct templates* place each field.
Drives the refusal message when a field in use is deleted. ``DISTINCT``
matters because one template may legitimately place the same field twice.
"""
if not field_ids:
return {}
stmt = (
select(
TemplateField.field_id,
func.count(func.distinct(TemplateField.template_id)),
)
.where(TemplateField.field_id.in_(list(field_ids)))
.group_by(TemplateField.field_id)
)
return {field_id: count for field_id, count in db.execute(stmt).all()}
def create(db: Session, data: TemplateFieldLibraryCreate) -> TemplateFieldLibrary:
"""Insert a field."""
field = TemplateFieldLibrary(**data.model_dump())
db.add(field)
db.commit()
db.refresh(field)
return field
def update(
db: Session,
field: TemplateFieldLibrary,
data: TemplateFieldLibraryUpdate,
) -> TemplateFieldLibrary:
"""Apply a partial update to a field.
``exclude_unset`` is what makes PATCH semantics work: a key the client did
not send leaves the column alone, while an explicit ``null`` — which the
schemas reject for every nullable-typed column here — would not.
"""
for key, value in data.model_dump(exclude_unset=True).items():
setattr(field, key, value)
db.commit()
db.refresh(field)
return field
def delete(db: Session, field: TemplateFieldLibrary) -> None:
"""Delete a field. Callers must check :func:`usage_counts` first."""
db.delete(field)
db.commit()
+125
View File
@@ -0,0 +1,125 @@
"""Data access for the reusable template-title library (字段管理)."""
from collections.abc import Sequence
from sqlalchemy import Select, func, select
from sqlalchemy.orm import Session
from app.crud.filters import LIKE_ESCAPE, like_pattern
from app.models import TemplateTitlePlacement, TemplateTitle
from app.schemas.template_title import (
TemplateTitleCreate,
TemplateTitleUpdate,
)
def _conditions(keyword: str | None, level: int | None) -> list:
"""Translate the list filters into SQLAlchemy predicates."""
conditions = []
if keyword:
conditions.append(
TemplateTitle.name.like(like_pattern(keyword), escape=LIKE_ESCAPE)
)
if level is not None:
conditions.append(TemplateTitle.level == level)
return conditions
def _ordered(stmt: Select) -> Select:
"""Apply the library's canonical browse order.
Grouped by ``level`` first so the picker reads as an outline, then by
insertion order so a title stays where the user put it. Deliberately *not*
ordered by ``name``: names carry hand-written numbering ("1.", "10.",
"2."), and string ordering would scramble it.
"""
return stmt.order_by(TemplateTitle.level.asc(), TemplateTitle.id.asc())
def list_titles(
db: Session,
*,
keyword: str | None = None,
level: int | None = None,
page: int = 1,
page_size: int = 20,
) -> tuple[list[TemplateTitle], int]:
"""Return one page of the title library, plus the unpaged total."""
conditions = _conditions(keyword, level)
total = db.scalar(
select(func.count(TemplateTitle.id)).where(*conditions)
) or 0
stmt = _ordered(
select(TemplateTitle)
.where(*conditions)
.offset((page - 1) * page_size)
.limit(page_size)
)
return list(db.scalars(stmt).all()), total
def get(db: Session, title_id: int) -> TemplateTitle | None:
"""Return one library entry, or ``None``."""
return db.get(TemplateTitle, title_id)
def get_many(db: Session, title_ids: Sequence[int]) -> list[TemplateTitle]:
"""Return every entry whose id is in ``title_ids`` (missing ids ignored)."""
if not title_ids:
return []
stmt = select(TemplateTitle).where(TemplateTitle.id.in_(list(title_ids)))
return list(db.scalars(stmt).all())
def usage_counts(db: Session, title_ids: Sequence[int]) -> dict[int, int]:
"""Count how many *distinct templates* place each title.
Drives the refusal message when a title in use is deleted. ``DISTINCT``
matters because one template may legitimately place the same title twice.
"""
if not title_ids:
return {}
stmt = (
select(
TemplateTitlePlacement.title_id,
func.count(func.distinct(TemplateTitlePlacement.template_id)),
)
.where(TemplateTitlePlacement.title_id.in_(list(title_ids)))
.group_by(TemplateTitlePlacement.title_id)
)
return {title_id: count for title_id, count in db.execute(stmt).all()}
def create(db: Session, data: TemplateTitleCreate) -> TemplateTitle:
"""Insert a library entry."""
title = TemplateTitle(**data.model_dump())
db.add(title)
db.commit()
db.refresh(title)
return title
def update(
db: Session,
title: TemplateTitle,
data: TemplateTitleUpdate,
) -> TemplateTitle:
"""Apply a partial update to a library entry.
``exclude_unset`` is what makes PATCH semantics work: a key the client did
not send leaves the column alone, while an explicit ``null`` — which the
schemas reject for every nullable-typed column here — would not.
"""
for key, value in data.model_dump(exclude_unset=True).items():
setattr(title, key, value)
db.commit()
db.refresh(title)
return title
def delete(db: Session, title: TemplateTitle) -> None:
"""Delete a library entry. Callers must check :func:`usage_counts` first."""
db.delete(title)
db.commit()