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:
1
infrastructure/__init__.py
Normal file
1
infrastructure/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
34
infrastructure/cart_identity.py
Normal file
34
infrastructure/cart_identity.py
Normal file
@@ -0,0 +1,34 @@
|
||||
"""
|
||||
Cart identity resolution — shared across all apps that need to know
|
||||
who the current cart owner is (user_id or anonymous session_id).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import secrets
|
||||
from typing import TypedDict, Optional
|
||||
|
||||
from quart import g, session as qsession
|
||||
|
||||
|
||||
class CartIdentity(TypedDict):
|
||||
user_id: Optional[int]
|
||||
session_id: Optional[str]
|
||||
|
||||
|
||||
def current_cart_identity() -> CartIdentity:
|
||||
"""
|
||||
Decide how to identify the cart:
|
||||
|
||||
- If user is logged in -> use user_id (and ignore session_id)
|
||||
- Else -> generate / reuse an anonymous session_id stored in Quart's session
|
||||
"""
|
||||
user = getattr(g, "user", None)
|
||||
if user is not None and getattr(user, "id", None) is not None:
|
||||
return {"user_id": user.id, "session_id": None}
|
||||
|
||||
sid = qsession.get("cart_sid")
|
||||
if not sid:
|
||||
sid = secrets.token_hex(16)
|
||||
qsession["cart_sid"] = sid
|
||||
|
||||
return {"user_id": None, "session_id": sid}
|
||||
11
infrastructure/cart_loader.py
Normal file
11
infrastructure/cart_loader.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from quart import g
|
||||
|
||||
|
||||
async def load_cart():
|
||||
# Lazy import: cart.bp.cart.services only exists in the cart app process.
|
||||
# This avoids cross-app model conflicts at import time.
|
||||
from cart.bp.cart.services import get_cart
|
||||
|
||||
g.cart = await get_cart(g.s)
|
||||
58
infrastructure/context.py
Normal file
58
infrastructure/context.py
Normal file
@@ -0,0 +1,58 @@
|
||||
"""
|
||||
Base template context shared by all apps.
|
||||
|
||||
This module no longer imports cart or menu_items services directly.
|
||||
Each app provides its own context_fn that calls this base and adds
|
||||
app-specific variables (cart data, menu_items, etc.).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from quart import request, g, current_app
|
||||
|
||||
from shared.config import config
|
||||
from shared.utils import host_url
|
||||
from shared.browser.app.utils import current_route_relative_path
|
||||
|
||||
|
||||
async def base_context() -> dict:
|
||||
"""
|
||||
Common template variables available in every app.
|
||||
|
||||
Does NOT include cart, calendar_cart_entries, total, calendar_total,
|
||||
or menu_items — those are added by each app's context_fn.
|
||||
"""
|
||||
is_htmx = request.headers.get("HX-Request") == "true"
|
||||
search = request.headers.get("X-Search", "")
|
||||
zap_filter = is_htmx and search == ""
|
||||
|
||||
def base_url():
|
||||
return host_url()
|
||||
|
||||
hx_select = "#main-panel"
|
||||
hx_select_search = (
|
||||
hx_select
|
||||
+ ", #search-mobile, #search-count-mobile, #search-desktop, #search-count-desktop, #menu-items-nav-wrapper"
|
||||
)
|
||||
|
||||
return {
|
||||
"is_htmx": is_htmx,
|
||||
"request": request,
|
||||
"now": datetime.now(),
|
||||
"current_local_href": current_route_relative_path(),
|
||||
"config": config(),
|
||||
"asset_url": current_app.jinja_env.globals.get("asset_url", lambda p: ""),
|
||||
"sort_options": [
|
||||
("az", "A\u2013Z", "order/a-z.svg"),
|
||||
("za", "Z\u2013A", "order/z-a.svg"),
|
||||
("price-asc", "\u00a3 low\u2192high", "order/l-h.svg"),
|
||||
("price-desc", "\u00a3 high\u2192low", "order/h-l.svg"),
|
||||
],
|
||||
"zap_filter": zap_filter,
|
||||
"print": print,
|
||||
"base_url": base_url,
|
||||
"base_title": config()["title"],
|
||||
"hx_select": hx_select,
|
||||
"hx_select_search": hx_select_search,
|
||||
}
|
||||
153
infrastructure/factory.py
Normal file
153
infrastructure/factory.py
Normal file
@@ -0,0 +1,153 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Callable, Awaitable, Sequence
|
||||
|
||||
from quart import Quart, request, g, send_from_directory
|
||||
|
||||
from shared.config import init_config, config, pretty
|
||||
from shared.models import KV # ensure models imported
|
||||
from shared.logging import configure_logging
|
||||
from shared.events import EventProcessor
|
||||
|
||||
from shared.db.session import register_db
|
||||
from shared.browser.app.middleware import register as register_middleware
|
||||
from shared.browser.app.redis_cacher import register as register_redis
|
||||
from shared.browser.app.csrf import protect
|
||||
from shared.browser.app.errors import errors
|
||||
|
||||
from .jinja_setup import setup_jinja
|
||||
from .user_loader import load_current_user
|
||||
|
||||
|
||||
# Async init of config (runs once at import)
|
||||
asyncio.run(init_config())
|
||||
|
||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||
STATIC_DIR = str(BASE_DIR / "static")
|
||||
TEMPLATE_DIR = str(BASE_DIR / "browser" / "templates")
|
||||
|
||||
|
||||
def create_base_app(
|
||||
name: str,
|
||||
*,
|
||||
context_fn: Callable[[], Awaitable[dict]] | None = None,
|
||||
before_request_fns: Sequence[Callable[[], Awaitable[None]]] | None = None,
|
||||
) -> Quart:
|
||||
"""
|
||||
Create a Quart app with shared infrastructure.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
name:
|
||||
Application name (also used as CACHE_APP_PREFIX).
|
||||
context_fn:
|
||||
Async function returning a dict for template context.
|
||||
Each app provides its own — the cart app queries locally,
|
||||
while coop/market apps fetch via internal API.
|
||||
If not provided, a minimal default context is used.
|
||||
before_request_fns:
|
||||
Extra before-request hooks (e.g. cart_loader for the cart app).
|
||||
"""
|
||||
app = Quart(
|
||||
name,
|
||||
static_folder=STATIC_DIR,
|
||||
static_url_path="/static",
|
||||
template_folder=TEMPLATE_DIR,
|
||||
)
|
||||
|
||||
configure_logging(name)
|
||||
|
||||
app.secret_key = os.getenv("SECRET_KEY", "dev-secret-key-change-me-777")
|
||||
|
||||
# Session cookie shared across subdomains
|
||||
cookie_domain = os.getenv("SESSION_COOKIE_DOMAIN") # e.g. ".rose-ash.com"
|
||||
if cookie_domain:
|
||||
app.config["SESSION_COOKIE_DOMAIN"] = cookie_domain
|
||||
app.config["SESSION_COOKIE_NAME"] = "coop_session"
|
||||
|
||||
# Ghost / Redis config
|
||||
app.config["GHOST_API_URL"] = os.getenv("GHOST_API_URL")
|
||||
app.config["GHOST_PUBLIC_URL"] = os.getenv("GHOST_PUBLIC_URL")
|
||||
app.config["GHOST_CONTENT_KEY"] = os.getenv("GHOST_CONTENT_API_KEY")
|
||||
app.config["REDIS_URL"] = os.getenv("REDIS_URL")
|
||||
|
||||
# Cache app prefix for key namespacing
|
||||
app.config["CACHE_APP_PREFIX"] = name
|
||||
|
||||
# --- infrastructure ---
|
||||
register_middleware(app)
|
||||
register_db(app)
|
||||
register_redis(app)
|
||||
setup_jinja(app)
|
||||
errors(app)
|
||||
|
||||
# --- before-request hooks ---
|
||||
@app.before_request
|
||||
async def _route_log():
|
||||
g.root = request.headers.get("x-forwarded-prefix", "/")
|
||||
g.scheme = request.scheme
|
||||
g.host = request.host
|
||||
|
||||
@app.before_request
|
||||
async def _load_user():
|
||||
await load_current_user()
|
||||
|
||||
# Register any app-specific before-request hooks (e.g. cart loader)
|
||||
if before_request_fns:
|
||||
for fn in before_request_fns:
|
||||
app.before_request(fn)
|
||||
|
||||
@app.before_request
|
||||
async def _csrf_protect():
|
||||
await protect()
|
||||
|
||||
# --- after-request hooks ---
|
||||
@app.after_request
|
||||
async def _add_hx_preserve_search_header(response):
|
||||
value = request.headers.get("X-Search")
|
||||
if value is not None:
|
||||
response.headers["HX-Preserve-Search"] = value
|
||||
return response
|
||||
|
||||
# --- context processor ---
|
||||
if context_fn is not None:
|
||||
@app.context_processor
|
||||
async def _inject_base():
|
||||
return await context_fn()
|
||||
else:
|
||||
# Minimal fallback (no cart, no menu_items)
|
||||
from .context import base_context
|
||||
|
||||
@app.context_processor
|
||||
async def _inject_base():
|
||||
return await base_context()
|
||||
|
||||
# --- cleanup internal API client on shutdown ---
|
||||
@app.after_serving
|
||||
async def _close_internal_client():
|
||||
from .internal_api import close_client
|
||||
await close_client()
|
||||
|
||||
# --- event processor ---
|
||||
_event_processor = EventProcessor()
|
||||
|
||||
# --- startup ---
|
||||
@app.before_serving
|
||||
async def _startup():
|
||||
await init_config()
|
||||
print(pretty())
|
||||
await _event_processor.start()
|
||||
|
||||
@app.after_serving
|
||||
async def _stop_event_processor():
|
||||
await _event_processor.stop()
|
||||
|
||||
# --- favicon ---
|
||||
@app.get("/favicon.ico")
|
||||
async def favicon():
|
||||
return await send_from_directory("static", "favicon.ico")
|
||||
|
||||
return app
|
||||
49
infrastructure/http_utils.py
Normal file
49
infrastructure/http_utils.py
Normal file
@@ -0,0 +1,49 @@
|
||||
"""
|
||||
HTTP utility helpers shared across apps.
|
||||
|
||||
Extracted from browse/services/services.py so order/orders blueprints
|
||||
(which live in the cart app) don't need to import from the browse blueprint.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from urllib.parse import urlencode
|
||||
|
||||
from quart import g, request
|
||||
from shared.utils import host_url
|
||||
|
||||
|
||||
def vary(resp):
|
||||
"""
|
||||
Ensure HX-Request and X-Origin are part of the Vary header
|
||||
so caches distinguish HTMX from full-page requests.
|
||||
"""
|
||||
v = resp.headers.get("Vary", "")
|
||||
parts = [p.strip() for p in v.split(",") if p.strip()]
|
||||
for h in ("HX-Request", "X-Origin"):
|
||||
if h not in parts:
|
||||
parts.append(h)
|
||||
if parts:
|
||||
resp.headers["Vary"] = ", ".join(parts)
|
||||
return resp
|
||||
|
||||
|
||||
def current_url_without_page():
|
||||
"""
|
||||
Return the current URL with the ``page`` query-string parameter removed.
|
||||
Used for Hx-Push-Url headers on paginated routes.
|
||||
"""
|
||||
(request.script_root or "").rstrip("/")
|
||||
root2 = "/" + g.root
|
||||
path_only = request.path
|
||||
|
||||
if root2 and path_only.startswith(root2):
|
||||
rel = path_only[len(root2):]
|
||||
rel = rel if rel.startswith("/") else "/" + rel
|
||||
else:
|
||||
rel = path_only
|
||||
base = host_url(rel)
|
||||
|
||||
params = request.args.to_dict(flat=False)
|
||||
params.pop("page", None)
|
||||
qs = urlencode(params, doseq=True)
|
||||
return f"{base}?{qs}" if qs else base
|
||||
152
infrastructure/internal_api.py
Normal file
152
infrastructure/internal_api.py
Normal file
@@ -0,0 +1,152 @@
|
||||
"""
|
||||
Async HTTP client for inter-app communication.
|
||||
|
||||
Each app exposes internal JSON API endpoints. Other apps call them
|
||||
via httpx over the Docker overlay network (or localhost in dev).
|
||||
|
||||
URLs resolved from env vars:
|
||||
INTERNAL_URL_COOP (default http://localhost:8000)
|
||||
INTERNAL_URL_MARKET (default http://localhost:8001)
|
||||
INTERNAL_URL_CART (default http://localhost:8002)
|
||||
|
||||
Session cookie forwarding: when ``forward_session=True`` the current
|
||||
request's ``coop_session`` cookie is sent along so the target app can
|
||||
resolve ``g.user`` / cart identity.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import os
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
from quart import request as quart_request
|
||||
|
||||
log = logging.getLogger("internal_api")
|
||||
|
||||
class DictObj:
|
||||
"""Thin wrapper so ``d.key`` works on dicts returned by JSON APIs.
|
||||
|
||||
Jinja templates use attribute access (``item.post.slug``) which
|
||||
doesn't work on plain dicts. Wrapping the API response with
|
||||
``dictobj()`` makes both ``item.post.slug`` and ``item["post"]["slug"]``
|
||||
work identically.
|
||||
"""
|
||||
|
||||
__slots__ = ("_data",)
|
||||
|
||||
def __init__(self, data: dict):
|
||||
self._data = data
|
||||
|
||||
def __getattr__(self, name: str):
|
||||
try:
|
||||
v = self._data[name]
|
||||
except KeyError:
|
||||
raise AttributeError(name)
|
||||
if isinstance(v, dict):
|
||||
return DictObj(v)
|
||||
return v
|
||||
|
||||
def get(self, key, default=None):
|
||||
v = self._data.get(key, default)
|
||||
if isinstance(v, dict):
|
||||
return DictObj(v)
|
||||
return v
|
||||
|
||||
def __repr__(self):
|
||||
return f"DictObj({self._data!r})"
|
||||
|
||||
def __bool__(self):
|
||||
return bool(self._data)
|
||||
|
||||
|
||||
def dictobj(data):
|
||||
"""Recursively wrap dicts (or lists of dicts) for attribute access."""
|
||||
if isinstance(data, list):
|
||||
return [DictObj(d) if isinstance(d, dict) else d for d in data]
|
||||
if isinstance(data, dict):
|
||||
return DictObj(data)
|
||||
return data
|
||||
|
||||
|
||||
_DEFAULTS = {
|
||||
"coop": "http://localhost:8000",
|
||||
"market": "http://localhost:8001",
|
||||
"cart": "http://localhost:8002",
|
||||
"events": "http://localhost:8003",
|
||||
}
|
||||
|
||||
_client: httpx.AsyncClient | None = None
|
||||
|
||||
TIMEOUT = 3.0 # seconds
|
||||
|
||||
|
||||
def _base_url(app_name: str) -> str:
|
||||
env_key = f"INTERNAL_URL_{app_name.upper()}"
|
||||
return os.getenv(env_key, _DEFAULTS.get(app_name, ""))
|
||||
|
||||
|
||||
def _get_client() -> httpx.AsyncClient:
|
||||
global _client
|
||||
if _client is None or _client.is_closed:
|
||||
_client = httpx.AsyncClient(timeout=TIMEOUT)
|
||||
return _client
|
||||
|
||||
|
||||
async def close_client() -> None:
|
||||
"""Call from ``@app.after_serving`` to cleanly close the pool."""
|
||||
global _client
|
||||
if _client is not None and not _client.is_closed:
|
||||
await _client.aclose()
|
||||
_client = None
|
||||
|
||||
|
||||
def _session_cookies() -> dict[str, str]:
|
||||
"""Extract the shared session cookie from the incoming request."""
|
||||
cookie_name = "coop_session"
|
||||
try:
|
||||
val = quart_request.cookies.get(cookie_name)
|
||||
except RuntimeError:
|
||||
# No active request context
|
||||
val = None
|
||||
if val:
|
||||
return {cookie_name: val}
|
||||
return {}
|
||||
|
||||
|
||||
async def get(
|
||||
app_name: str,
|
||||
path: str,
|
||||
*,
|
||||
forward_session: bool = False,
|
||||
params: dict | None = None,
|
||||
) -> dict | list | None:
|
||||
"""GET ``<app_base><path>`` and return parsed JSON, or ``None`` on failure."""
|
||||
url = _base_url(app_name).rstrip("/") + path
|
||||
cookies = _session_cookies() if forward_session else {}
|
||||
try:
|
||||
resp = await _get_client().get(url, params=params, cookies=cookies)
|
||||
resp.raise_for_status()
|
||||
return resp.json()
|
||||
except Exception as exc:
|
||||
log.warning("internal_api GET %s failed: %r", url, exc)
|
||||
return None
|
||||
|
||||
|
||||
async def post(
|
||||
app_name: str,
|
||||
path: str,
|
||||
*,
|
||||
json: Any = None,
|
||||
forward_session: bool = False,
|
||||
) -> dict | list | None:
|
||||
"""POST ``<app_base><path>`` and return parsed JSON, or ``None`` on failure."""
|
||||
url = _base_url(app_name).rstrip("/") + path
|
||||
cookies = _session_cookies() if forward_session else {}
|
||||
try:
|
||||
resp = await _get_client().post(url, json=json, cookies=cookies)
|
||||
resp.raise_for_status()
|
||||
return resp.json()
|
||||
except Exception as exc:
|
||||
log.warning("internal_api POST %s failed: %r", url, exc)
|
||||
return None
|
||||
99
infrastructure/jinja_setup.py
Normal file
99
infrastructure/jinja_setup.py
Normal file
@@ -0,0 +1,99 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
from quart import Quart, g, url_for
|
||||
|
||||
from shared.config import config
|
||||
from shared.utils import host_url
|
||||
|
||||
from shared.browser.app.csrf import generate_csrf_token
|
||||
from shared.browser.app.authz import has_access
|
||||
from shared.browser.app.filters import register as register_filters
|
||||
|
||||
from .urls import coop_url, market_url, cart_url, events_url, login_url, page_cart_url
|
||||
|
||||
|
||||
def setup_jinja(app: Quart) -> None:
|
||||
app.jinja_env.add_extension("jinja2.ext.do")
|
||||
|
||||
# --- template globals ---
|
||||
app.add_template_global(generate_csrf_token, "csrf_token")
|
||||
app.add_template_global(has_access, "has_access")
|
||||
|
||||
def level():
|
||||
if not hasattr(g, "_level_counter"):
|
||||
g._level_counter = 0
|
||||
return g._level_counter
|
||||
|
||||
def level_up():
|
||||
if not hasattr(g, "_level_counter"):
|
||||
g._level_counter = 0
|
||||
g._level_counter += 1
|
||||
return ""
|
||||
|
||||
app.jinja_env.globals["level"] = level
|
||||
app.jinja_env.globals["level_up"] = level_up
|
||||
app.jinja_env.globals["menu_colour"] = "sky"
|
||||
|
||||
nav_button = """justify-center cursor-pointer flex flex-row items-center gap-2 rounded bg-stone-200 text-black
|
||||
[.hover-capable_&]:hover:bg-yellow-300
|
||||
aria-selected:bg-stone-500 aria-selected:text-white
|
||||
[.hover-capable_&[aria-selected=true]:hover]:bg-orange-500"""
|
||||
|
||||
styles = {
|
||||
"pill": """
|
||||
inline-flex items-center px-3 py-1 rounded-full bg-stone-200 text-stone-700 text-sm
|
||||
hover:bg-stone-300 hover:text-stone-900
|
||||
focus:outline-none focus-visible:ring-2 focus-visible:ring-stone-400
|
||||
""",
|
||||
"tr": "odd:bg-slate-50 even:bg-white hover:bg-slate-100",
|
||||
"action_button": "px-2 py-1 border rounded text-sm bg-sky-300 hover:bg-sky-400 flex gap-1 items-center",
|
||||
"pre_action_button": "px-2 py-1 border rounded text-sm bg-green-200 hover:bg-green-300",
|
||||
"cancel_button": "px-3 py-1.5 rounded-full text-sm border border-stone-300 text-stone-700 hover:bg-stone-100",
|
||||
"list_container": "border border-stone-200 rounded-lg p-3 mb-3 bg-white space-y-3 bg-yellow-200",
|
||||
"nav_button": f"{nav_button} p-3",
|
||||
"nav_button_less_pad": f"{nav_button} p-2",
|
||||
}
|
||||
app.jinja_env.globals["styles"] = styles
|
||||
|
||||
def _asset_url(path: str) -> str:
|
||||
def squash_double_slashes(url: str) -> str:
|
||||
m = re.match(r"(?:[A-Za-z][\w+.-]*:)?//", url)
|
||||
prefix = m.group(0) if m else ""
|
||||
rest = re.sub(r"/+", "/", url[len(prefix):])
|
||||
return prefix + rest
|
||||
|
||||
file_path = Path("static") / path
|
||||
try:
|
||||
digest = hashlib.md5(file_path.read_bytes()).hexdigest()[:8]
|
||||
except Exception:
|
||||
digest = "dev"
|
||||
return squash_double_slashes(
|
||||
f"{g.scheme}://{g.host}{g.root}/{url_for('static', filename=path, v=digest)}"
|
||||
)
|
||||
|
||||
app.jinja_env.globals["asset_url"] = _asset_url
|
||||
|
||||
def site():
|
||||
return {
|
||||
"url": host_url(),
|
||||
"logo": _asset_url("img/logo.jpg"),
|
||||
"default_image": _asset_url("img/logo.jpg"),
|
||||
"title": config()["title"],
|
||||
}
|
||||
|
||||
app.jinja_env.globals["site"] = site
|
||||
|
||||
# cross-app URL helpers available in all templates
|
||||
app.jinja_env.globals["coop_url"] = coop_url
|
||||
app.jinja_env.globals["market_url"] = market_url
|
||||
app.jinja_env.globals["cart_url"] = cart_url
|
||||
app.jinja_env.globals["events_url"] = events_url
|
||||
app.jinja_env.globals["login_url"] = login_url
|
||||
app.jinja_env.globals["page_cart_url"] = page_cart_url
|
||||
|
||||
# register jinja filters
|
||||
register_filters(app)
|
||||
49
infrastructure/urls.py
Normal file
49
infrastructure/urls.py
Normal file
@@ -0,0 +1,49 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from urllib.parse import quote
|
||||
|
||||
from shared.config import config
|
||||
|
||||
|
||||
def _get_app_url(app_name: str) -> str:
|
||||
env_key = f"APP_URL_{app_name.upper()}"
|
||||
env_val = os.getenv(env_key)
|
||||
if env_val:
|
||||
return env_val.rstrip("/")
|
||||
return config()["app_urls"][app_name].rstrip("/")
|
||||
|
||||
|
||||
def app_url(app_name: str, path: str = "/") -> str:
|
||||
base = _get_app_url(app_name)
|
||||
if not path.startswith("/"):
|
||||
path = "/" + path
|
||||
return base + path
|
||||
|
||||
|
||||
def coop_url(path: str = "/") -> str:
|
||||
return app_url("coop", path)
|
||||
|
||||
|
||||
def market_url(path: str = "/") -> str:
|
||||
return app_url("market", path)
|
||||
|
||||
|
||||
def cart_url(path: str = "/") -> str:
|
||||
return app_url("cart", path)
|
||||
|
||||
|
||||
def events_url(path: str = "/") -> str:
|
||||
return app_url("events", path)
|
||||
|
||||
|
||||
def page_cart_url(page_slug: str, path: str = "/") -> str:
|
||||
if not path.startswith("/"):
|
||||
path = "/" + path
|
||||
return cart_url(f"/{page_slug}{path}")
|
||||
|
||||
|
||||
def login_url(next_url: str = "") -> str:
|
||||
if next_url:
|
||||
return coop_url(f"/auth/login/?next={quote(next_url, safe='')}")
|
||||
return coop_url("/auth/login/")
|
||||
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