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:
@@ -0,0 +1,82 @@
|
||||
"""The reusable template-field library (字段库).
|
||||
|
||||
A *library entry* is one heading a paper can contain — "1. Introduction",
|
||||
"2.1 Dataset", "0 Abstract". Fields live in this one global library and are
|
||||
never owned by a template.
|
||||
|
||||
Why there is no ``parent_id``
|
||||
-----------------------------
|
||||
A tree would make a field usable under exactly one parent, so a level-2 heading
|
||||
such as "Background" could not sit under both "1. Introduction" and
|
||||
"2. Related Work". Hierarchy is therefore expressed only by
|
||||
:attr:`TemplateTitle.level` (1, 2, 3 ...), which is a *rendering hint* — it
|
||||
drives indentation and numbering semantics in the UI — while a field stays
|
||||
free to be attached to any number of templates and any number of parents
|
||||
within them.
|
||||
|
||||
The number that the reader sees is part of :attr:`name` and is written by the
|
||||
user ("1. Introduction", "0 Abstract"). Nothing derives or rewrites it.
|
||||
|
||||
Ordering inside a template is *not* stored here: it lives in
|
||||
``template_title_placement.sort``. This table has no ``sort`` column on purpose, so that
|
||||
one library field can occupy a different position in every template.
|
||||
|
||||
Naming: the table is ``template_title`` and one row is one entry of it
|
||||
— the class is named after the table, as everywhere else in this package. The
|
||||
library is global, but it belongs to the 模板 module: it is the raw material a
|
||||
template is built from, and it is managed from the same menu.
|
||||
"""
|
||||
|
||||
from decimal import Decimal
|
||||
|
||||
from sqlalchemy import Integer, Numeric, String, text
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from app.db.base import Base
|
||||
from app.models.mixins import TimestampMixin
|
||||
|
||||
|
||||
class TemplateTitle(TimestampMixin, Base):
|
||||
"""A single reusable heading, with the typography it should render in."""
|
||||
|
||||
__tablename__ = "template_title"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
|
||||
#: Display name, numbering included. Stored and rendered verbatim.
|
||||
name: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||
|
||||
#: Heading depth: 1 for "1.", 2 for "1.1", 3 for "1.1.1". Rendering hint
|
||||
#: only — it does not link fields to each other.
|
||||
level: Mapped[int] = mapped_column(
|
||||
Integer,
|
||||
nullable=False,
|
||||
default=1,
|
||||
server_default=text("1"),
|
||||
)
|
||||
|
||||
#: Font size in points. ``Numeric`` rather than ``Integer`` because the
|
||||
#: conventional Chinese sizes are fractional (五号 = 10.5pt,
|
||||
#: 小四 = 12pt).
|
||||
font_size: Mapped[Decimal] = mapped_column(
|
||||
Numeric(4, 1),
|
||||
nullable=False,
|
||||
default=Decimal("12.0"),
|
||||
server_default=text("12.0"),
|
||||
)
|
||||
|
||||
#: Font colour as ``#RRGGBB`` — the canonical RGB encoding. The API
|
||||
#: normalises ``rgb(20, 30, 40)``, ``#abc`` and bare ``aabbcc`` to it on
|
||||
#: write, so the column always holds one comparable format.
|
||||
font_color: Mapped[str] = mapped_column(
|
||||
String(7),
|
||||
nullable=False,
|
||||
default="#000000",
|
||||
server_default=text("'#000000'"),
|
||||
)
|
||||
|
||||
def __repr__(self) -> str: # pragma: no cover - debugging aid
|
||||
return (
|
||||
f"<TemplateTitle id={self.id} name={self.name!r} "
|
||||
f"level={self.level} color={self.font_color}>"
|
||||
)
|
||||
Reference in New Issue
Block a user