diff options
Diffstat (limited to 'app/models/user.py')
| -rw-r--r-- | app/models/user.py | 33 |
1 files changed, 25 insertions, 8 deletions
diff --git a/app/models/user.py b/app/models/user.py index 8dbe419..442173c 100644 --- a/app/models/user.py +++ b/app/models/user.py @@ -1,14 +1,18 @@ +from __future__ import annotations + +from typing import TYPE_CHECKING + from fastapi import Depends from sqlalchemy import Boolean, Integer, String from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.future import select -from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column - -from app.utils.db import get_async_session +from sqlalchemy.orm import Mapped, mapped_column, relationship, selectinload +from app.utils.db import Base, get_async_session -class Base(DeclarativeBase): - pass +if TYPE_CHECKING: + from .integrations import UserIntegration + from .profile import Profile class User(Base): @@ -21,13 +25,19 @@ class User(Base): google_id: Mapped[str | None] = mapped_column( String(255), unique=True, nullable=True ) - avatar_file: Mapped[str | None] = mapped_column(String(255), nullable=True) - banner_file: Mapped[str | None] = mapped_column(String(255), nullable=True) + description: Mapped[str | None] = mapped_column(String(250), nullable=True) premium: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False) is_banned: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False) is_moderator: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False) token_version: Mapped[int] = mapped_column(Integer, default=0, nullable=False) + profile: Mapped["Profile"] = relationship( + "Profile", back_populates="user", uselist=False, lazy="selectin" + ) + integrations: Mapped["UserIntegration"] = relationship( + "UserIntegration", back_populates="user", uselist=False, lazy="selectin" + ) + @classmethod async def get_user_by_email( cls, email: str, session: AsyncSession = Depends(get_async_session) @@ -39,7 +49,14 @@ class User(Base): async def get_user_by_username( cls, username: str, session: AsyncSession = Depends(get_async_session) ): - result = await session.execute(select(cls).where(cls.username == username)) + result = await session.execute( + select(cls) + .options( + selectinload(cls.profile), + selectinload(cls.integrations), + ) + .where(cls.username == username) + ) return result.scalars().first() @classmethod |
