60 lines
2.3 KiB
Python

from typing import Optional, List
from uuid import UUID
from datetime import datetime
from sqlmodel import SQLModel, Field, Relationship
class User(SQLModel, table=True):
__tablename__ = 'user'
id: int = Field(primary_key=True)
uuid: UUID = Field(nullable=False, unique=True)
user_role_id: int = Field(foreign_key="user_role.id", nullable=False)
shop_id: Optional[int] = Field(foreign_key="shop.id")
username: str = Field(max_length=64, nullable=False, unique=True)
email: str = Field(max_length=128, nullable=False, unique=True)
password: str = Field(max_length=60, nullable=False)
first_name: Optional[str] = Field(max_length=64)
last_name: Optional[str] = Field(max_length=64)
phone_number: str = Field(max_length=15, nullable=False)
profile_picture: Optional[str] = Field(max_length=100)
created_at: datetime = Field(default_factory=datetime.utcnow, nullable=False)
updated_at: datetime = Field(default_factory=datetime.utcnow, nullable=False)
last_login: Optional[datetime] = Field(default_factory=datetime.utcnow)
owned_shops: List["Shop"] = Relationship(back_populates="owner")
registered_shop: List["Shop"] = Relationship(back_populates="registered_users")
user_assigned_role: Optional["UserRole"] = Relationship(back_populates="role_users", sa_relationship_kwargs={"foreign_keys": "[User.user_role_id]"})
preferences: Optional["UserPreferences"] = Relationship(back_populates="user")
statistics: Optional["UserStatistics"] = Relationship(back_populates="user_statistics")
class UserPreferences(SQLModel, table=True):
__tablename__ = 'user_preferences'
user_id: int = Field(foreign_key="user.id", primary_key=True)
user: Optional["User"] = Relationship(back_populates="preferences")
class UserRole(SQLModel, table=True):
__tablename__ = 'user_role'
id: int = Field(primary_key=True)
name: str = Field(nullable=False, unique=True, max_length=50)
role_users: List["User"] = Relationship(back_populates="user_assigned_role")
class UserStatistics(SQLModel, table=True):
__tablename__ = "user_statistics"
user_id: int = Field(foreign_key="user.id", primary_key=True)
total_spend: Optional[float] = Field(default=None)
user: Optional["User"] = Relationship(back_populates="statistics")
__all__ = ["User", "UserPreferences", "UserRole"]