Files
webref/backend/app/database/models/board_image.py
Danilo Reyes 209b6d9f18 fix part 2
2025-11-02 18:23:10 -06:00

68 lines
2.5 KiB
Python

"""BoardImage database model - junction table for boards and images."""
from datetime import datetime, timezone
from typing import TYPE_CHECKING
from uuid import UUID, uuid4
from sqlalchemy import DateTime, ForeignKey, Integer
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 import Board
from app.database.models.group import Group
from app.database.models.image import Image
class BoardImage(Base):
"""
BoardImage model - junction table connecting boards and images.
Stores position, transformations, and z-order for each image on a board.
"""
__tablename__ = "board_images"
id: Mapped[UUID] = mapped_column(PGUUID(as_uuid=True), primary_key=True, default=uuid4)
board_id: Mapped[UUID] = mapped_column(
PGUUID(as_uuid=True), ForeignKey("boards.id", ondelete="CASCADE"), nullable=False
)
image_id: Mapped[UUID] = mapped_column(
PGUUID(as_uuid=True), ForeignKey("images.id", ondelete="CASCADE"), nullable=False
)
position: Mapped[dict] = mapped_column(JSONB, nullable=False)
transformations: Mapped[dict] = mapped_column(
JSONB,
nullable=False,
default=lambda: {
"scale": 1.0,
"rotation": 0,
"opacity": 1.0,
"flipped_h": False,
"flipped_v": False,
"greyscale": False,
},
)
z_order: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
group_id: Mapped[UUID | None] = mapped_column(
PGUUID(as_uuid=True), ForeignKey("groups.id", ondelete="SET NULL"), nullable=True
)
created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=lambda: datetime.now(timezone.utc))
updated_at: Mapped[datetime] = mapped_column(
DateTime, nullable=False, default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc)
)
# Relationships
board: Mapped["Board"] = relationship("Board", back_populates="board_images")
image: Mapped["Image"] = relationship("Image", back_populates="board_images")
group: Mapped["Group | None"] = relationship("Group", back_populates="board_images")
def __repr__(self) -> str:
"""String representation of BoardImage."""
return f"<BoardImage(id={self.id}, board_id={self.board_id}, image_id={self.image_id})>"