File size: 3,376 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
"""
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