All three services now fetch page data via (service ...) IO primitives in .sx defpages instead of Python middleman functions. - Account: newsletters-data → AccountPageService.newsletters_data - Federation: 8 page helpers → FederationPageService methods (timeline, compose, search, following, followers, notifications) - Cart: 4 page helpers → CartPageService methods (overview, page-cart, admin, payments) - Serializers moved to service modules, thin delegates kept for routes - ~520 lines of Python page helpers removed Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
92 lines
2.9 KiB
Python
92 lines
2.9 KiB
Python
"""Account defpage setup — registers layouts and loads .sx pages."""
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
|
|
def setup_account_pages() -> None:
|
|
"""Register account-specific layouts and load page definitions."""
|
|
_register_account_layouts()
|
|
_load_account_page_files()
|
|
|
|
|
|
def _load_account_page_files() -> None:
|
|
import os
|
|
from shared.sx.pages import load_page_dir
|
|
load_page_dir(os.path.dirname(__file__), "account")
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Layouts
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def _register_account_layouts() -> None:
|
|
from shared.sx.layouts import register_custom_layout
|
|
register_custom_layout("account", _account_full, _account_oob, _account_mobile)
|
|
|
|
|
|
async def _account_full(ctx: dict, **kw: Any) -> str:
|
|
from shared.sx.helpers import root_header_sx, header_child_sx, render_to_sx
|
|
|
|
root_hdr = await root_header_sx(ctx)
|
|
auth_hdr = await render_to_sx("auth-header-row",
|
|
account_url=_call_url(ctx, "account_url", ""),
|
|
select_colours=ctx.get("select_colours", ""),
|
|
account_nav=_as_sx_nav(ctx),
|
|
)
|
|
hdr_child = await header_child_sx(auth_hdr)
|
|
return "(<> " + root_hdr + " " + hdr_child + ")"
|
|
|
|
|
|
async def _account_oob(ctx: dict, **kw: Any) -> str:
|
|
from shared.sx.helpers import root_header_sx, render_to_sx
|
|
|
|
auth_hdr = await render_to_sx("auth-header-row",
|
|
account_url=_call_url(ctx, "account_url", ""),
|
|
select_colours=ctx.get("select_colours", ""),
|
|
account_nav=_as_sx_nav(ctx),
|
|
oob=True,
|
|
)
|
|
return "(<> " + auth_hdr + " " + await root_header_sx(ctx, oob=True) + ")"
|
|
|
|
|
|
async def _account_mobile(ctx: dict, **kw: Any) -> str:
|
|
from shared.sx.helpers import mobile_menu_sx, mobile_root_nav_sx, render_to_sx
|
|
from shared.sx.parser import SxExpr
|
|
|
|
ctx = _inject_account_nav(ctx)
|
|
nav_items = await render_to_sx("auth-nav-items",
|
|
account_url=_call_url(ctx, "account_url", ""),
|
|
select_colours=ctx.get("select_colours", ""),
|
|
account_nav=_as_sx_nav(ctx),
|
|
)
|
|
auth_section = await render_to_sx("mobile-menu-section",
|
|
label="account", href="/", level=1, colour="sky",
|
|
items=SxExpr(nav_items))
|
|
return mobile_menu_sx(auth_section, await mobile_root_nav_sx(ctx))
|
|
|
|
|
|
def _call_url(ctx: dict, key: str, path: str = "/") -> str:
|
|
fn = ctx.get(key)
|
|
if callable(fn):
|
|
return fn(path)
|
|
return str(fn or "") + path
|
|
|
|
|
|
def _inject_account_nav(ctx: dict) -> dict:
|
|
"""Ensure account_nav is in ctx from g.account_nav."""
|
|
if "account_nav" not in ctx:
|
|
from quart import g
|
|
ctx = dict(ctx)
|
|
ctx["account_nav"] = getattr(g, "account_nav", "")
|
|
return ctx
|
|
|
|
|
|
def _as_sx_nav(ctx: dict) -> Any:
|
|
"""Convert account_nav fragment to SxExpr for use in component calls."""
|
|
from shared.sx.helpers import _as_sx
|
|
ctx = _inject_account_nav(ctx)
|
|
return _as_sx(ctx.get("account_nav"))
|
|
|
|
|