Skip to content

Commit 0ac2770

Browse files
committed
menu models
1 parent 5660948 commit 0ac2770

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-1
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""Add menu_b class Menu base model
2+
3+
Revision ID: 32ba675b8ec6
4+
Revises: 1cabfca3d8c8
5+
Create Date: 2025-03-05 08:58:47.583292
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
import sqlmodel.sql.sqltypes
11+
12+
13+
# revision identifiers, used by Alembic.
14+
revision = '32ba675b8ec6'
15+
down_revision = '1cabfca3d8c8'
16+
branch_labels = None
17+
depends_on = None
18+
19+
20+
def upgrade():
21+
# ### commands auto generated by Alembic - please adjust! ###
22+
op.create_table('menu',
23+
sa.Column('description', sqlmodel.sql.sqltypes.AutoString(length=255), nullable=True),
24+
sa.Column('data', sa.JSON(), nullable=True),
25+
sa.Column('current', sa.Boolean(), nullable=False),
26+
sa.Column('id', sa.Uuid(), nullable=False),
27+
sa.Column('title', sqlmodel.sql.sqltypes.AutoString(length=255), nullable=False),
28+
sa.Column('created_datetime', sa.TIMESTAMP(timezone=True), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False),
29+
sa.Column('owner_id', sa.Uuid(), nullable=False),
30+
sa.ForeignKeyConstraint(['owner_id'], ['patient.id'], ondelete='CASCADE'),
31+
sa.PrimaryKeyConstraint('id')
32+
)
33+
# ### end Alembic commands ###
34+
35+
36+
def downgrade():
37+
# ### commands auto generated by Alembic - please adjust! ###
38+
op.drop_table('menu')
39+
# ### end Alembic commands ###

backend/app/models.py

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import uuid
22
from typing import Optional
3-
from pydantic import EmailStr
3+
from pydantic import BaseModel, EmailStr
4+
from pydantic import Field as F
45
from sqlmodel import Field, Relationship, SQLModel, Column, TIMESTAMP, text, DATE
56
from datetime import datetime
7+
from sqlalchemy import JSON
8+
from sqlalchemy import Column as Col
69
import enum
710
# Shared properties
811
class 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
166171
class PatientPublic(PatientBase):
@@ -170,3 +175,61 @@ class PatientPublic(PatientBase):
170175
class 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

Comments
 (0)