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>
409 lines
16 KiB
Python
409 lines
16 KiB
Python
"""
|
|
Cart service s-expression page components.
|
|
|
|
Thin Python wrappers for header/layout helpers and route-level render
|
|
functions. All visual rendering logic lives in .sx defcomps.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
from typing import Any
|
|
from markupsafe import escape
|
|
|
|
from shared.sx.jinja_bridge import load_service_components
|
|
from shared.sx.helpers import (
|
|
call_url, root_header_sx, post_admin_header_sx,
|
|
post_header_sx as _shared_post_header_sx,
|
|
search_desktop_sx, search_mobile_sx,
|
|
full_page_sx, oob_page_sx, header_child_sx,
|
|
render_to_sx,
|
|
)
|
|
from shared.sx.parser import SxExpr
|
|
from shared.infrastructure.urls import cart_url
|
|
|
|
# Load cart-specific .sx components + handlers at import time
|
|
load_service_components(os.path.dirname(os.path.dirname(__file__)),
|
|
service_name="cart")
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Header helpers (used by layouts in sxc/pages/__init__.py)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def _ensure_post_ctx(ctx: dict, page_post: Any) -> dict:
|
|
"""Ensure ctx has a 'post' dict from page_post DTO (for shared post_header_sx)."""
|
|
if ctx.get("post") or not page_post:
|
|
return ctx
|
|
ctx = {**ctx, "post": {
|
|
"id": getattr(page_post, "id", None),
|
|
"slug": getattr(page_post, "slug", ""),
|
|
"title": getattr(page_post, "title", ""),
|
|
"feature_image": getattr(page_post, "feature_image", None),
|
|
}}
|
|
return ctx
|
|
|
|
|
|
async def _ensure_container_nav(ctx: dict) -> dict:
|
|
"""Fetch container_nav if not already present (for post header row)."""
|
|
if ctx.get("container_nav"):
|
|
return ctx
|
|
post = ctx.get("post") or {}
|
|
post_id = post.get("id")
|
|
slug = post.get("slug", "")
|
|
if not post_id:
|
|
return ctx
|
|
from shared.infrastructure.fragments import fetch_fragments
|
|
nav_params = {
|
|
"container_type": "page",
|
|
"container_id": str(post_id),
|
|
"post_slug": slug,
|
|
}
|
|
events_nav, market_nav = await fetch_fragments([
|
|
("events", "container-nav", nav_params),
|
|
("market", "container-nav", nav_params),
|
|
], required=False)
|
|
return {**ctx, "container_nav": events_nav + market_nav}
|
|
|
|
|
|
async def _post_header_sx(ctx: dict, page_post: Any, *, oob: bool = False) -> str:
|
|
"""Build post-level header row from page_post DTO, using shared helper."""
|
|
ctx = _ensure_post_ctx(ctx, page_post)
|
|
ctx = await _ensure_container_nav(ctx)
|
|
return await _shared_post_header_sx(ctx, oob=oob)
|
|
|
|
|
|
async def _cart_header_sx(ctx: dict, *, oob: bool = False) -> str:
|
|
"""Build the cart section header row."""
|
|
return await render_to_sx(
|
|
"menu-row-sx",
|
|
id="cart-row", level=1, colour="sky",
|
|
link_href=call_url(ctx, "cart_url", "/"),
|
|
link_label="cart", icon="fa fa-shopping-cart",
|
|
child_id="cart-header-child", oob=oob,
|
|
)
|
|
|
|
|
|
async def _page_cart_header_sx(ctx: dict, page_post: Any, *, oob: bool = False) -> str:
|
|
"""Build the per-page cart header row."""
|
|
slug = page_post.slug if page_post else ""
|
|
title = ((page_post.title if page_post else None) or "")[:160]
|
|
label_parts = []
|
|
if page_post and page_post.feature_image:
|
|
label_parts.append(await render_to_sx("cart-page-label-img", src=page_post.feature_image))
|
|
label_parts.append(f'(span "{escape(title)}")')
|
|
label_sx = "(<> " + " ".join(label_parts) + ")"
|
|
nav_sx = await render_to_sx("cart-all-carts-link", href=call_url(ctx, "cart_url", "/"))
|
|
return await render_to_sx(
|
|
"menu-row-sx",
|
|
id="page-cart-row", level=2, colour="sky",
|
|
link_href=call_url(ctx, "cart_url", f"/{slug}/"),
|
|
link_label_content=SxExpr(label_sx),
|
|
nav=SxExpr(nav_sx), oob=oob,
|
|
)
|
|
|
|
|
|
async def _auth_header_sx(ctx: dict, *, oob: bool = False) -> str:
|
|
"""Build the account section header row (for orders)."""
|
|
return await render_to_sx(
|
|
"auth-header-row-simple",
|
|
account_url=call_url(ctx, "account_url", ""),
|
|
oob=oob,
|
|
)
|
|
|
|
|
|
async def _orders_header_sx(ctx: dict, list_url: str) -> str:
|
|
"""Build the orders section header row."""
|
|
return await render_to_sx("orders-header-row", list_url=list_url)
|
|
|
|
|
|
async def _cart_page_admin_header_sx(ctx: dict, page_post: Any, *, oob: bool = False,
|
|
selected: str = "") -> str:
|
|
"""Build the page-level admin header row."""
|
|
slug = page_post.slug if page_post else ""
|
|
ctx = _ensure_post_ctx(ctx, page_post)
|
|
return await post_admin_header_sx(ctx, slug, oob=oob, selected=selected)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Serialization helpers (shared with sxc/pages/__init__.py)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def _serialize_order(order: Any) -> dict:
|
|
"""Serialize an order for SX defcomps."""
|
|
from shared.infrastructure.urls import market_product_url
|
|
created = order.created_at.strftime("%-d %b %Y, %H:%M") if order.created_at else "\u2014"
|
|
items = []
|
|
if order.items:
|
|
for item in order.items:
|
|
items.append({
|
|
"product_image": item.product_image,
|
|
"product_title": item.product_title or "Unknown product",
|
|
"product_id": item.product_id,
|
|
"product_slug": item.product_slug,
|
|
"product_url": market_product_url(item.product_slug),
|
|
"quantity": item.quantity,
|
|
"unit_price_formatted": f"{item.unit_price or 0:.2f}",
|
|
"currency": item.currency or order.currency or "GBP",
|
|
})
|
|
return {
|
|
"id": order.id,
|
|
"status": order.status or "pending",
|
|
"created_at_formatted": created,
|
|
"description": order.description or "",
|
|
"total_formatted": f"{order.total_amount or 0:.2f}",
|
|
"total_amount": float(order.total_amount or 0),
|
|
"currency": order.currency or "GBP",
|
|
"items": items,
|
|
}
|
|
|
|
|
|
def _serialize_calendar_entry(e: Any) -> dict:
|
|
"""Serialize an order calendar entry for SX defcomps."""
|
|
st = e.state or ""
|
|
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')}"
|
|
return {
|
|
"name": e.name,
|
|
"state": st,
|
|
"date_str": ds,
|
|
"cost_formatted": f"{e.cost or 0:.2f}",
|
|
}
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Public API: Orders list (used by cart/bp/orders/routes.py)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
async def render_orders_page(ctx: dict, orders: list, page: int,
|
|
total_pages: int, search: str | None,
|
|
search_count: int, url_for_fn: Any,
|
|
qs_fn: Any) -> str:
|
|
"""Full page: orders list."""
|
|
from shared.utils import route_prefix
|
|
|
|
ctx["search"] = search
|
|
ctx["search_count"] = search_count
|
|
pfx = route_prefix()
|
|
list_url = pfx + url_for_fn("orders.list_orders")
|
|
rows_url = list_url
|
|
detail_url_prefix = pfx + url_for_fn("orders.order.order_detail", order_id=0).rsplit("0/", 1)[0]
|
|
|
|
order_dicts = [_serialize_order(o) for o in orders]
|
|
content = await render_to_sx("orders-list-content",
|
|
orders=order_dicts,
|
|
page=page, total_pages=total_pages,
|
|
rows_url=rows_url, detail_url_prefix=detail_url_prefix)
|
|
|
|
hdr = await root_header_sx(ctx)
|
|
auth = await _auth_header_sx(ctx)
|
|
orders_hdr = await _orders_header_sx(ctx, list_url)
|
|
auth_child_inner = await render_to_sx("header-child-sx", id="auth-header-child", inner=SxExpr(orders_hdr))
|
|
auth_child = await render_to_sx(
|
|
"header-child-sx",
|
|
inner=SxExpr("(<> " + auth + " " + auth_child_inner + ")"),
|
|
)
|
|
header_rows = "(<> " + hdr + " " + auth_child + ")"
|
|
|
|
filt = await render_to_sx("order-list-header", search_mobile=SxExpr(await search_mobile_sx(ctx)))
|
|
return await full_page_sx(ctx, header_rows=header_rows,
|
|
filter=filt,
|
|
aside=await search_desktop_sx(ctx),
|
|
content=content)
|
|
|
|
|
|
async def render_orders_rows(ctx: dict, orders: list, page: int,
|
|
total_pages: int, url_for_fn: Any,
|
|
qs_fn: Any) -> str:
|
|
"""Pagination: just the table rows."""
|
|
from shared.utils import route_prefix
|
|
|
|
pfx = route_prefix()
|
|
list_url = pfx + url_for_fn("orders.list_orders")
|
|
detail_url_prefix = pfx + url_for_fn("orders.order.order_detail", order_id=0).rsplit("0/", 1)[0]
|
|
|
|
order_dicts = [_serialize_order(o) for o in orders]
|
|
parts = []
|
|
for od in order_dicts:
|
|
parts.append(await render_to_sx("order-row-pair",
|
|
order=od,
|
|
detail_url_prefix=detail_url_prefix))
|
|
|
|
if page < total_pages:
|
|
next_url = list_url + qs_fn(page=page + 1)
|
|
parts.append(await render_to_sx(
|
|
"infinite-scroll",
|
|
url=next_url, page=page, total_pages=total_pages,
|
|
id_prefix="orders", colspan=5,
|
|
))
|
|
else:
|
|
parts.append(await render_to_sx("order-end-row"))
|
|
|
|
return "(<> " + " ".join(parts) + ")"
|
|
|
|
|
|
async def render_orders_oob(ctx: dict, orders: list, page: int,
|
|
total_pages: int, search: str | None,
|
|
search_count: int, url_for_fn: Any,
|
|
qs_fn: Any) -> str:
|
|
"""OOB response for orders list."""
|
|
from shared.utils import route_prefix
|
|
|
|
ctx["search"] = search
|
|
ctx["search_count"] = search_count
|
|
pfx = route_prefix()
|
|
list_url = pfx + url_for_fn("orders.list_orders")
|
|
rows_url = list_url
|
|
detail_url_prefix = pfx + url_for_fn("orders.order.order_detail", order_id=0).rsplit("0/", 1)[0]
|
|
|
|
order_dicts = [_serialize_order(o) for o in orders]
|
|
content = await render_to_sx("orders-list-content",
|
|
orders=order_dicts,
|
|
page=page, total_pages=total_pages,
|
|
rows_url=rows_url, detail_url_prefix=detail_url_prefix)
|
|
|
|
auth_oob = await _auth_header_sx(ctx, oob=True)
|
|
orders_hdr = await _orders_header_sx(ctx, list_url)
|
|
auth_child_oob = await render_to_sx(
|
|
"oob-header-sx",
|
|
parent_id="auth-header-child",
|
|
row=SxExpr(orders_hdr),
|
|
)
|
|
root_oob = await root_header_sx(ctx, oob=True)
|
|
oobs = "(<> " + auth_oob + " " + auth_child_oob + " " + root_oob + ")"
|
|
|
|
filt = await render_to_sx("order-list-header", search_mobile=SxExpr(await search_mobile_sx(ctx)))
|
|
return await oob_page_sx(oobs=oobs,
|
|
filter=filt,
|
|
aside=await search_desktop_sx(ctx),
|
|
content=content)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Public API: Single order detail (used by cart/bp/order/routes.py)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
async def render_order_page(ctx: dict, order: Any,
|
|
calendar_entries: list | None,
|
|
url_for_fn: Any) -> str:
|
|
"""Full page: single order detail."""
|
|
from shared.utils import route_prefix
|
|
from shared.browser.app.csrf import generate_csrf_token
|
|
|
|
pfx = route_prefix()
|
|
detail_url = pfx + url_for_fn("orders.order.order_detail", order_id=order.id)
|
|
list_url = pfx + url_for_fn("orders.list_orders")
|
|
recheck_url = pfx + url_for_fn("orders.order.order_recheck", order_id=order.id)
|
|
pay_url = pfx + url_for_fn("orders.order.order_pay", order_id=order.id)
|
|
|
|
order_data = _serialize_order(order)
|
|
cal_data = [_serialize_calendar_entry(e) for e in (calendar_entries or [])]
|
|
|
|
main = await render_to_sx("order-detail-content",
|
|
order=order_data,
|
|
calendar_entries=cal_data)
|
|
filt = await render_to_sx("order-detail-filter-content",
|
|
order=order_data,
|
|
list_url=list_url, recheck_url=recheck_url,
|
|
pay_url=pay_url, csrf=generate_csrf_token())
|
|
|
|
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=f"Order {order.id}", icon="fa fa-gbp",
|
|
)
|
|
auth = await _auth_header_sx(ctx)
|
|
orders_hdr = await _orders_header_sx(ctx, list_url)
|
|
orders_child = await render_to_sx("header-child-sx", id="orders-header-child", inner=SxExpr(order_row))
|
|
auth_inner = "(<> " + orders_hdr + " " + orders_child + ")"
|
|
auth_child = await render_to_sx("header-child-sx", id="auth-header-child", inner=SxExpr(auth_inner))
|
|
order_child = await render_to_sx(
|
|
"header-child-sx",
|
|
inner=SxExpr("(<> " + auth + " " + auth_child + ")"),
|
|
)
|
|
header_rows = "(<> " + hdr + " " + order_child + ")"
|
|
|
|
return await full_page_sx(ctx, header_rows=header_rows, filter=filt, content=main)
|
|
|
|
|
|
async def render_order_oob(ctx: dict, order: Any,
|
|
calendar_entries: list | None,
|
|
url_for_fn: Any) -> str:
|
|
"""OOB response for single order detail."""
|
|
from shared.utils import route_prefix
|
|
from shared.browser.app.csrf import generate_csrf_token
|
|
|
|
pfx = route_prefix()
|
|
detail_url = pfx + url_for_fn("orders.order.order_detail", order_id=order.id)
|
|
list_url = pfx + url_for_fn("orders.list_orders")
|
|
recheck_url = pfx + url_for_fn("orders.order.order_recheck", order_id=order.id)
|
|
pay_url = pfx + url_for_fn("orders.order.order_pay", order_id=order.id)
|
|
|
|
order_data = _serialize_order(order)
|
|
cal_data = [_serialize_calendar_entry(e) for e in (calendar_entries or [])]
|
|
|
|
main = await render_to_sx("order-detail-content",
|
|
order=order_data,
|
|
calendar_entries=cal_data)
|
|
filt = await render_to_sx("order-detail-filter-content",
|
|
order=order_data,
|
|
list_url=list_url, recheck_url=recheck_url,
|
|
pay_url=pay_url, csrf=generate_csrf_token())
|
|
|
|
order_row_oob = await render_to_sx(
|
|
"menu-row-sx",
|
|
id="order-row", level=3, colour="sky",
|
|
link_href=detail_url, link_label=f"Order {order.id}", icon="fa fa-gbp",
|
|
oob=True,
|
|
)
|
|
orders_child_oob = await render_to_sx("oob-header-sx",
|
|
parent_id="orders-header-child",
|
|
row=SxExpr(order_row_oob))
|
|
root_oob = await root_header_sx(ctx, oob=True)
|
|
oobs = "(<> " + orders_child_oob + " " + root_oob + ")"
|
|
|
|
return await oob_page_sx(oobs=oobs, filter=filt, content=main)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Public API: Checkout error (used by cart/bp/cart routes + order routes)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
async def render_checkout_error_page(ctx: dict, error: str | None = None,
|
|
order: Any | None = None) -> str:
|
|
"""Full page: checkout error."""
|
|
err_msg = error or "Unexpected error while creating the hosted checkout session."
|
|
order_sx = None
|
|
if order:
|
|
order_sx = await render_to_sx("checkout-error-order-id", oid=f"#{order.id}")
|
|
back_url = cart_url("/")
|
|
|
|
hdr = await root_header_sx(ctx)
|
|
filt = await render_to_sx("checkout-error-header")
|
|
content = await render_to_sx(
|
|
"checkout-error-content",
|
|
msg=err_msg,
|
|
order=SxExpr(order_sx) if order_sx else None,
|
|
back_url=back_url,
|
|
)
|
|
return await full_page_sx(ctx, header_rows=hdr, filter=filt, content=content)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Public API: POST response renderers
|
|
# ---------------------------------------------------------------------------
|
|
|
|
async def render_cart_payments_panel(ctx: dict) -> str:
|
|
"""Render the payments config panel for PUT response."""
|
|
page_config = ctx.get("page_config")
|
|
pc_data = None
|
|
if page_config:
|
|
pc_data = {
|
|
"sumup_api_key": bool(getattr(page_config, "sumup_api_key", None)),
|
|
"sumup_merchant_code": getattr(page_config, "sumup_merchant_code", None) or "",
|
|
"sumup_checkout_prefix": getattr(page_config, "sumup_checkout_prefix", None) or "",
|
|
}
|
|
return await render_to_sx("cart-payments-content",
|
|
page_config=pc_data)
|