2023-11-25 00:08:22 +01:00
|
|
|
from sqlmodel import Field, Relationship, SQLModel
|
|
|
|
|
|
|
|
|
|
|
|
# Shared properties
|
2024-02-28 17:24:24 -05:00
|
|
|
# TODO replace email str with EmailStr when sqlmodel supports it
|
2023-11-25 00:08:22 +01:00
|
|
|
class UserBase(SQLModel):
|
2024-02-28 17:24:24 -05:00
|
|
|
email: str = Field(unique=True, index=True)
|
2023-11-25 00:08:22 +01:00
|
|
|
is_active: bool = True
|
|
|
|
is_superuser: bool = False
|
2024-02-25 19:39:33 +01:00
|
|
|
full_name: str | None = None
|
2023-11-25 00:08:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Properties to receive via API on creation
|
|
|
|
class UserCreate(UserBase):
|
|
|
|
password: str
|
|
|
|
|
|
|
|
|
2024-02-28 17:24:24 -05:00
|
|
|
# TODO replace email str with EmailStr when sqlmodel supports it
|
2023-11-29 12:13:15 -05:00
|
|
|
class UserCreateOpen(SQLModel):
|
2024-02-28 17:24:24 -05:00
|
|
|
email: str
|
2023-11-29 12:13:15 -05:00
|
|
|
password: str
|
2024-02-25 19:39:33 +01:00
|
|
|
full_name: str | None = None
|
2023-11-29 12:13:15 -05:00
|
|
|
|
|
|
|
|
2023-11-25 00:08:22 +01:00
|
|
|
# Properties to receive via API on update, all are optional
|
2024-02-28 17:24:24 -05:00
|
|
|
# TODO replace email str with EmailStr when sqlmodel supports it
|
2023-11-25 00:08:22 +01:00
|
|
|
class UserUpdate(UserBase):
|
2024-03-10 14:47:21 -05:00
|
|
|
email: str | None = None # type: ignore
|
2024-02-25 19:39:33 +01:00
|
|
|
password: str | None = None
|
2023-11-25 00:08:22 +01:00
|
|
|
|
|
|
|
|
2024-02-28 17:24:24 -05:00
|
|
|
# TODO replace email str with EmailStr when sqlmodel supports it
|
2024-02-17 21:29:15 +01:00
|
|
|
class UserUpdateMe(SQLModel):
|
2024-02-25 19:39:33 +01:00
|
|
|
full_name: str | None = None
|
2024-02-28 17:24:24 -05:00
|
|
|
email: str | None = None
|
2023-12-27 14:39:42 -05:00
|
|
|
|
2024-02-17 21:29:15 +01:00
|
|
|
|
2024-02-21 15:55:19 -05:00
|
|
|
class UpdatePassword(SQLModel):
|
|
|
|
current_password: str
|
|
|
|
new_password: str
|
|
|
|
|
|
|
|
|
2023-11-25 00:08:22 +01:00
|
|
|
# Database model, database table inferred from class name
|
|
|
|
class User(UserBase, table=True):
|
2024-02-25 19:39:33 +01:00
|
|
|
id: int | None = Field(default=None, primary_key=True)
|
2023-11-25 00:08:22 +01:00
|
|
|
hashed_password: str
|
|
|
|
items: list["Item"] = Relationship(back_populates="owner")
|
|
|
|
|
|
|
|
|
|
|
|
# Properties to return via API, id is always required
|
|
|
|
class UserOut(UserBase):
|
|
|
|
id: int
|
|
|
|
|
|
|
|
|
2024-02-25 10:04:47 -05:00
|
|
|
class UsersOut(SQLModel):
|
|
|
|
data: list[UserOut]
|
|
|
|
count: int
|
|
|
|
|
|
|
|
|
2023-11-25 00:08:22 +01:00
|
|
|
# Shared properties
|
|
|
|
class ItemBase(SQLModel):
|
|
|
|
title: str
|
2024-02-25 19:39:33 +01:00
|
|
|
description: str | None = None
|
2023-11-25 00:08:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Properties to receive on item creation
|
|
|
|
class ItemCreate(ItemBase):
|
|
|
|
title: str
|
|
|
|
|
|
|
|
|
|
|
|
# Properties to receive on item update
|
|
|
|
class ItemUpdate(ItemBase):
|
2024-03-10 14:47:21 -05:00
|
|
|
title: str | None = None # type: ignore
|
2023-11-25 00:08:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Database model, database table inferred from class name
|
|
|
|
class Item(ItemBase, table=True):
|
2024-02-25 19:39:33 +01:00
|
|
|
id: int | None = Field(default=None, primary_key=True)
|
2023-11-25 00:08:22 +01:00
|
|
|
title: str
|
2024-02-25 19:39:33 +01:00
|
|
|
owner_id: int | None = Field(default=None, foreign_key="user.id", nullable=False)
|
|
|
|
owner: User | None = Relationship(back_populates="items")
|
2023-11-25 00:08:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Properties to return via API, id is always required
|
|
|
|
class ItemOut(ItemBase):
|
|
|
|
id: int
|
2024-02-25 10:04:47 -05:00
|
|
|
owner_id: int
|
|
|
|
|
|
|
|
|
|
|
|
class ItemsOut(SQLModel):
|
|
|
|
data: list[ItemOut]
|
|
|
|
count: int
|
2023-11-25 00:08:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Generic message
|
2024-02-17 21:29:15 +01:00
|
|
|
class Message(SQLModel):
|
2023-12-27 19:06:47 +01:00
|
|
|
message: str
|
2023-11-25 00:08:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
# JSON payload containing access token
|
2024-02-17 21:29:15 +01:00
|
|
|
class Token(SQLModel):
|
2023-11-25 00:08:22 +01:00
|
|
|
access_token: str
|
2023-12-27 19:06:47 +01:00
|
|
|
token_type: str = "bearer"
|
2023-11-25 00:08:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Contents of JWT token
|
2024-02-17 21:29:15 +01:00
|
|
|
class TokenPayload(SQLModel):
|
2024-02-25 19:39:33 +01:00
|
|
|
sub: int | None = None
|
2023-12-27 19:06:47 +01:00
|
|
|
|
2023-12-27 14:39:42 -05:00
|
|
|
|
2024-02-17 21:29:15 +01:00
|
|
|
class NewPassword(SQLModel):
|
2023-12-27 19:06:47 +01:00
|
|
|
token: str
|
|
|
|
new_password: str
|