Spaces:
Sleeping
Sleeping
File size: 3,693 Bytes
b84ea83 | 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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | """
Photo Model β ORM + Pydantic Schemas
ORM: Photo (SQLAlchemy table: photos)
Schemas: PhotoCreate, PhotoResponse
"""
import uuid
from datetime import datetime
from typing import Optional
from sqlalchemy import String, Text, BigInteger, DateTime, ForeignKey, func
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import Mapped, mapped_column, relationship
from pydantic import BaseModel, Field, ConfigDict
from models.base import Base
# ββββββββββββββββββββββββββββββββββββββββββββββββββ
# ORM Model
# ββββββββββββββββββββββββββββββββββββββββββββββββββ
class Photo(Base):
"""SQLAlchemy ORM model for the 'photos' table."""
__tablename__ = "photos"
id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
)
visit_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True),
ForeignKey("visits.id", ondelete="CASCADE"),
nullable=False,
)
checklist_id: Mapped[Optional[uuid.UUID]] = mapped_column(
UUID(as_uuid=True),
ForeignKey("checklists.id", ondelete="SET NULL"),
nullable=True,
)
assessment_id: Mapped[Optional[uuid.UUID]] = mapped_column(
UUID(as_uuid=True),
ForeignKey("assessments.id", ondelete="SET NULL"),
nullable=True,
comment="Linked assessment item (for per-item photo gallery)"
)
file_path: Mapped[str] = mapped_column(String(500), nullable=False)
file_name: Mapped[str] = mapped_column(String(255), nullable=False)
file_size: Mapped[Optional[int]] = mapped_column(BigInteger, nullable=True)
mime_type: Mapped[Optional[str]] = mapped_column(String(100), nullable=True)
caption: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
uploaded_at: Mapped[datetime] = mapped_column(
DateTime, server_default=func.now()
)
created_at: Mapped[datetime] = mapped_column(
DateTime, server_default=func.now()
)
# Relationships
visit = relationship("Visit", back_populates="photos")
checklist = relationship("Checklist", back_populates="photos", foreign_keys=[checklist_id])
assessment = relationship("Assessment", back_populates="photos", foreign_keys=[assessment_id])
def __repr__(self) -> str:
return f"<Photo(id={self.id}, file='{self.file_name}')>"
# ββββββββββββββββββββββββββββββββββββββββββββββββββ
# Pydantic Schemas
# ββββββββββββββββββββββββββββββββββββββββββββββββββ
class PhotoCreate(BaseModel):
"""Schema for photo upload metadata."""
checklist_id: Optional[uuid.UUID] = Field(
None, description="Optional linked checklist item"
)
assessment_id: Optional[uuid.UUID] = Field(
None, description="Optional linked assessment item (per-item photo)"
)
caption: Optional[str] = Field(None, description="Photo caption")
class PhotoResponse(BaseModel):
"""Schema for a single photo in API responses."""
model_config = ConfigDict(from_attributes=True)
id: uuid.UUID
visit_id: uuid.UUID
checklist_id: Optional[uuid.UUID]
assessment_id: Optional[uuid.UUID]
file_path: str
file_name: str
file_size: Optional[int]
mime_type: Optional[str]
caption: Optional[str]
uploaded_at: datetime
created_at: datetime
|