|
1 | | -from sqlmodel import SQLModel, Field |
| 1 | +import uuid |
| 2 | + |
| 3 | +from pydantic import EmailStr |
| 4 | +from sqlmodel import Field, Relationship, SQLModel |
| 5 | + |
| 6 | + |
| 7 | +# Shared properties |
| 8 | +class UserBase(SQLModel): |
| 9 | + email: EmailStr = Field(unique=True, index=True, max_length=255) |
| 10 | + is_active: bool = True |
| 11 | + is_superuser: bool = False |
| 12 | + full_name: str | None = Field(default=None, max_length=255) |
| 13 | + |
| 14 | + |
| 15 | +# Properties to receive via API on creation |
| 16 | +class UserCreate(UserBase): |
| 17 | + password: str = Field(min_length=8, max_length=40) |
| 18 | + |
| 19 | + |
| 20 | +class UserRegister(SQLModel): |
| 21 | + email: EmailStr = Field(max_length=255) |
| 22 | + password: str = Field(min_length=8, max_length=40) |
| 23 | + full_name: str | None = Field(default=None, max_length=255) |
| 24 | + |
| 25 | + |
| 26 | +# Properties to receive via API on update, all are optional |
| 27 | +class UserUpdate(UserBase): |
| 28 | + email: EmailStr | None = Field(default=None, max_length=255) # type: ignore |
| 29 | + password: str | None = Field(default=None, min_length=8, max_length=40) |
| 30 | + |
| 31 | + |
| 32 | +class UserUpdateMe(SQLModel): |
| 33 | + full_name: str | None = Field(default=None, max_length=255) |
| 34 | + email: EmailStr | None = Field(default=None, max_length=255) |
| 35 | + |
| 36 | + |
| 37 | +class UpdatePassword(SQLModel): |
| 38 | + current_password: str = Field(min_length=8, max_length=40) |
| 39 | + new_password: str = Field(min_length=8, max_length=40) |
| 40 | + |
| 41 | + |
| 42 | +# Database model, database table inferred from class name |
| 43 | +class User(UserBase, table=True): |
| 44 | + id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True) |
| 45 | + hashed_password: str |
| 46 | + items: list["Item"] = Relationship(back_populates="owner", cascade_delete=True) |
| 47 | + |
| 48 | + |
| 49 | +# Properties to return via API, id is always required |
| 50 | +class UserPublic(UserBase): |
| 51 | + id: uuid.UUID |
| 52 | + |
| 53 | + |
| 54 | +class UsersPublic(SQLModel): |
| 55 | + data: list[UserPublic] |
| 56 | + count: int |
| 57 | + |
| 58 | + |
| 59 | +# Shared properties |
| 60 | +class ItemBase(SQLModel): |
| 61 | + title: str = Field(min_length=1, max_length=255) |
| 62 | + description: str | None = Field(default=None, max_length=255) |
| 63 | + |
| 64 | + |
| 65 | +# Properties to receive on item creation |
| 66 | +class ItemCreate(ItemBase): |
| 67 | + pass |
| 68 | + |
| 69 | + |
| 70 | +# Properties to receive on item update |
| 71 | +class ItemUpdate(ItemBase): |
| 72 | + title: str | None = Field(default=None, min_length=1, max_length=255) # type: ignore |
| 73 | + |
| 74 | + |
| 75 | +# Database model, database table inferred from class name |
| 76 | +class Item(ItemBase, table=True): |
| 77 | + id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True) |
| 78 | + title: str = Field(max_length=255) |
| 79 | + owner_id: uuid.UUID = Field( |
| 80 | + foreign_key="user.id", nullable=False, ondelete="CASCADE" |
| 81 | + ) |
| 82 | + owner: User | None = Relationship(back_populates="items") |
| 83 | + |
| 84 | + |
| 85 | +# Properties to return via API, id is always required |
| 86 | +class ItemPublic(ItemBase): |
| 87 | + id: uuid.UUID |
| 88 | + owner_id: uuid.UUID |
| 89 | + |
| 90 | + |
| 91 | +class ItemsPublic(SQLModel): |
| 92 | + data: list[ItemPublic] |
| 93 | + count: int |
2 | 94 |
|
3 | 95 |
|
4 | 96 | # Generic message |
|
0 commit comments