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:
@@ -73,11 +73,41 @@ def register(url_prefix: str) -> Blueprint:
|
||||
result = await g.s.execute(stmt)
|
||||
orders = result.scalars().all()
|
||||
|
||||
from sx.sx_components import _orders_rows_sx
|
||||
from shared.sx.helpers import sx_response
|
||||
from shared.sx.helpers import sx_response, sx_call, SxExpr
|
||||
from shared.sx.parser import serialize
|
||||
from shared.utils import route_prefix
|
||||
|
||||
pfx = route_prefix()
|
||||
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}",
|
||||
})
|
||||
|
||||
detail_prefix = pfx + url_for("orders.defpage_order_detail", order_id=0).rsplit("0/", 1)[0]
|
||||
qs_fn = makeqs_factory()
|
||||
sx_src = _orders_rows_sx(orders, page, total_pages, url_for, qs_fn)
|
||||
rows_url = pfx + url_for("orders.orders_rows")
|
||||
|
||||
# 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)),
|
||||
detail_url_prefix=detail_prefix))
|
||||
if page < total_pages:
|
||||
parts.append(sx_call("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"))
|
||||
sx_src = "(<> " + " ".join(parts) + ")"
|
||||
|
||||
resp = sx_response(sx_src)
|
||||
resp.headers["Hx-Push-Url"] = _current_url_without_page()
|
||||
return _vary(resp)
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
"""
|
||||
Orders service s-expression page components.
|
||||
|
||||
Each function renders a complete page section (full page, OOB, or pagination)
|
||||
using shared s-expression components. Called from route handlers in place
|
||||
of ``render_template()``.
|
||||
Checkout error/return pages are still rendered from Python because they
|
||||
use ``full_page_sx()`` with custom layouts. All other order rendering
|
||||
is now handled by .sx defcomps.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -12,8 +12,8 @@ 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, sx_call, SxExpr,
|
||||
root_header_sx, full_page_sx, header_child_sx,
|
||||
)
|
||||
from shared.infrastructure.urls import market_product_url, cart_url
|
||||
|
||||
@@ -22,260 +22,30 @@ load_service_components(os.path.dirname(os.path.dirname(__file__)),
|
||||
service_name="orders")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Header helpers (shared auth + orders-specific) — sx-native
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _auth_nav_sx(ctx: dict) -> str:
|
||||
"""Auth section desktop nav items as sx."""
|
||||
parts = [
|
||||
sx_call("nav-link",
|
||||
href=call_url(ctx, "account_url", "/newsletters/"),
|
||||
label="newsletters",
|
||||
select_colours=ctx.get("select_colours", ""),
|
||||
),
|
||||
]
|
||||
account_nav = ctx.get("account_nav")
|
||||
if account_nav:
|
||||
parts.append(str(account_nav))
|
||||
return "(<> " + " ".join(parts) + ")"
|
||||
|
||||
|
||||
def _auth_header_sx(ctx: dict, *, oob: bool = False) -> str:
|
||||
"""Build the account section header row as sx."""
|
||||
return sx_call(
|
||||
"menu-row-sx",
|
||||
id="auth-row", level=1, colour="sky",
|
||||
link_href=call_url(ctx, "account_url", "/"),
|
||||
link_label="account", icon="fa-solid fa-user",
|
||||
nav=SxExpr(_auth_nav_sx(ctx)),
|
||||
child_id="auth-header-child", oob=oob,
|
||||
)
|
||||
|
||||
|
||||
def _orders_header_sx(ctx: dict, list_url: str) -> str:
|
||||
"""Build the orders section header row as sx."""
|
||||
return sx_call(
|
||||
"menu-row-sx",
|
||||
id="orders-row", level=2, colour="sky",
|
||||
link_href=list_url, link_label="Orders", icon="fa fa-gbp",
|
||||
child_id="orders-header-child",
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Orders list rendering
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _status_pill_cls(status: str) -> str:
|
||||
"""Return Tailwind classes for order status pill."""
|
||||
sl = status.lower()
|
||||
if sl == "paid":
|
||||
return "border-emerald-300 bg-emerald-50 text-emerald-700"
|
||||
if sl in ("failed", "cancelled"):
|
||||
return "border-rose-300 bg-rose-50 text-rose-700"
|
||||
return "border-stone-300 bg-stone-50 text-stone-700"
|
||||
|
||||
|
||||
def _order_row_data(order: Any, detail_url: str) -> dict:
|
||||
"""Extract display data from an order model object."""
|
||||
status = order.status or "pending"
|
||||
pill = _status_pill_cls(status)
|
||||
created = order.created_at.strftime("%-d %b %Y, %H:%M") if order.created_at else "\u2014"
|
||||
total = f"{order.currency or 'GBP'} {order.total_amount or 0:.2f}"
|
||||
return dict(
|
||||
oid=f"#{order.id}", created=created,
|
||||
desc=order.description or "", total=total,
|
||||
pill_desktop=f"inline-flex items-center rounded-full border px-2 py-0.5 text-[11px] sm:text-xs {pill}",
|
||||
pill_mobile=f"inline-flex items-center rounded-full border px-2 py-0.5 text-[11px] {pill}",
|
||||
status=status, url=detail_url,
|
||||
)
|
||||
|
||||
|
||||
|
||||
def _orders_rows_sx(orders: list, page: int, total_pages: int,
|
||||
url_for_fn: Any, qs_fn: Any) -> str:
|
||||
"""S-expression wire format for order rows (client renders)."""
|
||||
from shared.utils import route_prefix
|
||||
pfx = route_prefix()
|
||||
|
||||
parts = []
|
||||
for o in orders:
|
||||
d = _order_row_data(o, pfx + url_for_fn("orders.defpage_order_detail", order_id=o.id))
|
||||
parts.append(sx_call("order-row-desktop",
|
||||
oid=d["oid"], created=d["created"],
|
||||
desc=d["desc"], total=d["total"],
|
||||
pill=d["pill_desktop"], status=d["status"],
|
||||
url=d["url"]))
|
||||
parts.append(sx_call("order-row-mobile",
|
||||
oid=d["oid"], created=d["created"],
|
||||
total=d["total"], pill=d["pill_mobile"],
|
||||
status=d["status"], url=d["url"]))
|
||||
|
||||
if page < total_pages:
|
||||
next_url = pfx + url_for_fn("orders.orders_rows") + qs_fn(page=page + 1)
|
||||
parts.append(sx_call("infinite-scroll",
|
||||
url=next_url, page=page,
|
||||
total_pages=total_pages,
|
||||
id_prefix="orders", colspan=5))
|
||||
else:
|
||||
parts.append(sx_call("order-end-row"))
|
||||
|
||||
return "(<> " + " ".join(parts) + ")"
|
||||
|
||||
|
||||
def _orders_main_panel_sx(orders: list, rows_sx: str) -> str:
|
||||
"""Main panel with table or empty state (sx)."""
|
||||
if not orders:
|
||||
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 (sx)."""
|
||||
return sx_call("order-list-header", search_mobile=SxExpr(search_mobile_sx(ctx)))
|
||||
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Public API: orders list
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Single order detail
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _order_items_sx(order: Any) -> str:
|
||||
"""Render order items list as sx."""
|
||||
if not order or not order.items:
|
||||
return ""
|
||||
items = []
|
||||
for item in order.items:
|
||||
prod_url = market_product_url(item.product_slug)
|
||||
if item.product_image:
|
||||
img = sx_call(
|
||||
"order-item-image",
|
||||
src=item.product_image, alt=item.product_title or "Product image",
|
||||
)
|
||||
else:
|
||||
img = sx_call("order-item-no-image")
|
||||
|
||||
items.append(sx_call(
|
||||
"order-item-row",
|
||||
href=prod_url, img=SxExpr(img),
|
||||
title=item.product_title or "Unknown product",
|
||||
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(items) + ")"
|
||||
return sx_call("order-items-panel", items=SxExpr(items_sx))
|
||||
|
||||
|
||||
def _calendar_items_sx(calendar_entries: list | None) -> str:
|
||||
"""Render calendar bookings for an order as sx."""
|
||||
if not calendar_entries:
|
||||
return ""
|
||||
items = []
|
||||
for e in calendar_entries:
|
||||
st = e.state or ""
|
||||
pill = (
|
||||
"bg-emerald-100 text-emerald-800" if st == "confirmed"
|
||||
else "bg-amber-100 text-amber-800" if st == "provisional"
|
||||
else "bg-blue-100 text-blue-800" if st == "ordered"
|
||||
else "bg-stone-100 text-stone-700"
|
||||
)
|
||||
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')}"
|
||||
items.append(sx_call(
|
||||
"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}",
|
||||
))
|
||||
|
||||
items_sx = "(<> " + " ".join(items) + ")"
|
||||
return sx_call("order-calendar-section", items=SxExpr(items_sx))
|
||||
|
||||
|
||||
def _order_main_sx(order: Any, calendar_entries: list | None) -> str:
|
||||
"""Main panel for single order detail (sx)."""
|
||||
summary = sx_call(
|
||||
"order-summary-card",
|
||||
order_id=order.id,
|
||||
created_at=order.created_at.strftime("%-d %b %Y, %H:%M") if order.created_at else None,
|
||||
description=order.description, status=order.status, currency=order.currency,
|
||||
total_amount=f"{order.total_amount:.2f}" if order.total_amount else None,
|
||||
)
|
||||
items = _order_items_sx(order)
|
||||
calendar = _calendar_items_sx(calendar_entries)
|
||||
return sx_call(
|
||||
"order-detail-panel",
|
||||
summary=SxExpr(summary),
|
||||
items=SxExpr(items) if items else None,
|
||||
calendar=SxExpr(calendar) if calendar else None,
|
||||
)
|
||||
|
||||
|
||||
def _order_filter_sx(order: Any, list_url: str, recheck_url: str,
|
||||
pay_url: str, csrf_token: str) -> str:
|
||||
"""Filter section for single order detail (sx)."""
|
||||
created = order.created_at.strftime("%-d %b %Y, %H:%M") if order.created_at else "\u2014"
|
||||
status = order.status or "pending"
|
||||
|
||||
pay = ""
|
||||
if status != "paid":
|
||||
pay = sx_call("order-pay-btn", url=pay_url)
|
||||
|
||||
return sx_call(
|
||||
"order-detail-filter",
|
||||
info=f"Placed {created} \u00b7 Status: {status}",
|
||||
list_url=list_url, recheck_url=recheck_url,
|
||||
csrf=csrf_token,
|
||||
pay=SxExpr(pay) if pay else None,
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Public API: Checkout error
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _checkout_error_filter_sx() -> str:
|
||||
return sx_call("checkout-error-header")
|
||||
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")
|
||||
|
||||
|
||||
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 = ""
|
||||
if order:
|
||||
order_sx = sx_call("checkout-error-order-id", oid=f"#{order.id}")
|
||||
back_url = cart_url("/")
|
||||
return sx_call(
|
||||
content = sx_call(
|
||||
"checkout-error-content",
|
||||
msg=err_msg,
|
||||
order=SxExpr(order_sx) if order_sx else None,
|
||||
back_url=back_url,
|
||||
)
|
||||
|
||||
|
||||
async def render_checkout_error_page(ctx: dict, error: str | None = None, order: Any | None = None) -> str:
|
||||
"""Full page: checkout error (sx wire format)."""
|
||||
hdr = root_header_sx(ctx)
|
||||
inner = _auth_header_sx(ctx)
|
||||
hdr = "(<> " + hdr + " " + header_child_sx(inner) + ")"
|
||||
filt = _checkout_error_filter_sx()
|
||||
content = _checkout_error_content_sx(error, order)
|
||||
return full_page_sx(ctx, header_rows=hdr, filter=filt, content=content)
|
||||
|
||||
|
||||
@@ -283,67 +53,127 @@ async def render_checkout_error_page(ctx: dict, error: str | None = None, order:
|
||||
# Public API: Checkout return
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _ticket_items_sx(order_tickets: list | None) -> str:
|
||||
"""Render ticket items for an order as sx."""
|
||||
if not order_tickets:
|
||||
return ""
|
||||
items = []
|
||||
for tk in order_tickets:
|
||||
st = tk.state or ""
|
||||
pill = (
|
||||
"bg-emerald-100 text-emerald-800" if st == "confirmed"
|
||||
else "bg-amber-100 text-amber-800" if st == "reserved"
|
||||
else "bg-blue-100 text-blue-800" if st == "checked_in"
|
||||
else "bg-stone-100 text-stone-700"
|
||||
)
|
||||
pill_cls = f"inline-flex items-center rounded-full px-2 py-0.5 text-[11px] font-medium {pill}"
|
||||
ds = tk.entry_start_at.strftime("%-d %b %Y, %H:%M") if tk.entry_start_at else ""
|
||||
if tk.entry_end_at:
|
||||
ds += f" – {tk.entry_end_at.strftime('%-d %b %Y, %H:%M')}"
|
||||
items.append(sx_call(
|
||||
"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"£{tk.price or 0:.2f}",
|
||||
))
|
||||
items_sx = "(<> " + " ".join(items) + ")"
|
||||
return sx_call("checkout-return-tickets", items=SxExpr(items_sx))
|
||||
|
||||
|
||||
async def render_checkout_return_page(ctx: dict, order: Any | None,
|
||||
status: str,
|
||||
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
|
||||
|
||||
filt = sx_call("checkout-return-header", status=status)
|
||||
|
||||
if not order:
|
||||
content = sx_call("checkout-return-missing")
|
||||
else:
|
||||
summary = sx_call(
|
||||
"order-summary-card",
|
||||
# Serialize order data for defcomp
|
||||
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 [])
|
||||
],
|
||||
}
|
||||
|
||||
summary = sx_call("order-summary-card",
|
||||
order_id=order.id,
|
||||
created_at=order.created_at.strftime("%-d %b %Y, %H:%M") if order.created_at else None,
|
||||
created_at=order_dict["created_at_formatted"],
|
||||
description=order.description, status=order.status,
|
||||
currency=order.currency,
|
||||
total_amount=f"{order.total_amount:.2f}" if order.total_amount else None,
|
||||
total_amount=order_dict["total_formatted"],
|
||||
)
|
||||
items = _order_items_sx(order)
|
||||
calendar = _calendar_items_sx(calendar_entries)
|
||||
tickets = _ticket_items_sx(order_tickets)
|
||||
|
||||
# Items
|
||||
items = ""
|
||||
if order.items:
|
||||
item_parts = []
|
||||
for item_d in order_dict["items"]:
|
||||
if item_d["product_image"]:
|
||||
img = sx_call("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",
|
||||
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=SxExpr("(<> " + " ".join(item_parts) + ")"))
|
||||
|
||||
# Calendar entries
|
||||
calendar = ""
|
||||
if calendar_entries:
|
||||
cal_parts = []
|
||||
for e in calendar_entries:
|
||||
st = e.state or ""
|
||||
pill = (
|
||||
"bg-emerald-100 text-emerald-800" if st == "confirmed"
|
||||
else "bg-amber-100 text-amber-800" if st == "provisional"
|
||||
else "bg-blue-100 text-blue-800" if st == "ordered"
|
||||
else "bg-stone-100 text-stone-700"
|
||||
)
|
||||
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",
|
||||
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",
|
||||
items=SxExpr("(<> " + " ".join(cal_parts) + ")"))
|
||||
|
||||
# Tickets
|
||||
tickets = ""
|
||||
if order_tickets:
|
||||
tk_parts = []
|
||||
for tk in order_tickets:
|
||||
st = tk.state or ""
|
||||
pill = (
|
||||
"bg-emerald-100 text-emerald-800" if st == "confirmed"
|
||||
else "bg-amber-100 text-amber-800" if st == "reserved"
|
||||
else "bg-blue-100 text-blue-800" if st == "checked_in"
|
||||
else "bg-stone-100 text-stone-700"
|
||||
)
|
||||
pill_cls = f"inline-flex items-center rounded-full px-2 py-0.5 text-[11px] font-medium {pill}"
|
||||
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",
|
||||
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",
|
||||
items=SxExpr("(<> " + " ".join(tk_parts) + ")"))
|
||||
|
||||
# Status message
|
||||
status_msg = ""
|
||||
if order.status == "failed":
|
||||
status_msg = sx_call("checkout-return-failed", order_id=order.id)
|
||||
elif order.status == "paid":
|
||||
status_msg = sx_call("checkout-return-paid")
|
||||
|
||||
content = sx_call(
|
||||
"checkout-return-content",
|
||||
content = sx_call("checkout-return-content",
|
||||
summary=SxExpr(summary),
|
||||
items=SxExpr(items) if items else None,
|
||||
calendar=SxExpr(calendar) if calendar else None,
|
||||
@@ -351,8 +181,9 @@ async def render_checkout_return_page(ctx: dict, order: Any | None,
|
||||
status_message=SxExpr(status_msg) if status_msg else None,
|
||||
)
|
||||
|
||||
account_url = call_url(ctx, "account_url", "")
|
||||
auth_hdr = sx_call("auth-header-row", account_url=account_url)
|
||||
hdr = root_header_sx(ctx)
|
||||
inner = _auth_header_sx(ctx)
|
||||
hdr = "(<> " + hdr + " " + header_child_sx(inner) + ")"
|
||||
hdr = "(<> " + hdr + " " + header_child_sx(auth_hdr) + ")"
|
||||
|
||||
return full_page_sx(ctx, header_rows=hdr, filter=filt, content=content)
|
||||
|
||||
@@ -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