Initial commit: WealthWise financial analytics platform
This commit is contained in:
76
backend/app/models/user.py
Normal file
76
backend/app/models/user.py
Normal file
@@ -0,0 +1,76 @@
|
||||
"""User model for WealthWise.
|
||||
|
||||
This module defines the User database model for self-hosted authentication.
|
||||
Users can have multiple portfolios and are authenticated via JWT tokens.
|
||||
"""
|
||||
|
||||
from typing import TYPE_CHECKING, List, Optional
|
||||
from uuid import UUID
|
||||
|
||||
from sqlalchemy import Boolean, Column, String
|
||||
from sqlmodel import Field, Relationship, SQLModel
|
||||
|
||||
from app.models.base import BaseModel
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from app.models.portfolio import Portfolio
|
||||
|
||||
|
||||
class UserBase(SQLModel):
|
||||
"""Base User model with common attributes.
|
||||
|
||||
Attributes:
|
||||
email: User's unique email address (indexed)
|
||||
is_active: Whether the user account is active
|
||||
is_superuser: Whether user has admin privileges
|
||||
"""
|
||||
|
||||
email: str = Field(
|
||||
sa_column=Column(String, unique=True, index=True, nullable=False),
|
||||
description="User's unique email address",
|
||||
)
|
||||
is_active: bool = Field(
|
||||
default=True,
|
||||
sa_column=Column(Boolean, default=True, nullable=False),
|
||||
description="Whether the user account is active",
|
||||
)
|
||||
is_superuser: bool = Field(
|
||||
default=False,
|
||||
sa_column=Column(Boolean, default=False, nullable=False),
|
||||
description="Whether user has admin privileges",
|
||||
)
|
||||
|
||||
|
||||
class User(BaseModel, UserBase, table=True):
|
||||
"""User database model.
|
||||
|
||||
This model stores user information including authentication credentials
|
||||
and account status. Passwords are stored as bcrypt hashes.
|
||||
|
||||
Attributes:
|
||||
id: UUID primary key (inherited from BaseModel)
|
||||
email: Unique email address
|
||||
hashed_password: Bcrypt hashed password
|
||||
is_active: Account status
|
||||
is_superuser: Admin flag
|
||||
portfolios: Relationship to user's portfolios
|
||||
created_at: Timestamp (inherited from BaseModel)
|
||||
updated_at: Timestamp (inherited from BaseModel)
|
||||
"""
|
||||
|
||||
__tablename__ = "users"
|
||||
|
||||
hashed_password: str = Field(
|
||||
sa_column=Column(String, nullable=False),
|
||||
description="Bcrypt hashed password",
|
||||
)
|
||||
|
||||
# Relationships
|
||||
portfolios: List["Portfolio"] = Relationship(
|
||||
back_populates="user",
|
||||
sa_relationship_kwargs={"cascade": "all, delete-orphan"},
|
||||
)
|
||||
|
||||
class Config:
|
||||
"""Pydantic configuration."""
|
||||
arbitrary_types_allowed = True
|
||||
Reference in New Issue
Block a user