53 lines
2.0 KiB
Python
53 lines
2.0 KiB
Python
"""Image database model."""
|
|
|
|
from datetime import datetime
|
|
from typing import TYPE_CHECKING
|
|
from uuid import UUID, uuid4
|
|
|
|
from sqlalchemy import BigInteger, DateTime, ForeignKey, Integer, String
|
|
from sqlalchemy.dialects.postgresql import JSONB
|
|
from sqlalchemy.dialects.postgresql import UUID as PGUUID
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from app.database.base import Base
|
|
|
|
if TYPE_CHECKING:
|
|
from app.database.models.board_image import BoardImage
|
|
from app.database.models.user import User
|
|
|
|
|
|
class Image(Base):
|
|
"""
|
|
Image model representing uploaded image files.
|
|
|
|
Images are stored in MinIO and can be reused across multiple boards.
|
|
Reference counting tracks how many boards use each image.
|
|
"""
|
|
|
|
__tablename__ = "images"
|
|
|
|
id: Mapped[UUID] = mapped_column(PGUUID(as_uuid=True), primary_key=True, default=uuid4)
|
|
user_id: Mapped[UUID] = mapped_column(
|
|
PGUUID(as_uuid=True), ForeignKey("users.id", ondelete="CASCADE"), nullable=False
|
|
)
|
|
filename: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
storage_path: Mapped[str] = mapped_column(String(512), nullable=False)
|
|
file_size: Mapped[int] = mapped_column(BigInteger, nullable=False)
|
|
mime_type: Mapped[str] = mapped_column(String(100), nullable=False)
|
|
width: Mapped[int] = mapped_column(Integer, nullable=False)
|
|
height: Mapped[int] = mapped_column(Integer, nullable=False)
|
|
image_metadata: Mapped[dict] = mapped_column(JSONB, nullable=False)
|
|
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.utcnow)
|
|
reference_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
|
|
|
|
# Relationships
|
|
user: Mapped["User"] = relationship("User", back_populates="images")
|
|
board_images: Mapped[list["BoardImage"]] = relationship(
|
|
"BoardImage", back_populates="image", cascade="all, delete-orphan"
|
|
)
|
|
|
|
def __repr__(self) -> str:
|
|
"""String representation of Image."""
|
|
return f"<Image(id={self.id}, filename='{self.filename}', user_id={self.user_id})>"
|