feat: extract shared infrastructure from shared_lib
Phase 1-3 of decoupling plan: - Shared DB, models, infrastructure, browser, config, utils - Event infrastructure (domain_events outbox, bus, processor) - Structured logging - Generic container concept (container_type/container_id) - Alembic migrations for all schema changes Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
35
infrastructure/user_loader.py
Normal file
35
infrastructure/user_loader.py
Normal file
@@ -0,0 +1,35 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from quart import session as qsession, g
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import selectinload
|
||||
|
||||
from shared.models.user import User
|
||||
from shared.models.ghost_membership_entities import UserNewsletter
|
||||
|
||||
|
||||
async def load_user_by_id(session, user_id: int):
|
||||
"""Load a user by ID with labels and newsletters eagerly loaded."""
|
||||
stmt = (
|
||||
select(User)
|
||||
.options(
|
||||
selectinload(User.labels),
|
||||
selectinload(User.user_newsletters).selectinload(
|
||||
UserNewsletter.newsletter
|
||||
),
|
||||
)
|
||||
.where(User.id == user_id)
|
||||
)
|
||||
result = await session.execute(stmt)
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
|
||||
async def load_current_user():
|
||||
uid = qsession.get("uid")
|
||||
if not uid:
|
||||
g.user = None
|
||||
g.rights = {"admin": False}
|
||||
return
|
||||
|
||||
g.user = await load_user_by_id(g.s, uid)
|
||||
g.rights = {l.name: True for l in g.user.labels} if g.user else {}
|
||||
Reference in New Issue
Block a user