Replace sx_call() with render_to_sx() across all services
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 2m6s

Python no longer generates s-expression strings. All SX rendering now
goes through render_to_sx() which builds AST from native Python values
and evaluates via async_eval_to_sx() — no SX string literals in Python.

- Add render_to_sx()/render_to_html() infrastructure in shared/sx/helpers.py
- Add (abort status msg) IO primitive in shared/sx/primitives_io.py
- Convert all 9 services: ~650 sx_call() invocations replaced
- Convert shared helpers (root_header_sx, full_page_sx, etc.) to async
- Fix likes service import bug (likes.models → models)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-04 00:08:33 +00:00
parent 0554f8a113
commit e085fe43b4
51 changed files with 1824 additions and 1742 deletions

View File

@@ -73,8 +73,7 @@ def register(url_prefix: str) -> Blueprint:
result = await g.s.execute(stmt)
orders = result.scalars().all()
from shared.sx.helpers import sx_response, sx_call, SxExpr
from shared.sx.parser import serialize
from shared.sx.helpers import sx_response, render_to_sx
from shared.utils import route_prefix
pfx = route_prefix()
@@ -96,16 +95,16 @@ def register(url_prefix: str) -> Blueprint:
# Build just the rows fragment (not full table) for infinite scroll
parts = []
for od in order_dicts:
parts.append(sx_call("order-row-pair",
order=SxExpr(serialize(od)),
parts.append(await render_to_sx("order-row-pair",
order=od,
detail_url_prefix=detail_prefix))
if page < total_pages:
parts.append(sx_call("infinite-scroll",
parts.append(await render_to_sx("infinite-scroll",
url=rows_url + qs_fn(page=page + 1),
page=page, total_pages=total_pages,
id_prefix="orders", colspan=5))
else:
parts.append(sx_call("order-end-row"))
parts.append(await render_to_sx("order-end-row"))
sx_src = "(<> " + " ".join(parts) + ")"
resp = sx_response(sx_src)

View File

@@ -12,7 +12,7 @@ from typing import Any
from shared.sx.jinja_bridge import load_service_components
from shared.sx.helpers import (
call_url, sx_call, SxExpr,
call_url, render_to_sx,
root_header_sx, full_page_sx, header_child_sx,
)
from shared.infrastructure.urls import market_product_url, cart_url
@@ -29,24 +29,24 @@ load_service_components(os.path.dirname(os.path.dirname(__file__)),
async def render_checkout_error_page(ctx: dict, error: str | None = None, order: Any | None = None) -> str:
"""Full page: checkout error (sx wire format)."""
account_url = call_url(ctx, "account_url", "")
auth_hdr = sx_call("auth-header-row", account_url=account_url)
hdr = root_header_sx(ctx)
hdr = "(<> " + hdr + " " + header_child_sx(auth_hdr) + ")"
filt = sx_call("checkout-error-header")
auth_hdr = await render_to_sx("auth-header-row", account_url=account_url)
hdr = await root_header_sx(ctx)
hdr = "(<> " + hdr + " " + await header_child_sx(auth_hdr) + ")"
filt = await render_to_sx("checkout-error-header")
err_msg = error or "Unexpected error while creating the hosted checkout session."
order_sx = ""
if order:
order_sx = sx_call("checkout-error-order-id", oid=f"#{order.id}")
back_url = cart_url("/")
content = sx_call(
order_sx = await render_to_sx("checkout-error-order-id", oid=f"#{order.id}")
from shared.sx.parser import SxExpr
content = await render_to_sx(
"checkout-error-content",
msg=err_msg,
order=SxExpr(order_sx) if order_sx else None,
back_url=back_url,
back_url=cart_url("/"),
)
return full_page_sx(ctx, header_rows=hdr, filter=filt, content=content)
return await full_page_sx(ctx, header_rows=hdr, filter=filt, content=content)
# ---------------------------------------------------------------------------
@@ -58,12 +58,12 @@ async def render_checkout_return_page(ctx: dict, order: Any | None,
calendar_entries: list | None = None,
order_tickets: list | None = None) -> str:
"""Full page: checkout return after SumUp payment (sx wire format)."""
from shared.sx.parser import serialize
from shared.sx.parser import SxExpr
filt = sx_call("checkout-return-header", status=status)
filt = await render_to_sx("checkout-return-header", status=status)
if not order:
content = sx_call("checkout-return-missing")
content = await render_to_sx("checkout-return-missing")
else:
# Serialize order data for defcomp
order_dict = {
@@ -87,7 +87,7 @@ async def render_checkout_return_page(ctx: dict, order: Any | None,
],
}
summary = sx_call("order-summary-card",
summary = await render_to_sx("order-summary-card",
order_id=order.id,
created_at=order_dict["created_at_formatted"],
description=order.description, status=order.status,
@@ -101,19 +101,19 @@ async def render_checkout_return_page(ctx: dict, order: Any | None,
item_parts = []
for item_d in order_dict["items"]:
if item_d["product_image"]:
img = sx_call("order-item-image",
img = await render_to_sx("order-item-image",
src=item_d["product_image"],
alt=item_d["product_title"] or "Product image")
else:
img = sx_call("order-item-no-image")
item_parts.append(sx_call("order-item-row",
img = await render_to_sx("order-item-no-image")
item_parts.append(await render_to_sx("order-item-row",
href=item_d["product_url"], img=SxExpr(img),
title=item_d["product_title"] or "Unknown product",
pid=f"Product ID: {item_d['product_id']}",
qty=f"Qty: {item_d['quantity']}",
price=f"{item_d['currency'] or order.currency or 'GBP'} {item_d['unit_price_formatted']}",
))
items = sx_call("order-items-panel",
items = await render_to_sx("order-items-panel",
items=SxExpr("(<> " + " ".join(item_parts) + ")"))
# Calendar entries
@@ -131,13 +131,13 @@ async def render_checkout_return_page(ctx: dict, order: Any | None,
ds = e.start_at.strftime("%-d %b %Y, %H:%M") if e.start_at else ""
if e.end_at:
ds += f" \u2013 {e.end_at.strftime('%-d %b %Y, %H:%M')}"
cal_parts.append(sx_call("order-calendar-entry",
cal_parts.append(await render_to_sx("order-calendar-entry",
name=e.name,
pill=f"inline-flex items-center rounded-full px-2 py-0.5 text-[11px] font-medium {pill}",
status=st.capitalize(), date_str=ds,
cost=f"\u00a3{e.cost or 0:.2f}",
))
calendar = sx_call("order-calendar-section",
calendar = await render_to_sx("order-calendar-section",
items=SxExpr("(<> " + " ".join(cal_parts) + ")"))
# Tickets
@@ -156,24 +156,24 @@ async def render_checkout_return_page(ctx: dict, order: Any | None,
ds = tk.entry_start_at.strftime("%-d %b %Y, %H:%M") if tk.entry_start_at else ""
if tk.entry_end_at:
ds += f" \u2013 {tk.entry_end_at.strftime('%-d %b %Y, %H:%M')}"
tk_parts.append(sx_call("checkout-return-ticket",
tk_parts.append(await render_to_sx("checkout-return-ticket",
name=tk.entry_name, pill=pill_cls,
state=st.replace("_", " ").capitalize(),
type_name=tk.ticket_type_name or None,
date_str=ds, code=tk.code,
price=f"\u00a3{tk.price or 0:.2f}",
))
tickets = sx_call("checkout-return-tickets",
tickets = await render_to_sx("checkout-return-tickets",
items=SxExpr("(<> " + " ".join(tk_parts) + ")"))
# Status message
status_msg = ""
if order.status == "failed":
status_msg = sx_call("checkout-return-failed", order_id=order.id)
status_msg = await render_to_sx("checkout-return-failed", order_id=order.id)
elif order.status == "paid":
status_msg = sx_call("checkout-return-paid")
status_msg = await render_to_sx("checkout-return-paid")
content = sx_call("checkout-return-content",
content = await render_to_sx("checkout-return-content",
summary=SxExpr(summary),
items=SxExpr(items) if items else None,
calendar=SxExpr(calendar) if calendar else None,
@@ -182,8 +182,8 @@ async def render_checkout_return_page(ctx: dict, order: Any | None,
)
account_url = call_url(ctx, "account_url", "")
auth_hdr = sx_call("auth-header-row", account_url=account_url)
hdr = root_header_sx(ctx)
hdr = "(<> " + hdr + " " + header_child_sx(auth_hdr) + ")"
auth_hdr = await render_to_sx("auth-header-row", account_url=account_url)
hdr = await root_header_sx(ctx)
hdr = "(<> " + hdr + " " + await header_child_sx(auth_hdr) + ")"
return full_page_sx(ctx, header_rows=hdr, filter=filt, content=content)
return await full_page_sx(ctx, header_rows=hdr, filter=filt, content=content)

View File

@@ -28,94 +28,101 @@ def _register_orders_layouts() -> None:
register_custom_layout("order-detail", _order_detail_full, _order_detail_oob, _order_detail_mobile)
def _orders_full(ctx: dict, **kw: Any) -> str:
from shared.sx.helpers import root_header_sx, header_child_sx, call_url, sx_call, SxExpr
async def _orders_full(ctx: dict, **kw: Any) -> str:
from shared.sx.helpers import root_header_sx, header_child_sx, call_url, render_to_sx
list_url = kw.get("list_url", "/")
account_url = call_url(ctx, "account_url", "")
root_hdr = root_header_sx(ctx)
auth_hdr = sx_call("auth-header-row",
root_hdr = await root_header_sx(ctx)
auth_hdr = await render_to_sx("auth-header-row",
account_url=account_url,
select_colours=ctx.get("select_colours", ""),
account_nav=_as_sx_nav(ctx),
)
orders_hdr = sx_call("orders-header-row", list_url=list_url)
orders_hdr = await render_to_sx("orders-header-row", list_url=list_url)
inner = "(<> " + auth_hdr + " " + orders_hdr + ")"
return "(<> " + root_hdr + " " + header_child_sx(inner) + ")"
return "(<> " + root_hdr + " " + await header_child_sx(inner) + ")"
def _orders_oob(ctx: dict, **kw: Any) -> str:
from shared.sx.helpers import root_header_sx, sx_call, SxExpr, call_url
async def _orders_oob(ctx: dict, **kw: Any) -> str:
from shared.sx.helpers import root_header_sx, render_to_sx
from shared.sx.helpers import call_url
from shared.sx.parser import SxExpr
list_url = kw.get("list_url", "/")
account_url = call_url(ctx, "account_url", "")
auth_hdr = sx_call("auth-header-row",
auth_hdr = await render_to_sx("auth-header-row",
account_url=account_url,
select_colours=ctx.get("select_colours", ""),
account_nav=_as_sx_nav(ctx),
oob=True,
)
auth_child_oob = sx_call("oob-header-sx",
parent_id="auth-header-child",
row=SxExpr(sx_call("orders-header-row", list_url=list_url)))
root_hdr = root_header_sx(ctx, oob=True)
orders_hdr = await render_to_sx("orders-header-row", list_url=list_url)
auth_child_oob = await render_to_sx("oob-header-sx",
parent_id="auth-header-child",
row=SxExpr(orders_hdr))
root_hdr = await root_header_sx(ctx, oob=True)
return "(<> " + auth_hdr + " " + auth_child_oob + " " + root_hdr + ")"
def _orders_mobile(ctx: dict, **kw: Any) -> str:
async def _orders_mobile(ctx: dict, **kw: Any) -> str:
from shared.sx.helpers import mobile_menu_sx, mobile_root_nav_sx
return mobile_menu_sx(mobile_root_nav_sx(ctx))
return mobile_menu_sx(await mobile_root_nav_sx(ctx))
def _order_detail_full(ctx: dict, **kw: Any) -> str:
from shared.sx.helpers import root_header_sx, sx_call, SxExpr, call_url
async def _order_detail_full(ctx: dict, **kw: Any) -> str:
from shared.sx.helpers import root_header_sx, render_to_sx
from shared.sx.helpers import call_url
from shared.sx.parser import SxExpr
list_url = kw.get("list_url", "/")
detail_url = kw.get("detail_url", "/")
account_url = call_url(ctx, "account_url", "")
root_hdr = root_header_sx(ctx)
order_row = sx_call(
root_hdr = await root_header_sx(ctx)
order_row = await render_to_sx(
"menu-row-sx",
id="order-row", level=3, colour="sky", link_href=detail_url,
link_label="Order", icon="fa fa-gbp",
)
auth_hdr = sx_call("auth-header-row",
auth_hdr = await render_to_sx("auth-header-row",
account_url=account_url,
select_colours=ctx.get("select_colours", ""),
account_nav=_as_sx_nav(ctx),
)
detail_header = sx_call(
orders_hdr = await render_to_sx("orders-header-row", list_url=list_url)
detail_header = await render_to_sx(
"order-detail-header-stack",
auth=SxExpr(auth_hdr),
orders=SxExpr(sx_call("orders-header-row", list_url=list_url)),
orders=SxExpr(orders_hdr),
order=SxExpr(order_row),
)
return "(<> " + root_hdr + " " + detail_header + ")"
def _order_detail_oob(ctx: dict, **kw: Any) -> str:
from shared.sx.helpers import root_header_sx, sx_call, SxExpr
async def _order_detail_oob(ctx: dict, **kw: Any) -> str:
from shared.sx.helpers import root_header_sx, render_to_sx
from shared.sx.parser import SxExpr
detail_url = kw.get("detail_url", "/")
order_row_oob = sx_call(
order_row_oob = await render_to_sx(
"menu-row-sx",
id="order-row", level=3, colour="sky", link_href=detail_url,
link_label="Order", icon="fa fa-gbp", oob=True,
)
header_child_oob = sx_call("oob-header-sx",
header_child_oob = await render_to_sx("oob-header-sx",
parent_id="orders-header-child",
row=SxExpr(order_row_oob))
root_hdr = root_header_sx(ctx, oob=True)
root_hdr = await root_header_sx(ctx, oob=True)
return "(<> " + header_child_oob + " " + root_hdr + ")"
def _order_detail_mobile(ctx: dict, **kw: Any) -> str:
async def _order_detail_mobile(ctx: dict, **kw: Any) -> str:
from shared.sx.helpers import mobile_menu_sx, mobile_root_nav_sx
return mobile_menu_sx(mobile_root_nav_sx(ctx))
return mobile_menu_sx(await mobile_root_nav_sx(ctx))
def _as_sx_nav(ctx: dict) -> Any:
"""Convert account_nav fragment to SxExpr for use in sx_call."""
"""Convert account_nav fragment to SxExpr for use in component calls."""
from shared.sx.helpers import _as_sx
return _as_sx(ctx.get("account_nav"))
@@ -279,11 +286,10 @@ async def _ensure_order_detail(order_id):
async def _h_orders_list_content(**kw):
await _ensure_orders_list()
from quart import g
from shared.sx.helpers import sx_call, SxExpr
from shared.sx.parser import serialize
from shared.sx.helpers import render_to_sx
d = getattr(g, "orders_page_data", None)
if not d:
return sx_call("order-empty-state")
return await render_to_sx("order-empty-state")
orders = d["orders"]
url_for_fn = d["url_for_fn"]
@@ -305,8 +311,8 @@ async def _h_orders_list_content(**kw):
detail_prefix = rpfx + url_for_fn("orders.defpage_order_detail", order_id=0).rsplit("0/", 1)[0]
rows_url = rpfx + url_for_fn("orders.orders_rows")
return sx_call("orders-list-content",
orders=SxExpr(serialize(order_dicts)),
return await render_to_sx("orders-list-content",
orders=order_dicts,
page=d["page"],
total_pages=d["total_pages"],
rows_url=rows_url,
@@ -316,30 +322,31 @@ async def _h_orders_list_content(**kw):
async def _h_orders_list_filter(**kw):
await _ensure_orders_list()
from quart import g
from shared.sx.helpers import sx_call, SxExpr
from shared.sx.helpers import render_to_sx
from shared.sx.page import SEARCH_HEADERS_MOBILE
from shared.sx.parser import SxExpr
d = getattr(g, "orders_page_data", None)
search = d.get("search", "") if d else ""
search_count = d.get("search_count", "") if d else ""
search_mobile = sx_call("search-mobile",
search_mobile = await render_to_sx("search-mobile",
current_local_href="/",
search=search or "",
search_count=search_count or "",
hx_select="#main-panel",
search_headers_mobile=SEARCH_HEADERS_MOBILE,
)
return sx_call("order-list-header", search_mobile=SxExpr(search_mobile))
return await render_to_sx("order-list-header", search_mobile=SxExpr(search_mobile))
async def _h_orders_list_aside(**kw):
await _ensure_orders_list()
from quart import g
from shared.sx.helpers import sx_call
from shared.sx.helpers import render_to_sx
from shared.sx.page import SEARCH_HEADERS_DESKTOP
d = getattr(g, "orders_page_data", None)
search = d.get("search", "") if d else ""
search_count = d.get("search_count", "") if d else ""
return sx_call("search-desktop",
return await render_to_sx("search-desktop",
current_local_href="/",
search=search or "",
search_count=search_count or "",
@@ -358,8 +365,7 @@ async def _h_orders_list_url(**kw):
async def _h_order_detail_content(order_id=None, **kw):
await _ensure_order_detail(order_id)
from quart import g
from shared.sx.helpers import sx_call, SxExpr
from shared.sx.parser import serialize
from shared.sx.helpers import render_to_sx
from shared.infrastructure.urls import market_product_url
d = getattr(g, "order_detail_data", None)
if not d:
@@ -402,16 +408,15 @@ async def _h_order_detail_content(order_id=None, **kw):
"cost_formatted": f"{e.cost or 0:.2f}",
})
return sx_call("order-detail-content",
order=SxExpr(serialize(order_dict)),
calendar_entries=SxExpr(serialize(cal_dicts)) if cal_dicts else None)
return await render_to_sx("order-detail-content",
order=order_dict,
calendar_entries=cal_dicts)
async def _h_order_detail_filter(order_id=None, **kw):
await _ensure_order_detail(order_id)
from quart import g
from shared.sx.helpers import sx_call, SxExpr
from shared.sx.parser import serialize
from shared.sx.helpers import render_to_sx
d = getattr(g, "order_detail_data", None)
if not d:
return ""
@@ -422,8 +427,8 @@ async def _h_order_detail_filter(order_id=None, **kw):
"created_at_formatted": order.created_at.strftime("%-d %b %Y, %H:%M") if order.created_at else "\u2014",
}
return sx_call("order-detail-filter-content",
order=SxExpr(serialize(order_dict)),
return await render_to_sx("order-detail-filter-content",
order=order_dict,
list_url=d["list_url"],
recheck_url=d["recheck_url"],
pay_url=d["pay_url"],