Refactor SX templates: shared components, Python migration, cleanup

- Extract shared components (empty-state, delete-btn, sentinel, crud-*,
  view-toggle, img-or-placeholder, avatar, sumup-settings-form, auth
  forms, order tables/detail/checkout)
- Migrate all Python sx_call() callers to use shared components directly
- Remove 55+ thin wrapper defcomps from domain .sx files
- Remove trivial passthrough wrappers (blog-header-label, market-card-text, etc)
- Unify duplicate auth flows (account + federation) into shared/sx/templates/auth.sx
- Unify duplicate order views (cart + orders) into shared/sx/templates/orders.sx
- Disable static file caching in dev (SEND_FILE_MAX_AGE_DEFAULT=0)
- Add SX response validation and debug headers

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-01 20:34:34 +00:00
parent 755313bd29
commit c0d369eb8e
58 changed files with 3473 additions and 1210 deletions

View File

@@ -166,7 +166,9 @@ def _page_group_card_sx(grp: Any, ctx: dict) -> str:
if feature_image:
img = sx_call("cart-group-card-img", src=feature_image, alt=title)
else:
img = sx_call("cart-group-card-placeholder")
img = sx_call("img-or-placeholder", src=None,
size_cls="h-16 w-16 rounded-xl",
placeholder_icon="fa fa-store text-xl")
mp_sub = ""
if market_place:
@@ -194,7 +196,13 @@ def _page_group_card_sx(grp: Any, ctx: dict) -> str:
def _empty_cart_sx() -> str:
"""Empty cart state."""
return sx_call("cart-empty")
empty = sx_call("empty-state", icon="fa fa-shopping-cart",
message="Your cart is empty", cls="text-center")
return (
'(div :class "max-w-full px-3 py-3 space-y-3"'
' (div :class "rounded-2xl border border-dashed border-stone-300 bg-white/80 p-6 sm:p-8 text-center"'
f' {empty}))'
)
def _overview_main_panel_sx(page_groups: list, ctx: dict) -> str:
@@ -232,7 +240,9 @@ def _cart_item_sx(item: Any, ctx: dict) -> str:
if p.image:
img = sx_call("cart-item-img", src=p.image, alt=p.title)
else:
img = sx_call("cart-item-no-img")
img = sx_call("img-or-placeholder", src=None,
size_cls="w-24 h-24 sm:w-32 sm:h-28 rounded-xl border border-dashed border-stone-300",
placeholder_text="No image")
price_parts = []
if unit_price:
@@ -381,7 +391,14 @@ def _page_cart_main_panel_sx(ctx: dict, cart: list, cal_entries: list,
ticket_total_fn: Any) -> str:
"""Page cart main panel."""
if not cart and not cal_entries and not tickets:
return sx_call("cart-page-empty")
empty = sx_call("empty-state", icon="fa fa-shopping-cart",
message="Your cart is empty", cls="text-center")
return (
'(div :class "max-w-full px-3 py-3 space-y-3"'
' (div :id "cart"'
' (div :class "rounded-2xl border border-dashed border-stone-300 bg-white/80 p-6 sm:p-8 text-center"'
f' {empty})))'
)
item_parts = [_cart_item_sx(item, ctx) for item in cart]
items_sx = "(<> " + " ".join(item_parts) + ")" if item_parts else '""'
@@ -416,16 +433,16 @@ def _order_row_sx(order: Any, detail_url: str) -> str:
total = f"{order.currency or 'GBP'} {order.total_amount or 0:.2f}"
desktop = sx_call(
"cart-order-row-desktop",
order_id=f"#{order.id}", created=created, desc=order.description or "",
total=total, pill=pill_cls, status=status, detail_url=detail_url,
"order-row-desktop",
oid=f"#{order.id}", created=created, desc=order.description or "",
total=total, pill=pill_cls, status=status, url=detail_url,
)
mobile_pill = f"inline-flex items-center rounded-full border px-2 py-0.5 text-[11px] {pill}"
mobile = sx_call(
"cart-order-row-mobile",
order_id=f"#{order.id}", pill=mobile_pill, status=status,
created=created, total=total, detail_url=detail_url,
"order-row-mobile",
oid=f"#{order.id}", pill=mobile_pill, status=status,
created=created, total=total, url=detail_url,
)
return "(<> " + desktop + " " + mobile + ")"
@@ -450,7 +467,7 @@ def _orders_rows_sx(orders: list, page: int, total_pages: int,
id_prefix="orders", colspan=5,
))
else:
parts.append(sx_call("cart-orders-end"))
parts.append(sx_call("order-end-row"))
return "(<> " + " ".join(parts) + ")"
@@ -458,13 +475,13 @@ def _orders_rows_sx(orders: list, page: int, total_pages: int,
def _orders_main_panel_sx(orders: list, rows_sx: str) -> str:
"""Main panel for orders list."""
if not orders:
return sx_call("cart-orders-empty")
return sx_call("cart-orders-table", rows=SxExpr(rows_sx))
return sx_call("order-empty-state")
return sx_call("order-table", rows=SxExpr(rows_sx))
def _orders_summary_sx(ctx: dict) -> str:
"""Filter section for orders list."""
return sx_call("cart-orders-filter", search_mobile=SxExpr(search_mobile_sx(ctx)))
return sx_call("order-list-header", search_mobile=SxExpr(search_mobile_sx(ctx)))
# ---------------------------------------------------------------------------
@@ -480,21 +497,21 @@ def _order_items_sx(order: Any) -> str:
prod_url = market_product_url(item.product_slug)
if item.product_image:
img = sx_call(
"cart-order-item-img",
"order-item-image",
src=item.product_image, alt=item.product_title or "Product image",
)
else:
img = sx_call("cart-order-item-no-img")
img = sx_call("order-item-no-image")
parts.append(sx_call(
"cart-order-item",
prod_url=prod_url, img=SxExpr(img),
"order-item-row",
href=prod_url, img=SxExpr(img),
title=item.product_title or "Unknown product",
product_id=f"Product ID: {item.product_id}",
pid=f"Product ID: {item.product_id}",
qty=f"Qty: {item.quantity}",
price=f"{item.currency or order.currency or 'GBP'} {item.unit_price or 0:.2f}",
))
items_sx = "(<> " + " ".join(parts) + ")"
return sx_call("cart-order-items-panel", items=SxExpr(items_sx))
return sx_call("order-items-panel", items=SxExpr(items_sx))
def _order_summary_sx(order: Any) -> str:
@@ -526,12 +543,12 @@ def _order_calendar_items_sx(calendar_entries: list | None) -> str:
if e.end_at:
ds += f" \u2013 {e.end_at.strftime('%-d %b %Y, %H:%M')}"
parts.append(sx_call(
"cart-order-cal-entry",
"order-calendar-entry",
name=e.name, pill=pill_cls, status=st.capitalize(),
date_str=ds, cost=f"\u00a3{e.cost or 0:.2f}",
))
items_sx = "(<> " + " ".join(parts) + ")"
return sx_call("cart-order-cal-section", items=SxExpr(items_sx))
return sx_call("order-calendar-section", items=SxExpr(items_sx))
def _order_main_sx(order: Any, calendar_entries: list | None) -> str:
@@ -540,10 +557,10 @@ def _order_main_sx(order: Any, calendar_entries: list | None) -> str:
items = _order_items_sx(order)
cal = _order_calendar_items_sx(calendar_entries)
return sx_call(
"cart-order-main",
"order-detail-panel",
summary=SxExpr(summary),
items=SxExpr(items) if items else None,
cal=SxExpr(cal) if cal else None,
calendar=SxExpr(cal) if cal else None,
)
@@ -555,10 +572,10 @@ def _order_filter_sx(order: Any, list_url: str, recheck_url: str,
pay_sx = None
if status != "paid":
pay_sx = sx_call("cart-order-pay-btn", url=pay_url)
pay_sx = sx_call("order-pay-btn", url=pay_url)
return sx_call(
"cart-order-filter",
"order-detail-filter",
info=f"Placed {created} \u00b7 Status: {status}",
list_url=list_url, recheck_url=recheck_url, csrf=csrf_token,
pay=SxExpr(pay_sx) if pay_sx else None,
@@ -598,8 +615,8 @@ async def render_page_cart_page(ctx: dict, page_post: Any,
child = _cart_header_sx(ctx)
page_hdr = _page_cart_header_sx(ctx, page_post)
nested = sx_call(
"cart-header-child-nested",
outer=SxExpr(child), inner=SxExpr(page_hdr),
"header-child-sx",
inner=SxExpr("(<> " + child + " " + sx_call("header-child-sx", id="cart-header-child", inner=SxExpr(page_hdr)) + ")"),
)
header_rows = "(<> " + hdr + " " + nested + ")"
return full_page_sx(ctx, header_rows=header_rows, content=main)
@@ -612,8 +629,9 @@ async def render_page_cart_oob(ctx: dict, page_post: Any,
"""OOB response for page cart."""
main = _page_cart_main_panel_sx(ctx, cart, cal_entries, tickets, ticket_groups,
total_fn, cal_total_fn, ticket_total_fn)
child_oob = sx_call("cart-header-child-oob",
inner=SxExpr(_page_cart_header_sx(ctx, page_post)))
child_oob = sx_call("oob-header-sx",
parent_id="cart-header-child",
row=SxExpr(_page_cart_header_sx(ctx, page_post)))
cart_hdr_oob = _cart_header_sx(ctx, oob=True)
root_hdr_oob = root_header_sx(ctx, oob=True)
oobs = "(<> " + child_oob + " " + cart_hdr_oob + " " + root_hdr_oob + ")"
@@ -642,8 +660,8 @@ async def render_orders_page(ctx: dict, orders: list, page: int,
auth = _auth_header_sx(ctx)
orders_hdr = _orders_header_sx(ctx, list_url)
auth_child = sx_call(
"cart-auth-header-child",
auth=SxExpr(auth), orders=SxExpr(orders_hdr),
"header-child-sx",
inner=SxExpr("(<> " + auth + " " + sx_call("header-child-sx", id="auth-header-child", inner=SxExpr(orders_hdr)) + ")"),
)
header_rows = "(<> " + hdr + " " + auth_child + ")"
@@ -676,8 +694,9 @@ async def render_orders_oob(ctx: dict, orders: list, page: int,
auth_oob = _auth_header_sx(ctx, oob=True)
auth_child_oob = sx_call(
"cart-auth-header-child-oob",
inner=SxExpr(_orders_header_sx(ctx, list_url)),
"oob-header-sx",
parent_id="auth-header-child",
row=SxExpr(_orders_header_sx(ctx, list_url)),
)
root_oob = root_header_sx(ctx, oob=True)
oobs = "(<> " + auth_oob + " " + auth_child_oob + " " + root_oob + ")"
@@ -715,10 +734,10 @@ async def render_order_page(ctx: dict, order: Any,
link_href=detail_url, link_label=f"Order {order.id}", icon="fa fa-gbp",
)
order_child = sx_call(
"cart-order-header-child",
auth=SxExpr(_auth_header_sx(ctx)),
orders=SxExpr(_orders_header_sx(ctx, list_url)),
order=SxExpr(order_row),
"header-child-sx",
inner=SxExpr("(<> " + _auth_header_sx(ctx) + " " + sx_call("header-child-sx", id="auth-header-child", inner=SxExpr(
"(<> " + _orders_header_sx(ctx, list_url) + " " + sx_call("header-child-sx", id="orders-header-child", inner=SxExpr(order_row)) + ")"
)) + ")"),
)
header_rows = "(<> " + hdr + " " + order_child + ")"
@@ -747,8 +766,9 @@ async def render_order_oob(ctx: dict, order: Any,
link_href=detail_url, link_label=f"Order {order.id}", icon="fa fa-gbp",
oob=True,
)
orders_child_oob = sx_call("cart-orders-header-child-oob",
inner=SxExpr(order_row_oob))
orders_child_oob = sx_call("oob-header-sx",
parent_id="orders-header-child",
row=SxExpr(order_row_oob))
root_oob = root_header_sx(ctx, oob=True)
oobs = "(<> " + orders_child_oob + " " + root_oob + ")"
@@ -760,18 +780,18 @@ async def render_order_oob(ctx: dict, order: Any,
# ---------------------------------------------------------------------------
def _checkout_error_filter_sx() -> str:
return sx_call("cart-checkout-error-filter")
return sx_call("checkout-error-header")
def _checkout_error_content_sx(error: str | None, order: Any | None) -> str:
err_msg = error or "Unexpected error while creating the hosted checkout session."
order_sx = None
if order:
order_sx = sx_call("cart-checkout-error-order-id", order_id=f"#{order.id}")
order_sx = sx_call("checkout-error-order-id", oid=f"#{order.id}")
back_url = cart_url("/")
return sx_call(
"cart-checkout-error-content",
error_msg=err_msg,
"checkout-error-content",
msg=err_msg,
order=SxExpr(order_sx) if order_sx else None,
back_url=back_url,
)