from __future__ import annotations from datetime import datetime from sqlalchemy import ( Boolean, Column, DateTime, ForeignKey, Integer, String, Table, func, ) from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship from .collections import Collection Base = DeclarativeBase() image_tags = Table( "image_tags", Base.metadata, Column("image_id", Integer, ForeignKey("images.id"), primary_key=True), Column("tag_id", Integer, ForeignKey("tags.id"), primary_key=True), ) class Tag(Base): __tablename__ = "tags" id: Mapped[int] = mapped_column(Integer, primary_key=True) name: Mapped[str] = mapped_column(String(50), unique=True, nullable=False) category: Mapped[str | None] = mapped_column(String(50), nullable=True) images: Mapped[list["Image"]] = relationship( "Image", secondary=image_tags, back_populates="tags", ) class Image(Base): __tablename__ = "images" id: Mapped[int] = mapped_column(Integer, primary_key=True) image_file: Mapped[str] = mapped_column(String(255), nullable=False) original_file: Mapped[str] = mapped_column(String(255), nullable=False) user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), nullable=False) publication_date: Mapped[datetime] = mapped_column( DateTime(timezone=True), server_default=func.now() ) source: Mapped[str] = mapped_column(String(255), nullable=False) views: Mapped[int] = mapped_column(Integer, default=0, nullable=False) likes: Mapped[int] = mapped_column(Integer, default=0, nullable=False) is_verified: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False) is_rejected: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False) tags: Mapped[list[Tag]] = relationship( "Tag", secondary=image_tags, back_populates="images", ) collections: Mapped[list["Collection"]] = relationship( "Collection", secondary="collection_images", back_populates="images", )