Move SX construction from Python to .sx defcomps (phases 0-4)
Eliminate Python s-expression string building across account, orders, federation, and cart services. Visual rendering logic now lives entirely in .sx defcomp components; Python files contain only data serialization, header/layout wiring, and thin wrappers that call defcomps. Phase 0: Shared DRY extraction — auth/orders header defcomps, format-decimal/ pluralize/escape/route-prefix primitives. Phase 1: Account — dashboard, newsletters, login/device/check-email content. Phase 2: Orders — order list, detail, filter, checkout return assembled defcomps. Phase 3: Federation — social nav, post cards, timeline, search, actors, notifications, compose, profile assembled defcomps. Phase 4: Cart — overview, page cart items/calendar/tickets/summary, admin, payments assembled defcomps; orders rendering reuses Phase 2 shared defcomps. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -29,24 +29,35 @@ def _register_orders_layouts() -> None:
|
||||
|
||||
|
||||
def _orders_full(ctx: dict, **kw: Any) -> str:
|
||||
from shared.sx.helpers import root_header_sx, header_child_sx
|
||||
from sx.sx_components import _auth_header_sx, _orders_header_sx
|
||||
from shared.sx.helpers import root_header_sx, header_child_sx, call_url, sx_call, SxExpr
|
||||
|
||||
list_url = kw.get("list_url", "/")
|
||||
account_url = call_url(ctx, "account_url", "")
|
||||
root_hdr = root_header_sx(ctx)
|
||||
inner = "(<> " + _auth_header_sx(ctx) + " " + _orders_header_sx(ctx, list_url) + ")"
|
||||
auth_hdr = sx_call("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)
|
||||
inner = "(<> " + auth_hdr + " " + orders_hdr + ")"
|
||||
return "(<> " + root_hdr + " " + header_child_sx(inner) + ")"
|
||||
|
||||
|
||||
def _orders_oob(ctx: dict, **kw: Any) -> str:
|
||||
from shared.sx.helpers import root_header_sx, sx_call, SxExpr
|
||||
from sx.sx_components import _auth_header_sx, _orders_header_sx
|
||||
from shared.sx.helpers import root_header_sx, sx_call, SxExpr, call_url
|
||||
|
||||
list_url = kw.get("list_url", "/")
|
||||
auth_hdr = _auth_header_sx(ctx, oob=True)
|
||||
account_url = call_url(ctx, "account_url", "")
|
||||
auth_hdr = sx_call("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(_orders_header_sx(ctx, list_url)))
|
||||
row=SxExpr(sx_call("orders-header-row", list_url=list_url)))
|
||||
root_hdr = root_header_sx(ctx, oob=True)
|
||||
return "(<> " + auth_hdr + " " + auth_child_oob + " " + root_hdr + ")"
|
||||
|
||||
@@ -57,21 +68,26 @@ def _orders_mobile(ctx: dict, **kw: Any) -> str:
|
||||
|
||||
|
||||
def _order_detail_full(ctx: dict, **kw: Any) -> str:
|
||||
from shared.sx.helpers import root_header_sx, sx_call, SxExpr
|
||||
from sx.sx_components import _auth_header_sx, _orders_header_sx
|
||||
from shared.sx.helpers import root_header_sx, sx_call, SxExpr, call_url
|
||||
|
||||
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(
|
||||
"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",
|
||||
account_url=account_url,
|
||||
select_colours=ctx.get("select_colours", ""),
|
||||
account_nav=_as_sx_nav(ctx),
|
||||
)
|
||||
detail_header = sx_call(
|
||||
"order-detail-header-stack",
|
||||
auth=SxExpr(_auth_header_sx(ctx)),
|
||||
orders=SxExpr(_orders_header_sx(ctx, list_url)),
|
||||
auth=SxExpr(auth_hdr),
|
||||
orders=SxExpr(sx_call("orders-header-row", list_url=list_url)),
|
||||
order=SxExpr(order_row),
|
||||
)
|
||||
return "(<> " + root_hdr + " " + detail_header + ")"
|
||||
@@ -98,6 +114,12 @@ def _order_detail_mobile(ctx: dict, **kw: Any) -> str:
|
||||
return mobile_menu_sx(mobile_root_nav_sx(ctx))
|
||||
|
||||
|
||||
def _as_sx_nav(ctx: dict) -> Any:
|
||||
"""Convert account_nav fragment to SxExpr for use in sx_call."""
|
||||
from shared.sx.helpers import _as_sx
|
||||
return _as_sx(ctx.get("account_nav"))
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Page helpers — Python functions callable from defpage expressions
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -257,14 +279,38 @@ 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
|
||||
d = getattr(g, "orders_page_data", None)
|
||||
if not d:
|
||||
from shared.sx.helpers import sx_call
|
||||
return sx_call("order-empty-state")
|
||||
from sx.sx_components import _orders_rows_sx, _orders_main_panel_sx
|
||||
rows = _orders_rows_sx(d["orders"], d["page"], d["total_pages"],
|
||||
d["url_for_fn"], d["qs_fn"])
|
||||
return _orders_main_panel_sx(d["orders"], rows)
|
||||
|
||||
orders = d["orders"]
|
||||
url_for_fn = d["url_for_fn"]
|
||||
pfx = d.get("list_url", "/").rsplit("/", 1)[0] if d.get("list_url") else ""
|
||||
|
||||
order_dicts = []
|
||||
for o in orders:
|
||||
order_dicts.append({
|
||||
"id": o.id,
|
||||
"status": o.status or "pending",
|
||||
"created_at_formatted": o.created_at.strftime("%-d %b %Y, %H:%M") if o.created_at else "\u2014",
|
||||
"description": o.description or "",
|
||||
"currency": o.currency or "GBP",
|
||||
"total_formatted": f"{o.total_amount or 0:.2f}",
|
||||
})
|
||||
|
||||
from shared.utils import route_prefix
|
||||
rpfx = route_prefix()
|
||||
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)),
|
||||
page=d["page"],
|
||||
total_pages=d["total_pages"],
|
||||
rows_url=rows_url,
|
||||
detail_url_prefix=detail_prefix)
|
||||
|
||||
|
||||
async def _h_orders_list_filter(**kw):
|
||||
@@ -312,22 +358,76 @@ 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.infrastructure.urls import market_product_url
|
||||
d = getattr(g, "order_detail_data", None)
|
||||
if not d:
|
||||
return ""
|
||||
from sx.sx_components import _order_main_sx
|
||||
return _order_main_sx(d["order"], d["calendar_entries"])
|
||||
|
||||
order = d["order"]
|
||||
order_dict = {
|
||||
"id": order.id,
|
||||
"status": order.status or "pending",
|
||||
"created_at_formatted": order.created_at.strftime("%-d %b %Y, %H:%M") if order.created_at else None,
|
||||
"description": order.description,
|
||||
"currency": order.currency,
|
||||
"total_formatted": f"{order.total_amount:.2f}" if order.total_amount else None,
|
||||
"items": [
|
||||
{
|
||||
"product_url": market_product_url(item.product_slug),
|
||||
"product_image": item.product_image,
|
||||
"product_title": item.product_title,
|
||||
"product_id": item.product_id,
|
||||
"quantity": item.quantity,
|
||||
"currency": item.currency,
|
||||
"unit_price_formatted": f"{item.unit_price or 0:.2f}",
|
||||
}
|
||||
for item in (order.items or [])
|
||||
],
|
||||
}
|
||||
|
||||
cal_entries = d["calendar_entries"]
|
||||
cal_dicts = None
|
||||
if cal_entries:
|
||||
cal_dicts = []
|
||||
for e in cal_entries:
|
||||
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_dicts.append({
|
||||
"name": e.name,
|
||||
"state": e.state or "",
|
||||
"date_str": ds,
|
||||
"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)
|
||||
|
||||
|
||||
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
|
||||
d = getattr(g, "order_detail_data", None)
|
||||
if not d:
|
||||
return ""
|
||||
from sx.sx_components import _order_filter_sx
|
||||
return _order_filter_sx(d["order"], d["list_url"], d["recheck_url"],
|
||||
d["pay_url"], d["csrf_token"])
|
||||
|
||||
order = d["order"]
|
||||
order_dict = {
|
||||
"status": order.status or "pending",
|
||||
"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)),
|
||||
list_url=d["list_url"],
|
||||
recheck_url=d["recheck_url"],
|
||||
pay_url=d["pay_url"],
|
||||
csrf=d["csrf_token"])
|
||||
|
||||
|
||||
async def _h_order_detail_url(order_id=None, **kw):
|
||||
|
||||
Reference in New Issue
Block a user