Migrate ~52 GET route handlers across all 7 services from Jinja render_template() to s-expression component rendering. Each service gets a sexp_components.py with page/oob/cards render functions. - Add per-service sexp_components.py (account, blog, cart, events, federation, market, orders) with full page, OOB, and pagination card rendering - Add shared/sexp/helpers.py with call_url, root_header_html, full_page, oob_page utilities - Update all GET routes to use get_template_context() + render fns - Fix get_template_context() to inject Jinja globals (URL helpers) - Add qs_filter to base_context for sexp filter URL building - Mount sexp_components.py in docker-compose.dev.yml for all services - Import sexp_components in app.py for Hypercorn --reload watching - Fix route_prefix import (shared.utils not shared.infrastructure.urls) - Fix federation choose-username missing actor in context - Fix market page_markets missing post in context Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
386 lines
18 KiB
Python
386 lines
18 KiB
Python
"""
|
|
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()``.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from shared.sexp.jinja_bridge import sexp, register_components
|
|
from shared.sexp.helpers import (
|
|
call_url, get_asset_url, root_header_html,
|
|
search_mobile_html, search_desktop_html, full_page, oob_page,
|
|
)
|
|
from shared.sexp.page import HAMBURGER_HTML
|
|
from shared.infrastructure.urls import market_product_url
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Service-specific component definitions
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def load_orders_components() -> None:
|
|
"""Register orders-specific s-expression components (placeholder for future)."""
|
|
pass
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Header helpers (shared auth + orders-specific)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def _auth_nav_html(ctx: dict) -> str:
|
|
"""Auth section desktop nav items."""
|
|
html = sexp(
|
|
'(~nav-link :href h :label "newsletters" :select-colours sc)',
|
|
h=call_url(ctx, "account_url", "/newsletters/"),
|
|
sc=ctx.get("select_colours", ""),
|
|
)
|
|
account_nav_html = ctx.get("account_nav_html", "")
|
|
if account_nav_html:
|
|
html += account_nav_html
|
|
return html
|
|
|
|
|
|
def _auth_header_html(ctx: dict, *, oob: bool = False) -> str:
|
|
"""Build the account section header row."""
|
|
return sexp(
|
|
'(~menu-row :id "auth-row" :level 1 :colour "sky"'
|
|
' :link-href lh :link-label "account" :icon "fa-solid fa-user"'
|
|
' :nav-html nh :child-id "auth-header-child" :oob oob)',
|
|
lh=call_url(ctx, "account_url", "/"),
|
|
nh=_auth_nav_html(ctx),
|
|
oob=oob,
|
|
)
|
|
|
|
|
|
def _orders_header_html(ctx: dict, list_url: str) -> str:
|
|
"""Build the orders section header row."""
|
|
return sexp(
|
|
'(~menu-row :id "orders-row" :level 2 :colour "sky"'
|
|
' :link-href lh :link-label "Orders" :icon "fa fa-gbp"'
|
|
' :child-id "orders-header-child")',
|
|
lh=list_url,
|
|
)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Orders list rendering
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def _order_row_html(order: Any, detail_url: str) -> str:
|
|
"""Render a single order as desktop table row + mobile card."""
|
|
status = order.status or "pending"
|
|
sl = status.lower()
|
|
pill = (
|
|
"border-emerald-300 bg-emerald-50 text-emerald-700" if sl == "paid"
|
|
else "border-rose-300 bg-rose-50 text-rose-700" if sl in ("failed", "cancelled")
|
|
else "border-stone-300 bg-stone-50 text-stone-700"
|
|
)
|
|
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 (
|
|
# Desktop row
|
|
f'<tr class="hidden sm:table-row border-t border-stone-100 hover:bg-stone-50/60">'
|
|
f'<td class="px-3 py-2 align-top"><span class="font-mono text-[11px] sm:text-xs">#{order.id}</span></td>'
|
|
f'<td class="px-3 py-2 align-top text-stone-700 text-xs sm:text-sm">{created}</td>'
|
|
f'<td class="px-3 py-2 align-top text-stone-700 text-xs sm:text-sm">{order.description or ""}</td>'
|
|
f'<td class="px-3 py-2 align-top text-stone-700 text-xs sm:text-sm">{total}</td>'
|
|
f'<td class="px-3 py-2 align-top"><span class="inline-flex items-center rounded-full border px-2 py-0.5 text-[11px] sm:text-xs {pill}">{status}</span></td>'
|
|
f'<td class="px-3 py-0.5 align-top text-right"><a href="{detail_url}" class="inline-flex items-center px-3 py-1.5 text-xs sm:text-sm rounded-full border border-stone-300 bg-white hover:bg-stone-50 transition">View</a></td></tr>'
|
|
# Mobile row
|
|
f'<tr class="sm:hidden border-t border-stone-100"><td colspan="5" class="px-3 py-3"><div class="flex flex-col gap-2 text-xs">'
|
|
f'<div class="flex items-center justify-between gap-2"><span class="font-mono text-[11px] text-stone-700">#{order.id}</span>'
|
|
f'<span class="inline-flex items-center rounded-full border px-2 py-0.5 text-[11px] {pill}">{status}</span></div>'
|
|
f'<div class="text-[11px] text-stone-500 break-words">{created}</div>'
|
|
f'<div class="flex items-center justify-between gap-2"><div class="font-medium text-stone-800">{total}</div>'
|
|
f'<a href="{detail_url}" class="inline-flex items-center px-2 py-1 text-[11px] rounded-full border border-stone-300 bg-white hover:bg-stone-50 transition shrink-0">View</a></div></div></td></tr>'
|
|
)
|
|
|
|
|
|
def _orders_rows_html(orders: list, page: int, total_pages: int,
|
|
url_for_fn: Any, qs_fn: Any) -> str:
|
|
"""Render order rows + infinite scroll sentinel."""
|
|
from shared.utils import route_prefix
|
|
pfx = route_prefix()
|
|
|
|
parts = [
|
|
_order_row_html(o, pfx + url_for_fn("orders.order.order_detail", order_id=o.id))
|
|
for o in orders
|
|
]
|
|
|
|
if page < total_pages:
|
|
next_url = pfx + url_for_fn("orders.list_orders") + qs_fn(page=page + 1)
|
|
parts.append(sexp(
|
|
'(~infinite-scroll :url u :page p :total-pages tp :id-prefix "orders" :colspan 5)',
|
|
u=next_url, p=page, **{"total-pages": total_pages},
|
|
))
|
|
else:
|
|
parts.append('<tr><td colspan="5" class="px-3 py-4 text-center text-xs text-stone-400">End of results</td></tr>')
|
|
|
|
return "".join(parts)
|
|
|
|
|
|
def _orders_main_panel_html(orders: list, rows_html: str) -> str:
|
|
"""Main panel with table or empty state."""
|
|
if not orders:
|
|
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-4 sm:p-6 text-sm text-stone-700">'
|
|
'No orders yet.</div></div>'
|
|
)
|
|
return (
|
|
'<div class="max-w-full px-3 py-3 space-y-3">'
|
|
'<div class="overflow-x-auto rounded-2xl border border-stone-200 bg-white/80">'
|
|
'<table class="min-w-full text-xs sm:text-sm">'
|
|
'<thead class="bg-stone-50 border-b border-stone-200 text-stone-600"><tr>'
|
|
'<th class="px-3 py-2 text-left font-medium">Order</th>'
|
|
'<th class="px-3 py-2 text-left font-medium">Created</th>'
|
|
'<th class="px-3 py-2 text-left font-medium">Description</th>'
|
|
'<th class="px-3 py-2 text-left font-medium">Total</th>'
|
|
'<th class="px-3 py-2 text-left font-medium">Status</th>'
|
|
'<th class="px-3 py-2 text-left font-medium"></th>'
|
|
f'</tr></thead><tbody>{rows_html}</tbody></table></div></div>'
|
|
)
|
|
|
|
|
|
def _orders_summary_html(ctx: dict) -> str:
|
|
"""Filter section for orders list."""
|
|
return (
|
|
'<header class="mb-6 sm:mb-8 flex flex-col sm:flex-row sm:items-center justify-between gap-3 sm:gap-4">'
|
|
'<div class="space-y-1"><p class="text-xs sm:text-sm text-stone-600">Recent orders placed via the checkout.</p></div>'
|
|
f'<div class="md:hidden">{search_mobile_html(ctx)}</div>'
|
|
'</header>'
|
|
)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Public API: orders list
|
|
# ---------------------------------------------------------------------------
|
|
|
|
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
|
|
list_url = route_prefix() + url_for_fn("orders.list_orders")
|
|
|
|
rows = _orders_rows_html(orders, page, total_pages, url_for_fn, qs_fn)
|
|
main = _orders_main_panel_html(orders, rows)
|
|
|
|
hdr = root_header_html(ctx)
|
|
hdr += sexp(
|
|
'(div :id "root-header-child" :class "flex flex-col w-full items-center" (raw! a) (raw! o))',
|
|
a=_auth_header_html(ctx), o=_orders_header_html(ctx, list_url),
|
|
)
|
|
|
|
return full_page(ctx, header_rows_html=hdr,
|
|
filter_html=_orders_summary_html(ctx),
|
|
aside_html=search_desktop_html(ctx),
|
|
content_html=main)
|
|
|
|
|
|
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."""
|
|
return _orders_rows_html(orders, page, total_pages, url_for_fn, qs_fn)
|
|
|
|
|
|
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 HTMX navigation to orders list."""
|
|
from shared.utils import route_prefix
|
|
|
|
ctx["search"] = search
|
|
ctx["search_count"] = search_count
|
|
list_url = route_prefix() + url_for_fn("orders.list_orders")
|
|
|
|
rows = _orders_rows_html(orders, page, total_pages, url_for_fn, qs_fn)
|
|
main = _orders_main_panel_html(orders, rows)
|
|
|
|
oobs = (
|
|
_auth_header_html(ctx, oob=True)
|
|
+ sexp(
|
|
'(div :id "auth-header-child" :hx-swap-oob "outerHTML"'
|
|
' :class "flex flex-col w-full items-center" (raw! o))',
|
|
o=_orders_header_html(ctx, list_url),
|
|
)
|
|
+ root_header_html(ctx, oob=True)
|
|
)
|
|
|
|
return oob_page(ctx, oobs_html=oobs,
|
|
filter_html=_orders_summary_html(ctx),
|
|
aside_html=search_desktop_html(ctx),
|
|
content_html=main)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Single order detail
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def _order_items_html(order: Any) -> str:
|
|
"""Render order items list."""
|
|
if not order or not order.items:
|
|
return ""
|
|
items = []
|
|
for item in order.items:
|
|
prod_url = market_product_url(item.product_slug)
|
|
img = (
|
|
f'<img src="{item.product_image}" alt="{item.product_title or "Product image"}"'
|
|
f' class="w-full h-full object-contain object-center" loading="lazy" decoding="async">'
|
|
if item.product_image else
|
|
'<div class="w-full h-full flex items-center justify-center text-[9px] text-stone-400">No image</div>'
|
|
)
|
|
items.append(
|
|
f'<li><a class="w-full py-2 flex gap-3" href="{prod_url}">'
|
|
f'<div class="w-12 h-12 sm:w-14 sm:h-14 rounded-md bg-stone-100 flex-shrink-0 overflow-hidden">{img}</div>'
|
|
f'<div class="flex-1 flex justify-between gap-3">'
|
|
f'<div><p class="font-medium">{item.product_title or "Unknown product"}</p>'
|
|
f'<p class="text-[11px] text-stone-500">Product ID: {item.product_id}</p></div>'
|
|
f'<div class="text-right whitespace-nowrap"><p>Qty: {item.quantity}</p>'
|
|
f'<p>{item.currency or order.currency or "GBP"} {item.unit_price or 0:.2f}</p>'
|
|
f'</div></div></a></li>'
|
|
)
|
|
return (
|
|
'<div class="rounded-2xl border border-stone-200 bg-white/80 p-4 sm:p-6">'
|
|
'<h2 class="text-sm sm:text-base font-semibold mb-3">Items</h2>'
|
|
f'<ul class="divide-y divide-stone-100 text-xs sm:text-sm">{"".join(items)}</ul></div>'
|
|
)
|
|
|
|
|
|
def _calendar_items_html(calendar_entries: list | None) -> str:
|
|
"""Render calendar bookings for an order."""
|
|
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(
|
|
f'<li class="px-4 py-3 flex items-start justify-between text-sm">'
|
|
f'<div><div class="font-medium flex items-center gap-2">{e.name}'
|
|
f'<span class="inline-flex items-center rounded-full px-2 py-0.5 text-[11px] font-medium {pill}">'
|
|
f'{st.capitalize()}</span></div>'
|
|
f'<div class="text-xs text-stone-500">{ds}</div></div>'
|
|
f'<div class="ml-4 font-medium">\u00a3{e.cost or 0:.2f}</div></li>'
|
|
)
|
|
return (
|
|
'<section class="mt-6 space-y-3">'
|
|
'<h2 class="text-base sm:text-lg font-semibold">Calendar bookings in this order</h2>'
|
|
f'<ul class="divide-y divide-stone-200 rounded-2xl border border-stone-200 bg-white/80">{"".join(items)}</ul></section>'
|
|
)
|
|
|
|
|
|
def _order_main_html(order: Any, calendar_entries: list | None) -> str:
|
|
"""Main panel for single order detail."""
|
|
summary = sexp(
|
|
'(~order-summary-card :order-id oid :created-at ca :description d :status s :currency c :total-amount ta)',
|
|
oid=order.id,
|
|
ca=order.created_at.strftime("%-d %b %Y, %H:%M") if order.created_at else None,
|
|
d=order.description, s=order.status, c=order.currency,
|
|
ta=f"{order.total_amount:.2f}" if order.total_amount else None,
|
|
)
|
|
return f'<div class="max-w-full px-3 py-3 space-y-4">{summary}{_order_items_html(order)}{_calendar_items_html(calendar_entries)}</div>'
|
|
|
|
|
|
def _order_filter_html(order: Any, list_url: str, recheck_url: str,
|
|
pay_url: str, csrf_token: str) -> str:
|
|
"""Filter section for single order detail."""
|
|
created = order.created_at.strftime("%-d %b %Y, %H:%M") if order.created_at else "\u2014"
|
|
status = order.status or "pending"
|
|
pay = (
|
|
f'<a href="{pay_url}" class="inline-flex items-center px-3 py-2 text-xs sm:text-sm '
|
|
f'rounded-full border border-emerald-600 bg-emerald-600 text-white hover:bg-emerald-700 transition">'
|
|
f'<i class="fa fa-credit-card mr-2" aria-hidden="true"></i>Open payment page</a>'
|
|
) if status != "paid" else ""
|
|
|
|
return (
|
|
'<header class="mb-6 sm:mb-8 flex flex-col sm:flex-row sm:items-center justify-between gap-3 sm:gap-4">'
|
|
f'<div class="space-y-1"><p class="text-xs sm:text-sm text-stone-600">Placed {created} · Status: {status}</p></div>'
|
|
'<div class="flex w-full sm:w-auto justify-start sm:justify-end gap-2">'
|
|
f'<a href="{list_url}" class="inline-flex items-center px-3 py-2 text-xs sm:text-sm rounded-full border border-stone-300 bg-white hover:bg-stone-50 transition"><i class="fa-solid fa-list mr-2" aria-hidden="true"></i>All orders</a>'
|
|
f'<form method="post" action="{recheck_url}" class="inline"><input type="hidden" name="csrf_token" value="{csrf_token}">'
|
|
f'<button type="submit" class="inline-flex items-center px-3 py-2 text-xs sm:text-sm rounded-full border border-stone-300 bg-white hover:bg-stone-50 transition"><i class="fa-solid fa-rotate mr-2" aria-hidden="true"></i>Re-check status</button></form>'
|
|
f'{pay}</div></header>'
|
|
)
|
|
|
|
|
|
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)
|
|
|
|
main = _order_main_html(order, calendar_entries)
|
|
filt = _order_filter_html(order, list_url, recheck_url, pay_url, generate_csrf_token())
|
|
|
|
# Header stack: root -> auth -> orders -> order
|
|
hdr = root_header_html(ctx)
|
|
order_row = sexp(
|
|
'(~menu-row :id "order-row" :level 3 :colour "sky" :link-href lh :link-label "Order" :icon "fa fa-gbp")',
|
|
lh=detail_url,
|
|
)
|
|
hdr += sexp(
|
|
'(div :id "root-header-child" :class "flex flex-col w-full items-center" (raw! a)'
|
|
' (div :id "auth-header-child" :class "flex flex-col w-full items-center" (raw! b)'
|
|
' (div :id "orders-header-child" :class "flex flex-col w-full items-center" (raw! c))))',
|
|
a=_auth_header_html(ctx),
|
|
b=_orders_header_html(ctx, list_url),
|
|
c=order_row,
|
|
)
|
|
|
|
return full_page(ctx, header_rows_html=hdr, filter_html=filt, content_html=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)
|
|
|
|
main = _order_main_html(order, calendar_entries)
|
|
filt = _order_filter_html(order, list_url, recheck_url, pay_url, generate_csrf_token())
|
|
|
|
order_row_oob = sexp(
|
|
'(~menu-row :id "order-row" :level 3 :colour "sky" :link-href lh :link-label "Order" :icon "fa fa-gbp" :oob true)',
|
|
lh=detail_url,
|
|
)
|
|
oobs = (
|
|
sexp('(div :id "orders-header-child" :hx-swap-oob "outerHTML" :class "flex flex-col w-full items-center" (raw! o))', o=order_row_oob)
|
|
+ root_header_html(ctx, oob=True)
|
|
)
|
|
|
|
return oob_page(ctx, oobs_html=oobs, filter_html=filt, content_html=main)
|