Files
sdb2-backend/app/models/user.py
JSC af20bc8724 Add database seeding functionality and enhance model relationships
- Implement initial data seeding for plans in the database.
- Create a new `seed_all_data` function to manage seeding process.
- Update `Sound` and `User` models to include relationships for `SoundPlayed` and `Stream`.
2025-07-25 11:44:47 +02:00

36 lines
1.3 KiB
Python

from datetime import datetime
from typing import TYPE_CHECKING
from sqlmodel import Field, Relationship
from app.models.base import BaseModel
if TYPE_CHECKING:
from app.models.plan import Plan
from app.models.playlist import Playlist
from app.models.sound_played import SoundPlayed
from app.models.stream import Stream
from app.models.user_oauth import UserOauth
class User(BaseModel, table=True):
"""Database model for a user."""
plan_id: int = Field(foreign_key="plan.id")
role: str = Field(nullable=False, default="user")
email: str = Field(unique=True, nullable=False)
name: str = Field(nullable=False)
picture: str | None = Field(default=None)
password_hash: str | None = Field(default=None)
is_active: bool = Field(nullable=False, default=True)
credits: int = Field(default=0, ge=0, nullable=False)
api_token: str | None = Field(unique=True, default=None)
api_token_expires_at: datetime | None = Field(default=None)
# relationships
oauths: list["UserOauth"] = Relationship(back_populates="user")
plan: "Plan" = Relationship(back_populates="users")
playlists: list["Playlist"] = Relationship(back_populates="user")
sounds_played: list["SoundPlayed"] = Relationship(back_populates="user")
streams: list["Stream"] = Relationship(back_populates="user")