mirror of
https://github.com/maxdorninger/MediaManager.git
synced 2026-04-17 15:43:28 +02:00
This PR enables the ruff rule for return type annotations (ANN), and adds the ty package for type checking.
46 lines
1.4 KiB
Python
46 lines
1.4 KiB
Python
from collections.abc import AsyncGenerator
|
|
from typing import Optional
|
|
|
|
from fastapi import Depends
|
|
from fastapi_users.db import (
|
|
SQLAlchemyBaseOAuthAccountTableUUID,
|
|
SQLAlchemyBaseUserTableUUID,
|
|
SQLAlchemyUserDatabase,
|
|
)
|
|
from sqlalchemy import String
|
|
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from media_manager.config import MediaManagerConfig
|
|
from media_manager.database import Base, build_db_url
|
|
|
|
|
|
class OAuthAccount(SQLAlchemyBaseOAuthAccountTableUUID, Base):
|
|
access_token: Mapped[str] = mapped_column(String(length=4096), nullable=False)
|
|
refresh_token: Mapped[Optional[str]] = mapped_column(
|
|
String(length=4096), nullable=True
|
|
)
|
|
|
|
|
|
class User(SQLAlchemyBaseUserTableUUID, Base):
|
|
oauth_accounts: Mapped[list[OAuthAccount]] = relationship(
|
|
"OAuthAccount", lazy="joined"
|
|
)
|
|
|
|
|
|
engine = create_async_engine(
|
|
build_db_url(**MediaManagerConfig().database.model_dump()), echo=False
|
|
)
|
|
async_session_maker = async_sessionmaker(engine, expire_on_commit=False)
|
|
|
|
|
|
async def get_async_session() -> AsyncGenerator[AsyncSession, None]:
|
|
async with async_session_maker() as session:
|
|
yield session
|
|
|
|
|
|
async def get_user_db(
|
|
session: AsyncSession = Depends(get_async_session),
|
|
) -> AsyncGenerator[SQLAlchemyUserDatabase, None]:
|
|
yield SQLAlchemyUserDatabase(session, User, OAuthAccount)
|