Spaces:
Sleeping
Sleeping
| """ | |
| Checklist Model β ORM + Pydantic Schemas | |
| ORM: Checklist (SQLAlchemy table: checklists) | |
| Schemas: ChecklistCreate, ChecklistUpdate, ChecklistResponse | |
| """ | |
| import uuid | |
| from datetime import datetime | |
| from typing import Optional | |
| from sqlalchemy import String, Text, Boolean, 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 Checklist(Base): | |
| """SQLAlchemy ORM model for the 'checklists' table.""" | |
| __tablename__ = "checklists" | |
| 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, | |
| ) | |
| item_name: Mapped[str] = mapped_column(String(255), nullable=False) | |
| is_checked: Mapped[bool] = mapped_column(Boolean, default=False) | |
| notes: Mapped[Optional[str]] = mapped_column(Text, nullable=True) | |
| checked_at: Mapped[Optional[datetime]] = mapped_column( | |
| DateTime, nullable=True | |
| ) | |
| created_at: Mapped[datetime] = mapped_column( | |
| DateTime, server_default=func.now() | |
| ) | |
| updated_at: Mapped[datetime] = mapped_column( | |
| DateTime, server_default=func.now(), onupdate=func.now() | |
| ) | |
| # Relationships | |
| visit = relationship("Visit", back_populates="checklists") | |
| photos = relationship( | |
| "Photo", back_populates="checklist", cascade="all, delete-orphan" | |
| ) | |
| def __repr__(self) -> str: | |
| return ( | |
| f"<Checklist(id={self.id}, item='{self.item_name}', " | |
| f"checked={self.is_checked})>" | |
| ) | |
| # ββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # Pydantic Schemas | |
| # ββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class ChecklistCreate(BaseModel): | |
| """Schema for adding a checklist item to a visit.""" | |
| item_name: str = Field( | |
| ..., min_length=1, max_length=255, description="Checklist item name" | |
| ) | |
| is_checked: bool = Field(False, description="Whether the item is checked") | |
| notes: Optional[str] = Field(None, description="Additional notes") | |
| class ChecklistUpdate(BaseModel): | |
| """Schema for updating a checklist item (all fields optional).""" | |
| item_name: Optional[str] = Field(None, min_length=1, max_length=255) | |
| is_checked: Optional[bool] = None | |
| notes: Optional[str] = None | |
| class ChecklistResponse(BaseModel): | |
| """Schema for a single checklist item in API responses.""" | |
| model_config = ConfigDict(from_attributes=True) | |
| id: uuid.UUID | |
| visit_id: uuid.UUID | |
| item_name: str | |
| is_checked: bool | |
| notes: Optional[str] | |
| checked_at: Optional[datetime] | |
| created_at: datetime | |
| updated_at: datetime | |