Replace Python GET page handlers with declarative defpage definitions in .sx files across all 8 apps (sx docs, orders, account, market, cart, federation, events, blog). Each app now has sxc/pages/ with setup functions, layout registrations, page helpers, and .sx defpage declarations. Core infrastructure: add g I/O primitive, PageDef support for auth/layout/ data/content/filter/aside/menu slots, post_author auth level, and custom layout registration. Remove ~1400 lines of render_*_page/render_*_oob boilerplate. Update all endpoint references in routes, sx_components, and templates to defpage_* naming. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
118 lines
3.9 KiB
Python
118 lines
3.9 KiB
Python
from __future__ import annotations
|
|
import path_setup # noqa: F401 # adds shared/ to sys.path
|
|
import sx.sx_components as sx_components # noqa: F401 # ensure Hypercorn --reload watches this file
|
|
from pathlib import Path
|
|
|
|
from quart import g, request
|
|
from jinja2 import FileSystemLoader, ChoiceLoader
|
|
|
|
from shared.infrastructure.factory import create_base_app
|
|
from shared.services.registry import services
|
|
|
|
from bp import (
|
|
register_identity_bp,
|
|
register_social_bp,
|
|
register_fragments,
|
|
)
|
|
|
|
|
|
async def federation_context() -> dict:
|
|
"""Federation app context processor."""
|
|
from shared.infrastructure.context import base_context
|
|
from shared.infrastructure.cart_identity import current_cart_identity
|
|
from shared.infrastructure.fragments import fetch_fragments
|
|
from shared.infrastructure.data_client import fetch_data
|
|
from shared.contracts.dtos import CartSummaryDTO, dto_from_dict
|
|
|
|
ctx = await base_context()
|
|
|
|
# menu_nodes lives in db_blog; nav-tree fragment provides the real nav
|
|
ctx["menu_items"] = []
|
|
|
|
# Cart data via internal data endpoint
|
|
ident = current_cart_identity()
|
|
summary_params = {}
|
|
if ident["user_id"] is not None:
|
|
summary_params["user_id"] = ident["user_id"]
|
|
if ident["session_id"] is not None:
|
|
summary_params["session_id"] = ident["session_id"]
|
|
raw = await fetch_data("cart", "cart-summary", params=summary_params, required=False)
|
|
summary = dto_from_dict(CartSummaryDTO, raw) if raw else CartSummaryDTO()
|
|
ctx["cart_count"] = summary.count + summary.calendar_count + summary.ticket_count
|
|
ctx["cart_total"] = float(summary.total + summary.calendar_total + summary.ticket_total)
|
|
|
|
# Pre-fetch cross-app HTML fragments concurrently
|
|
user = getattr(g, "user", None)
|
|
cart_params = {}
|
|
if ident["user_id"] is not None:
|
|
cart_params["user_id"] = ident["user_id"]
|
|
if ident["session_id"] is not None:
|
|
cart_params["session_id"] = ident["session_id"]
|
|
|
|
cart_mini, auth_menu, nav_tree = await fetch_fragments([
|
|
("cart", "cart-mini", cart_params or None),
|
|
("account", "auth-menu", {"email": user.email} if user else None),
|
|
("blog", "nav-tree", {"app_name": "federation", "path": request.path}),
|
|
])
|
|
ctx["cart_mini"] = cart_mini
|
|
ctx["auth_menu"] = auth_menu
|
|
ctx["nav_tree"] = nav_tree
|
|
|
|
# Actor profile for logged-in users
|
|
if g.get("user"):
|
|
actor = await services.federation.get_actor_by_user_id(g.s, g.user.id)
|
|
ctx["actor"] = actor
|
|
else:
|
|
ctx["actor"] = None
|
|
|
|
return ctx
|
|
|
|
|
|
def create_app() -> "Quart":
|
|
from services import register_domain_services
|
|
|
|
app = create_base_app(
|
|
"federation",
|
|
context_fn=federation_context,
|
|
domain_services_fn=register_domain_services,
|
|
)
|
|
|
|
# App-specific templates override shared templates
|
|
app_templates = str(Path(__file__).resolve().parent / "templates")
|
|
app.jinja_loader = ChoiceLoader([
|
|
FileSystemLoader(app_templates),
|
|
app.jinja_loader,
|
|
])
|
|
|
|
# --- defpage setup ---
|
|
from sxc.pages import setup_federation_pages
|
|
setup_federation_pages()
|
|
|
|
# --- blueprints ---
|
|
# Well-known + actors (webfinger, inbox, outbox, etc.) are now handled
|
|
# by the shared AP blueprint registered in create_base_app().
|
|
app.register_blueprint(register_identity_bp())
|
|
|
|
social_bp = register_social_bp()
|
|
from shared.sx.pages import mount_pages
|
|
mount_pages(social_bp, "federation")
|
|
app.register_blueprint(social_bp)
|
|
|
|
app.register_blueprint(register_fragments())
|
|
|
|
# --- home page ---
|
|
@app.get("/")
|
|
async def home():
|
|
from quart import make_response
|
|
from shared.sx.page import get_template_context
|
|
from sx.sx_components import render_federation_home
|
|
|
|
ctx = await get_template_context()
|
|
html = await render_federation_home(ctx)
|
|
return await make_response(html)
|
|
|
|
return app
|
|
|
|
|
|
app = create_app()
|