11import uuid
2+ from enum import Enum
23
34from pydantic import EmailStr
45from sqlmodel import Field , Relationship , SQLModel
@@ -44,6 +45,7 @@ class User(UserBase, table=True):
4445 id : uuid .UUID = Field (default_factory = uuid .uuid4 , primary_key = True )
4546 hashed_password : str
4647 items : list ["Item" ] = Relationship (back_populates = "owner" , cascade_delete = True )
48+ todos : list ["Todo" ] = Relationship (back_populates = "owner" , cascade_delete = True )
4749
4850
4951# Properties to return via API, id is always required
@@ -113,26 +115,71 @@ class NewPassword(SQLModel):
113115 token : str
114116 new_password : str = Field (min_length = 8 , max_length = 40 )
115117
118+ # Shared properties
119+ class StatusEnum (str , Enum ):
120+ pending = "pending"
121+ completed = "completed"
122+ in_progress = "in_progress"
123+
116124class TodoBase (SQLModel ):
117125 title : str = Field (min_length = 1 , max_length = 255 )
118- desc : str | None = Field (default = None , max_length = 255 )
126+ desc : str = Field (max_length = 255 )
119127
120128# Table Todo
121129class Todo (TodoBase , table = True ):
122130 id : uuid .UUID = Field (default_factory = uuid .uuid4 , primary_key = True )
123- title : str = Field (max_length = 255 )
124- desc : str | None = Field (default = None , max_length = 255 )
125- user_id : uuid .UUID = Field (
131+ owner_id : uuid .UUID = Field (
126132 foreign_key = "user.id" , nullable = False , ondelete = "CASCADE"
127133 )
128- status : str = Field (max_length = 20 )
134+ status : str = Field (max_length = 255 )
135+ owner : User | None = Relationship (back_populates = "todos" )
136+ subtodos : list ["SubTodo" ] = Relationship (back_populates = "todo" )
137+
138+ class TodoCreate (TodoBase ):
139+ pass
140+
141+ # Properties to receive on item update
142+ class TodoUpdate (TodoBase ):
143+ title : str | None = Field (default = None , min_length = 1 , max_length = 255 ) # type: ignore
144+ desc : str | None = Field (default = None , max_length = 255 )
145+ status : str = Field (max_length = 255 )
146+
147+ class TodoPublic (TodoBase ):
148+ id : uuid .UUID
149+ owner_id : uuid .UUID
150+ status : StatusEnum
151+
152+ class TodosPublic (SQLModel ):
153+ data : list [TodoPublic ]
154+ count : int
129155
130156# Table SubTodo
131- class SubTodo (TodoBase , table = True ):
157+ class SubTodoBase (SQLModel ):
158+ title : str = Field (min_length = 1 , max_length = 255 )
159+ desc : str = Field (max_length = 255 )
160+
161+ class SubTodo (SubTodoBase , table = True ):
132162 id : uuid .UUID = Field (default_factory = uuid .uuid4 , primary_key = True )
133- title : str = Field (max_length = 255 )
134- desc : str | None = Field (default = None , max_length = 255 )
135163 todo_id : uuid .UUID = Field (
136164 foreign_key = "todo.id" , nullable = False , ondelete = "CASCADE"
137165 )
138- status : str = Field (max_length = 20 )
166+ status : str = Field (max_length = 255 )
167+ todo : Todo | None = Relationship (back_populates = "subtodos" )
168+
169+ class SubTodoCreate (SubTodoBase ):
170+ pass
171+
172+ # Properties to receive on item update
173+ class SubTodoUpdate (SubTodoBase ):
174+ title : str | None = Field (default = None , min_length = 1 , max_length = 255 ) # type: ignore
175+ desc : str | None = Field (default = None , max_length = 255 )
176+ status : StatusEnum | None = Field (default = None )
177+
178+ class SubTodoPublic (SubTodoBase ):
179+ id : uuid .UUID
180+ todo_id : uuid .UUID
181+ status : StatusEnum
182+
183+ class SubTodosPublic (SQLModel ):
184+ data : list [SubTodoPublic ]
185+ count : int
0 commit comments