11import uuid
22from typing import Optional
3- from pydantic import EmailStr
3+ from pydantic import BaseModel , EmailStr
4+ from pydantic import Field as F
45from sqlmodel import Field , Relationship , SQLModel , Column , TIMESTAMP , text , DATE
56from datetime import datetime
7+ from sqlalchemy import JSON
8+ from sqlalchemy import Column as Col
69import enum
710# Shared properties
811class UserBase (SQLModel ):
@@ -161,6 +164,8 @@ class Patient(PatientBase, table=True):
161164 nullable = False ,
162165 server_default = text ("CURRENT_TIMESTAMP" ),
163166 ))
167+ menus : list ["Menu" ] = Relationship (back_populates = "owner" , cascade_delete = True )
168+
164169
165170# Properties to return via API, id is always required
166171class PatientPublic (PatientBase ):
@@ -170,3 +175,61 @@ class PatientPublic(PatientBase):
170175class PatientsPublic (SQLModel ):
171176 data : list [PatientPublic ]
172177 count : int
178+
179+ class Menu_B (BaseModel ):
180+ def __init__ (self ):
181+ self .data : dict = F (default = self .menu_builder ())
182+
183+ def menu_builder (self ):
184+ days = ["Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday" , "Saturday" , "Sunday" ]
185+ mills = ["breakfast" , "mid-morning" , "lunch" ,"mid-afternoon" , "dinner" ]
186+ new_menu = {}
187+ for day in days :
188+ new_menu .update ({day :{mill :{} for mill in mills }})
189+ self .data = new_menu
190+ return self .data
191+
192+ # Shared properties
193+ class MenuBase (SQLModel ,Menu_B ):
194+ title : str = Field (min_length = 1 , max_length = 255 )
195+ description : str | None = Field (default = None , max_length = 255 )
196+ data : dict = Field (sa_column = Col (JSON ), default_factory = dict )
197+ current : bool = Field ()
198+
199+
200+ # Properties to receive on item creation
201+ class MenuCreate (MenuBase ):
202+ pass
203+
204+
205+ # Properties to receive on item update
206+ class MenuUpdate (MenuBase ):
207+ title : str | None = Field (default = None , min_length = 1 , max_length = 255 ) # type: ignore
208+ description : str | None = Field (default = None , max_length = 255 )
209+ data : dict = Field (sa_column = Column (JSON ), default_factory = dict )
210+ current : bool = Field ()
211+
212+ # Database model, database table inferred from class name
213+ class Menu (MenuBase , table = True ):
214+ id : uuid .UUID = Field (default_factory = uuid .uuid4 , primary_key = True )
215+ title : str = Field (max_length = 255 )
216+ created_datetime : Optional [datetime ] = Field (sa_column = Column (
217+ TIMESTAMP (timezone = True ),
218+ nullable = False ,
219+ server_default = text ("CURRENT_TIMESTAMP" ),
220+ ))
221+ owner_id : uuid .UUID = Field (
222+ foreign_key = "patient.id" , nullable = False , ondelete = "CASCADE"
223+ )
224+ owner : Patient | None = Relationship (back_populates = "menus" )
225+
226+
227+ # Properties to return via API, id is always required
228+ class MenuPublic (MenuBase ):
229+ pass
230+
231+
232+ class MenusPublic (SQLModel ):
233+ data : list [MenuPublic ]
234+ count : int
235+
0 commit comments