Skip to content

Commit

Permalink
Update fastapi-users v12 (#329)
Browse files Browse the repository at this point in the history
* Disable type check for click

See pallets/click#2558

* Update fastapi-users v12

* Remove email column from User

* Use SQLAlchemy AsyncAttrs/awaitable_attrs when accessing oauth_accounts with lazy loading
  • Loading branch information
davidbrochart authored Jul 11, 2023
1 parent 06e50b0 commit 8108945
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 12 deletions.
2 changes: 1 addition & 1 deletion jupyverse_api/jupyverse_api/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from asphalt.core.cli import run


@click.command()
@click.command() # type: ignore
@click.option(
"--open-browser",
is_flag=True,
Expand Down
2 changes: 1 addition & 1 deletion plugins/auth/fps_auth/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def get_jwt_strategy() -> JWTStrategy:

class UserManager(UUIDIDMixin, BaseUserManager[User, uuid.UUID]):
async def on_after_register(self, user: User, request: Optional[Request] = None):
for oauth_account in user.oauth_accounts:
for oauth_account in await user.awaitable_attrs.oauth_accounts:
if oauth_account.oauth_name == "github":
async with httpx.AsyncClient() as client:
r = (
Expand Down
16 changes: 8 additions & 8 deletions plugins/auth/fps_auth/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@
SQLAlchemyBaseUserTableUUID,
SQLAlchemyUserDatabase,
)
from sqlalchemy import JSON, Boolean, Column, String, Text # type: ignore
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine # type: ignore
from sqlalchemy.ext.declarative import DeclarativeMeta, declarative_base # type: ignore
from sqlalchemy.orm import relationship, sessionmaker # type: ignore
from sqlalchemy import JSON, Boolean, Column, String, Text
from sqlalchemy.ext.asyncio import AsyncAttrs, AsyncSession, async_sessionmaker, create_async_engine
from sqlalchemy.orm import DeclarativeBase, Mapped, relationship

from .config import _AuthConfig


logger = logging.getLogger("auth")

Base: DeclarativeMeta = declarative_base()

class Base(AsyncAttrs, DeclarativeBase):
pass


class OAuthAccount(SQLAlchemyBaseOAuthAccountTableUUID, Base):
Expand All @@ -29,7 +30,6 @@ class OAuthAccount(SQLAlchemyBaseOAuthAccountTableUUID, Base):

class User(SQLAlchemyBaseUserTableUUID, Base):
anonymous = Column(Boolean, default=True, nullable=False)
email = Column(String(length=32), nullable=False, unique=True)
username = Column(String(length=32), nullable=False, unique=True)
name = Column(String(length=32), default="")
display_name = Column(String(length=32), default="")
Expand All @@ -39,7 +39,7 @@ class User(SQLAlchemyBaseUserTableUUID, Base):
workspace = Column(Text(), default="{}", nullable=False)
settings = Column(Text(), default="{}", nullable=False)
permissions = Column(JSON, default={}, nullable=False)
oauth_accounts: List[OAuthAccount] = relationship("OAuthAccount", lazy="joined")
oauth_accounts: Mapped[List[OAuthAccount]] = relationship("OAuthAccount", lazy="joined")


@dataclass
Expand Down Expand Up @@ -82,7 +82,7 @@ def get_db(auth_config: _AuthConfig) -> Res:
database_url = f"sqlite+aiosqlite:///{userdb_path}"

engine = create_async_engine(database_url)
async_session_maker = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
async_session_maker = async_sessionmaker(engine, expire_on_commit=False)

async def create_db_and_tables():
async with engine.begin() as conn:
Expand Down
3 changes: 1 addition & 2 deletions plugins/auth/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ dynamic = ["version"]
requires-python = ">=3.8"
dependencies = [
"aiosqlite",
"fastapi-users[sqlalchemy,oauth] >=11,<12",
"sqlalchemy >=1,<2",
"fastapi-users[sqlalchemy,oauth] >=12,<13",
"jupyverse-api >=0.1.2,<1",
]

Expand Down

0 comments on commit 8108945

Please sign in to comment.