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) genre: 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"), )