File size: 3,523 Bytes
bd09498
 
 
 
 
 
 
 
193eb98
bd09498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
193eb98
 
 
 
4aac406
 
 
193eb98
bd09498
 
 
 
 
 
 
 
 
 
 
 
 
fc1a5f0
bd09498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc1a5f0
bd09498
 
 
 
1865b8a
 
 
 
bd09498
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""
Modèles SQLAlchemy 2.0 — tables Corpus, Manuscript, Page.

Ces modèles représentent la couche de persistance (BDD).
Ils NE se substituent PAS aux schémas Pydantic (source canonique des types).
"""
# 1. stdlib
from datetime import datetime, timezone
from functools import partial

# 2. third-party
from sqlalchemy import DateTime, Float, ForeignKey, Integer, String, Text
from sqlalchemy.orm import Mapped, mapped_column, relationship

# 3. local
from app.models.database import Base


class CorpusModel(Base):
    """Un corpus regroupe un ou plusieurs manuscrits sous un même profil."""

    __tablename__ = "corpora"

    id: Mapped[str] = mapped_column(String, primary_key=True)
    slug: Mapped[str] = mapped_column(String, unique=True, nullable=False, index=True)
    title: Mapped[str] = mapped_column(String, nullable=False)
    profile_id: Mapped[str] = mapped_column(String, nullable=False)
    created_at: Mapped[datetime] = mapped_column(
        DateTime, nullable=False, default=partial(datetime.now, tz=timezone.utc)
    )
    updated_at: Mapped[datetime] = mapped_column(
        DateTime, nullable=False,
        default=partial(datetime.now, tz=timezone.utc),
        onupdate=partial(datetime.now, tz=timezone.utc),
    )

    manuscripts: Mapped[list["ManuscriptModel"]] = relationship(
        back_populates="corpus", cascade="all, delete-orphan"
    )


class ManuscriptModel(Base):
    """Un manuscrit appartient à un corpus et contient des pages."""

    __tablename__ = "manuscripts"

    id: Mapped[str] = mapped_column(String, primary_key=True)
    corpus_id: Mapped[str] = mapped_column(
        String, ForeignKey("corpora.id", ondelete="CASCADE"), nullable=False, index=True
    )
    shelfmark: Mapped[str | None] = mapped_column(String, nullable=True)
    title: Mapped[str] = mapped_column(String, nullable=False)
    date_label: Mapped[str | None] = mapped_column(String, nullable=True)
    total_pages: Mapped[int] = mapped_column(Integer, default=0, nullable=False)

    corpus: Mapped["CorpusModel"] = relationship(back_populates="manuscripts")
    pages: Mapped[list["PageModel"]] = relationship(
        back_populates="manuscript", cascade="all, delete-orphan"
    )


class PageModel(Base):
    """Une page appartient à un manuscrit.

    processing_status suit le cycle de vie défini en CLAUDE.md §10 :
      CREATED → INGESTING → INGESTED → PREPARED → ANALYZED → LAYERED
      → EXPORTED → VALIDATED → ERROR
    """

    __tablename__ = "pages"

    id: Mapped[str] = mapped_column(String, primary_key=True)
    manuscript_id: Mapped[str] = mapped_column(
        String, ForeignKey("manuscripts.id", ondelete="CASCADE"), nullable=False, index=True
    )
    folio_label: Mapped[str] = mapped_column(String, nullable=False)
    sequence: Mapped[int] = mapped_column(Integer, nullable=False)
    image_master_path: Mapped[str | None] = mapped_column(Text, nullable=True)
    iiif_service_url: Mapped[str | None] = mapped_column(Text, nullable=True)
    canvas_width: Mapped[int | None] = mapped_column(Integer, nullable=True)
    canvas_height: Mapped[int | None] = mapped_column(Integer, nullable=True)
    manifest_url: Mapped[str | None] = mapped_column(Text, nullable=True)
    processing_status: Mapped[str] = mapped_column(
        String, nullable=False, default="CREATED"
    )
    confidence_summary: Mapped[float | None] = mapped_column(Float, nullable=True)

    manuscript: Mapped["ManuscriptModel"] = relationship(back_populates="pages")