2024-07-22 17:49:02 -05:00
|
|
|
import uuid
|
|
|
|
|
2024-06-18 19:20:39 -05:00
|
|
|
from pydantic import EmailStr
|
2023-11-25 00:08:22 +01:00
|
|
|
from sqlmodel import Field, Relationship, SQLModel
|
|
|
|
|
|
|
|
|
|
|
|
# Shared properties
|
|
|
|
class UserBase(SQLModel):
|
2024-06-18 19:20:39 -05:00
|
|
|
email: EmailStr = Field(unique=True, index=True, max_length=255)
|
2023-11-25 00:08:22 +01:00
|
|
|
is_active: bool = True
|
|
|
|
is_superuser: bool = False
|
2024-06-18 19:20:39 -05:00
|
|
|
full_name: str | None = Field(default=None, max_length=255)
|
2023-11-25 00:08:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Properties to receive via API on creation
|
|
|
|
class UserCreate(UserBase):
|
2024-06-18 19:20:39 -05:00
|
|
|
password: str = Field(min_length=8, max_length=40)
|
2023-11-25 00:08:22 +01:00
|
|
|
|
|
|
|
|
2024-04-01 22:53:33 -05:00
|
|
|
class UserRegister(SQLModel):
|
2024-06-18 19:20:39 -05:00
|
|
|
email: EmailStr = Field(max_length=255)
|
|
|
|
password: str = Field(min_length=8, max_length=40)
|
|
|
|
full_name: str | None = Field(default=None, max_length=255)
|
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
|
|
|
|
class UserUpdate(UserBase):
|
2024-06-18 19:20:39 -05:00
|
|
|
email: EmailStr | None = Field(default=None, max_length=255) # type: ignore
|
|
|
|
password: str | None = Field(default=None, min_length=8, max_length=40)
|
2023-11-25 00:08:22 +01:00
|
|
|
|
|
|
|
|
2024-02-17 21:29:15 +01:00
|
|
|
class UserUpdateMe(SQLModel):
|
2024-06-18 19:20:39 -05:00
|
|
|
full_name: str | None = Field(default=None, max_length=255)
|
|
|
|
email: EmailStr | None = Field(default=None, max_length=255)
|
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):
|
2024-06-18 19:20:39 -05:00
|
|
|
current_password: str = Field(min_length=8, max_length=40)
|
|
|
|
new_password: str = Field(min_length=8, max_length=40)
|
2024-02-21 15:55:19 -05:00
|
|
|
|
|
|
|
|
2023-11-25 00:08:22 +01:00
|
|
|
# Database model, database table inferred from class name
|
|
|
|
class User(UserBase, table=True):
|
2024-07-22 17:49:02 -05:00
|
|
|
id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True)
|
2023-11-25 00:08:22 +01:00
|
|
|
hashed_password: str
|
2024-08-01 11:44:00 -05:00
|
|
|
items: list["Item"] = Relationship(back_populates="owner", cascade_delete=True)
|
2023-11-25 00:08:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Properties to return via API, id is always required
|
2024-04-06 16:53:49 -05:00
|
|
|
class UserPublic(UserBase):
|
2024-07-22 17:49:02 -05:00
|
|
|
id: uuid.UUID
|
2023-11-25 00:08:22 +01:00
|
|
|
|
|
|
|
|
2024-04-06 16:53:49 -05:00
|
|
|
class UsersPublic(SQLModel):
|
|
|
|
data: list[UserPublic]
|
2024-02-25 10:04:47 -05:00
|
|
|
count: int
|
|
|
|
|
|
|
|
|
2023-11-25 00:08:22 +01:00
|
|
|
# Shared properties
|
|
|
|
class ItemBase(SQLModel):
|
2024-06-18 19:20:39 -05:00
|
|
|
title: str = Field(min_length=1, max_length=255)
|
|
|
|
description: str | None = Field(default=None, max_length=255)
|
2023-11-25 00:08:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Properties to receive on item creation
|
|
|
|
class ItemCreate(ItemBase):
|
2024-06-18 19:20:39 -05:00
|
|
|
title: str = Field(min_length=1, max_length=255)
|
2023-11-25 00:08:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Properties to receive on item update
|
|
|
|
class ItemUpdate(ItemBase):
|
2024-06-18 19:20:39 -05:00
|
|
|
title: str | None = Field(default=None, min_length=1, max_length=255) # type: ignore
|
2023-11-25 00:08:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Database model, database table inferred from class name
|
|
|
|
class Item(ItemBase, table=True):
|
2024-07-22 17:49:02 -05:00
|
|
|
id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True)
|
2024-06-18 19:20:39 -05:00
|
|
|
title: str = Field(max_length=255)
|
2024-08-01 11:44:00 -05:00
|
|
|
owner_id: uuid.UUID = Field(
|
|
|
|
foreign_key="user.id", nullable=False, ondelete="CASCADE"
|
|
|
|
)
|
2024-02-25 19:39:33 +01:00
|
|
|
owner: User | None = Relationship(back_populates="items")
|
2023-11-25 00:08:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Properties to return via API, id is always required
|
2024-04-06 16:53:49 -05:00
|
|
|
class ItemPublic(ItemBase):
|
2024-07-22 17:49:02 -05:00
|
|
|
id: uuid.UUID
|
|
|
|
owner_id: uuid.UUID
|
2024-02-25 10:04:47 -05:00
|
|
|
|
|
|
|
|
2024-04-06 16:53:49 -05:00
|
|
|
class ItemsPublic(SQLModel):
|
|
|
|
data: list[ItemPublic]
|
2024-02-25 10:04:47 -05:00
|
|
|
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-07-22 17:49:02 -05:00
|
|
|
sub: str | 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
|
2024-06-18 19:20:39 -05:00
|
|
|
new_password: str = Field(min_length=8, max_length=40)
|