🚚 Refactor and simplify backend file structure (#609)
This commit is contained in:

committed by
GitHub

parent
a065f9c9e8
commit
73b2884057
114
src/backend/app/models.py
Normal file
114
src/backend/app/models.py
Normal file
@@ -0,0 +1,114 @@
|
||||
from typing import Union
|
||||
|
||||
from pydantic import EmailStr
|
||||
from sqlmodel import Field, Relationship, SQLModel
|
||||
|
||||
|
||||
# Shared properties
|
||||
class UserBase(SQLModel):
|
||||
email: EmailStr = Field(unique=True, index=True)
|
||||
is_active: bool = True
|
||||
is_superuser: bool = False
|
||||
full_name: Union[str, None] = None
|
||||
|
||||
|
||||
# Properties to receive via API on creation
|
||||
class UserCreate(UserBase):
|
||||
password: str
|
||||
|
||||
|
||||
class UserCreateOpen(SQLModel):
|
||||
email: EmailStr
|
||||
password: str
|
||||
full_name: Union[str, None] = None
|
||||
|
||||
|
||||
# Properties to receive via API on update, all are optional
|
||||
class UserUpdate(UserBase):
|
||||
email: Union[EmailStr, None] = None
|
||||
password: Union[str, None] = None
|
||||
|
||||
|
||||
class UserUpdateMe(SQLModel):
|
||||
full_name: Union[str, None] = None
|
||||
email: Union[EmailStr, None] = None
|
||||
|
||||
|
||||
class UpdatePassword(SQLModel):
|
||||
current_password: str
|
||||
new_password: str
|
||||
|
||||
|
||||
# Database model, database table inferred from class name
|
||||
class User(UserBase, table=True):
|
||||
id: Union[int, None] = Field(default=None, primary_key=True)
|
||||
hashed_password: str
|
||||
items: list["Item"] = Relationship(back_populates="owner")
|
||||
|
||||
|
||||
# Properties to return via API, id is always required
|
||||
class UserOut(UserBase):
|
||||
id: int
|
||||
|
||||
|
||||
class UsersOut(SQLModel):
|
||||
data: list[UserOut]
|
||||
count: int
|
||||
|
||||
|
||||
# Shared properties
|
||||
class ItemBase(SQLModel):
|
||||
title: str
|
||||
description: Union[str, None] = None
|
||||
|
||||
|
||||
# Properties to receive on item creation
|
||||
class ItemCreate(ItemBase):
|
||||
title: str
|
||||
|
||||
|
||||
# Properties to receive on item update
|
||||
class ItemUpdate(ItemBase):
|
||||
title: Union[str, None] = None
|
||||
|
||||
|
||||
# Database model, database table inferred from class name
|
||||
class Item(ItemBase, table=True):
|
||||
id: Union[int, None] = Field(default=None, primary_key=True)
|
||||
title: str
|
||||
owner_id: Union[int, None] = Field(
|
||||
default=None, foreign_key="user.id", nullable=False
|
||||
)
|
||||
owner: Union[User, None] = Relationship(back_populates="items")
|
||||
|
||||
|
||||
# Properties to return via API, id is always required
|
||||
class ItemOut(ItemBase):
|
||||
id: int
|
||||
owner_id: int
|
||||
|
||||
|
||||
class ItemsOut(SQLModel):
|
||||
data: list[ItemOut]
|
||||
count: int
|
||||
|
||||
|
||||
# Generic message
|
||||
class Message(SQLModel):
|
||||
message: str
|
||||
|
||||
|
||||
# JSON payload containing access token
|
||||
class Token(SQLModel):
|
||||
access_token: str
|
||||
token_type: str = "bearer"
|
||||
|
||||
|
||||
# Contents of JWT token
|
||||
class TokenPayload(SQLModel):
|
||||
sub: Union[int, None] = None
|
||||
|
||||
|
||||
class NewPassword(SQLModel):
|
||||
token: str
|
||||
new_password: str
|
Reference in New Issue
Block a user