-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
83 lines (65 loc) · 2.52 KB
/
models.py
File metadata and controls
83 lines (65 loc) · 2.52 KB
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
from datetime import datetime
from enum import Enum
from uuid import UUID, uuid4
from pydantic import BaseModel, Field, field_validator
# ============= EVENT MODELS =============
class EventCreate(BaseModel):
"""
What the user sends when creating an event.
Example: {"name": "Concert", "date_time": "2026-12-31T20:00:00", ...}
"""
name: str = Field(..., min_length=1, description="Event name")
date_time: datetime = Field(..., description="When the event happens")
venue: str = Field(..., min_length=1, description="Where the event happens")
total_seats: int = Field(..., gt=0, description="How many seats total")
@field_validator('date_time')
@classmethod
def check_future_date(cls, v: datetime) -> datetime:
"""Make sure event is in the future"""
# Handle both timezone-aware and timezone-naive datetimes
from datetime import timezone
# Make the comparison timezone-aware if needed
now = datetime.now(timezone.utc) if v.tzinfo else datetime.now()
if v < now:
raise ValueError('Event must be in the future')
return v
class Event(BaseModel):
"""
The complete event stored in our system.
Has everything from EventCreate PLUS id, available_seats, created_at
"""
id: UUID = Field(default_factory=uuid4)
name: str
date_time: datetime
venue: str
total_seats: int
available_seats: int # Decreases when people book
created_at: datetime = Field(default_factory=datetime.now)
# ============= BOOKING MODELS =============
class BookingStatus(str, Enum):
"""Possible booking states"""
CONFIRMED = "confirmed"
CANCELLED = "cancelled"
class BookingCreate(BaseModel):
"""
What the user sends when making a booking.
Example: {"event_id": "abc-123...", "num_seats": 2}
"""
event_id: UUID = Field(..., description="Which event to book")
num_seats: int = Field(..., gt=0, description="How many seats to book")
class Booking(BaseModel):
"""
The complete booking stored in our system.
"""
booking_id: UUID = Field(default_factory=uuid4)
event_id: UUID
num_seats: int
status: BookingStatus = BookingStatus.CONFIRMED
created_at: datetime = Field(default_factory=datetime.now)
updated_at: datetime = Field(default_factory=datetime.now)
# ============= API RESPONSE MODELS =============
class ApiResponse(BaseModel):
"""Standard response format for all API calls"""
success: bool
message: str
data: dict | list | None = None