Phase 1 of zero-Python rendering: account service. - Auth pages (login, device, check-email) use _render_auth_page() helper calling render_to_sx() + full_page_sx() directly in routes - Newsletter toggle POST renders inline via render_to_sx() - Newsletter page helper returns data dict; defpage :data slot fetches, :content slot renders via ~account-newsletters-content defcomp - Fragment page uses (frag ...) IO primitive directly in .sx - Defpage _eval_slot now uses async_eval_slot_to_sx which expands component bodies server-side (executing IO) but serializes tags as SX - Fix pre-existing OOB ParseError: _eval_slot was producing HTML instead of s-expressions for component content slots - Fix market url_for endpoint: defpage_market_home (app-level, not blueprint) - Fix events calendar nav: wrap multiple SX parts in fragment Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
140 lines
4.7 KiB
Python
140 lines
4.7 KiB
Python
"""Account defpage setup — registers layouts, page helpers, and loads .sx pages."""
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
|
|
def setup_account_pages() -> None:
|
|
"""Register account-specific layouts, page helpers, and load page definitions."""
|
|
_register_account_layouts()
|
|
_register_account_helpers()
|
|
_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"))
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Page helpers
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def _register_account_helpers() -> None:
|
|
from shared.sx.pages import register_page_helpers
|
|
|
|
register_page_helpers("account", {
|
|
"newsletters-data": _h_newsletters_data,
|
|
})
|
|
|
|
|
|
async def _h_newsletters_data(**kw):
|
|
"""Fetch newsletter data — returns dict merged into defpage env."""
|
|
from quart import g
|
|
from sqlalchemy import select
|
|
from shared.models import UserNewsletter
|
|
from shared.models.ghost_membership_entities import GhostNewsletter
|
|
|
|
result = await g.s.execute(
|
|
select(GhostNewsletter).order_by(GhostNewsletter.name)
|
|
)
|
|
all_newsletters = result.scalars().all()
|
|
|
|
sub_result = await g.s.execute(
|
|
select(UserNewsletter).where(
|
|
UserNewsletter.user_id == g.user.id,
|
|
)
|
|
)
|
|
user_subs = {un.newsletter_id: un for un in sub_result.scalars().all()}
|
|
|
|
newsletter_list = []
|
|
for nl in all_newsletters:
|
|
un = user_subs.get(nl.id)
|
|
newsletter_list.append({
|
|
"newsletter": {"id": nl.id, "name": nl.name, "description": nl.description},
|
|
"un": {"newsletter_id": un.newsletter_id, "subscribed": un.subscribed} if un else None,
|
|
"subscribed": un.subscribed if un else False,
|
|
})
|
|
|
|
account_url = getattr(g, "_account_url", None)
|
|
if account_url is None:
|
|
from shared.infrastructure.urls import account_url as _account_url
|
|
account_url = _account_url
|
|
account_url_str = account_url("") if callable(account_url) else str(account_url or "")
|
|
|
|
return {"newsletter-list": newsletter_list, "account-url": account_url_str}
|