Skip to content

users#

User Database Model

AccessToken #

Bases: SQLAlchemyBaseAccessTokenTableUUID, UpdatedAtMixin, Base

FastAPI Users - Access Token Model

Source code in zoo/models/users.py
class AccessToken(SQLAlchemyBaseAccessTokenTableUUID, UpdatedAtMixin, Base):
    """
    FastAPI Users - Access Token Model
    """

    __tablename__ = "access_token"

User #

Bases: SQLAlchemyBaseUserTableUUID, CreatedUpdatedMixin, Base

FastAPI Users - User Model

Source code in zoo/models/users.py
class User(SQLAlchemyBaseUserTableUUID, CreatedUpdatedMixin, Base):
    """
    FastAPI Users - User Model
    """

UserManager #

Bases: UUIDIDMixin, BaseUserManager[User, UUID]

UserManager for FastAPI Users

Source code in zoo/models/users.py
class UserManager(UUIDIDMixin, BaseUserManager[User, uuid.UUID]):
    """
    UserManager for FastAPI Users
    """

bootstrap_fastapi_users(app) #

Bootstrap the application with FastAPI Users Routers

Parameters:

Name Type Description Default
app FastAPI
required

Returns:

Type Description
None
Source code in zoo/models/users.py
def bootstrap_fastapi_users(app: FastAPI) -> None:
    """
    Bootstrap the application with FastAPI Users Routers

    Parameters
    ----------
    app: FastAPI

    Returns
    -------
    None
    """
    app.include_router(
        fastapi_users.get_auth_router(auth_backend),
        prefix=f"/{auth_endpoint}/{jwt_endpoint}",
        tags=[auth_endpoint],
    )
    app.include_router(
        fastapi_users.get_auth_router(cookie_auth_backend),
        prefix=f"/{auth_endpoint}/{cookie_endpoint}",
        tags=[auth_endpoint],
    )
    app.include_router(
        fastapi_users.get_register_router(UserRead, UserCreate),
        prefix=f"/{auth_endpoint}",
        tags=[auth_endpoint],
    )

create_user(email, password, *, is_superuser=False) async #

Create a user

Parameters:

Name Type Description Default
email str

User email

required
password str

User password

required
is_superuser bool

Is the user a superuser

False

Returns:

Type Description
User

Raises:

Type Description
UserAlreadyExists

If the user already exists

Source code in zoo/models/users.py
async def create_user(email: str, password: str, *, is_superuser: bool = False) -> User:
    """
    Create a user

    Parameters
    ----------
    email: str
        User email
    password: str
        User password
    is_superuser: bool
        Is the user a superuser

    Returns
    -------
    User

    Raises
    ------
    UserAlreadyExists
        If the user already exists
    """
    async with get_async_session_context() as session:  # pragma: no cover
        async with get_user_db_context(session) as user_db:
            async with get_user_manager_context(user_db) as user_manager:
                user = await user_manager.create(
                    UserCreate(
                        email=email, password=password, is_superuser=is_superuser
                    )
                )
                return user

get_access_token_db(session=Depends(get_async_session)) async #

Yield a SQLAlchemyAccessTokenDatabase

Source code in zoo/models/users.py
async def get_access_token_db(
    session: AsyncSession = Depends(get_async_session),
) -> AsyncGenerator[SQLAlchemyAccessTokenDatabase[AccessToken], None]:
    """
    Yield a SQLAlchemyAccessTokenDatabase
    """
    yield SQLAlchemyAccessTokenDatabase[AccessToken](
        access_token_table=AccessToken, session=session
    )

get_database_strategy(access_token_db=Depends(get_access_token_db)) #

Get a DatabaseStrategy using the AccessTokenDatabase

Source code in zoo/models/users.py
def get_database_strategy(
    access_token_db: AccessTokenDatabase = Depends(get_access_token_db),
) -> DatabaseStrategy:
    """
    Get a DatabaseStrategy using the AccessTokenDatabase
    """
    return DatabaseStrategy(
        database=access_token_db, lifetime_seconds=app_config.JWT_EXPIRATION
    )

get_jwt_strategy() #

Get a DatabaseStrategy using the AccessTokenDatabase

Source code in zoo/models/users.py
def get_jwt_strategy() -> JWTStrategy:
    """
    Get a DatabaseStrategy using the AccessTokenDatabase
    """
    return JWTStrategy(
        secret=app_config.DATABASE_SECRET, lifetime_seconds=app_config.JWT_EXPIRATION
    )

get_user_db(session=Depends(get_async_session)) async #

Yield a SQLModelUserDatabaseAsync

Source code in zoo/models/users.py
async def get_user_db(
    session: AsyncSession = Depends(get_async_session),
) -> AsyncGenerator[SQLAlchemyUserDatabase[User, Any], None]:
    """
    Yield a SQLModelUserDatabaseAsync
    """
    yield SQLAlchemyUserDatabase(user_table=User, session=session)

get_user_manager(user_db=Depends(get_user_db)) async #

Yield a UserManager

Source code in zoo/models/users.py
async def get_user_manager(
    user_db: SQLAlchemyUserDatabase = Depends(get_user_db),
) -> AsyncGenerator[UserManager, None]:
    """
    Yield a UserManager
    """
    yield UserManager(user_db=user_db)