summaryrefslogtreecommitdiff
path: root/models.py
blob: 6643a586848617a7b8abcf2569cfcaff599931d4 (plain)
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
from datetime import UTC, date, datetime

from sqlalchemy import Boolean, CheckConstraint, Date, DateTime, Integer, String, Text, text
from sqlalchemy.orm import Mapped, mapped_column

from database import Base


def utc_now() -> datetime:
    return datetime.now(UTC)


class Film(Base):
    __tablename__ = "films"
    __table_args__ = (
        CheckConstraint("shelf in ('diary', 'queue', 'abandoned')", name="ck_films_shelf"),
        CheckConstraint("stars in (0, 1, 2, 3)", name="ck_films_stars"),
        CheckConstraint("rewatch_count >= 0", name="ck_films_rewatch_count"),
        {"sqlite_autoincrement": True},
    )

    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True, index=True)
    tmdb_id: Mapped[int | None] = mapped_column(Integer, nullable=True)
    poster_url: Mapped[str | None] = mapped_column(String(1024), nullable=True)
    title: Mapped[str] = mapped_column(String(255), index=True, nullable=False)
    original_title: Mapped[str | None] = mapped_column(String(255), nullable=True)
    director: Mapped[str | None] = mapped_column(String(255), nullable=True)
    year: Mapped[int | None] = mapped_column(Integer, nullable=True)
    country: Mapped[str | None] = mapped_column(String(255), nullable=True)
    language: Mapped[str | None] = mapped_column(String(255), nullable=True)
    runtime: Mapped[int | None] = mapped_column(Integer, nullable=True)
    date_watched: Mapped[date | None] = mapped_column(Date, nullable=True)
    rewatch: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, server_default=text("0"))
    rewatch_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default=text("0"))
    stars: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default=text("0"))
    watched_with: Mapped[str | None] = mapped_column(String(255), nullable=True)
    shelf: Mapped[str] = mapped_column(String(32), nullable=False, default="diary", server_default=text("'diary'"))
    how_found: Mapped[str | None] = mapped_column(String(255), nullable=True)
    context: Mapped[str | None] = mapped_column(String(255), nullable=True)
    notes: Mapped[str | None] = mapped_column(Text, nullable=True)
    created_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True),
        nullable=False,
        default=utc_now,
        server_default=text("CURRENT_TIMESTAMP"),
    )
    updated_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True),
        nullable=False,
        default=utc_now,
        onupdate=utc_now,
        server_default=text("CURRENT_TIMESTAMP"),
    )