Move events/market/blog composition from Python to .sx defcomps (Phase 9)
Some checks failed
Build and Deploy / build-and-deploy (push) Failing after 2m33s
Some checks failed
Build and Deploy / build-and-deploy (push) Failing after 2m33s
Continues the pattern of eliminating Python sx_call tree-building in favour of data-driven .sx defcomps. POST/PUT/DELETE routes now pass plain data (dicts, lists, scalars) and let .sx handle iteration, conditionals, and layout via map/let/when/if. Single response components wrap OOB swaps. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -11,7 +11,7 @@ from shared.sx.helpers import (
|
||||
full_page_sx, oob_page_sx,
|
||||
)
|
||||
|
||||
from .utils import _clear_deeper_oob, _product_detail_sx, _product_meta_sx
|
||||
from .utils import _product_detail_sx, _product_meta_sx
|
||||
from .cards import _product_cards_sx, _market_cards_sx
|
||||
from .filters import _desktop_filter_sx, _mobile_filter_summary_sx
|
||||
from .layouts import (
|
||||
@@ -25,15 +25,9 @@ from .helpers import _markets_admin_panel_sx
|
||||
# Browse page
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _product_grid(cards_sx: str) -> str:
|
||||
"""Wrap product cards in a grid as sx."""
|
||||
return sx_call("market-product-grid", cards=SxExpr(cards_sx))
|
||||
|
||||
|
||||
async def render_browse_page(ctx: dict) -> str:
|
||||
"""Full page: product browse with filters."""
|
||||
cards = _product_cards_sx(ctx)
|
||||
content = _product_grid(cards)
|
||||
content = sx_call("market-product-grid", cards=SxExpr(_product_cards_sx(ctx)))
|
||||
|
||||
from shared.sx.helpers import render_to_sx_with_env
|
||||
hdr = await render_to_sx_with_env("market-browse-layout-full", {})
|
||||
@@ -47,8 +41,7 @@ async def render_browse_page(ctx: dict) -> str:
|
||||
|
||||
async def render_browse_oob(ctx: dict) -> str:
|
||||
"""OOB response: product browse."""
|
||||
cards = _product_cards_sx(ctx)
|
||||
content = _product_grid(cards)
|
||||
content = sx_call("market-product-grid", cards=SxExpr(_product_cards_sx(ctx)))
|
||||
|
||||
# Layout handles all OOB headers via auto-fetch macros
|
||||
oobs = sx_call("market-browse-layout-oob")
|
||||
@@ -86,13 +79,10 @@ async def render_product_oob(ctx: dict, d: dict) -> str:
|
||||
"""OOB response: product detail."""
|
||||
content = _product_detail_sx(d, ctx)
|
||||
|
||||
oobs = sx_call("market-oob-wrap",
|
||||
parts=SxExpr("(<> " + _market_header_sx(ctx, oob=True) + " "
|
||||
+ await _oob_header_sx("market-header-child", "product-header-child",
|
||||
_product_header_sx(ctx, d)) + " "
|
||||
+ _clear_deeper_oob("post-row", "post-header-child",
|
||||
"market-row", "market-header-child",
|
||||
"product-row", "product-header-child") + ")"))
|
||||
oobs = sx_call("market-product-oob",
|
||||
market_header=SxExpr(_market_header_sx(ctx, oob=True)),
|
||||
oob_header=SxExpr(await _oob_header_sx("market-header-child", "product-header-child",
|
||||
_product_header_sx(ctx, d))))
|
||||
menu = _mobile_nav_panel_sx(ctx)
|
||||
return await oob_page_sx(oobs=oobs, content=content, menu=menu)
|
||||
|
||||
@@ -118,14 +108,10 @@ async def render_product_admin_oob(ctx: dict, d: dict) -> str:
|
||||
"""OOB response: product admin."""
|
||||
content = _product_detail_sx(d, ctx)
|
||||
|
||||
oobs = sx_call("market-oob-wrap",
|
||||
parts=SxExpr("(<> " + _product_header_sx(ctx, d, oob=True) + " "
|
||||
+ await _oob_header_sx("product-header-child", "product-admin-header-child",
|
||||
_product_admin_header_sx(ctx, d)) + " "
|
||||
+ _clear_deeper_oob("post-row", "post-header-child",
|
||||
"market-row", "market-header-child",
|
||||
"product-row", "product-header-child",
|
||||
"product-admin-row", "product-admin-header-child") + ")"))
|
||||
oobs = sx_call("market-product-admin-oob",
|
||||
product_header=SxExpr(_product_header_sx(ctx, d, oob=True)),
|
||||
oob_header=SxExpr(await _oob_header_sx("product-header-child", "product-admin-header-child",
|
||||
_product_admin_header_sx(ctx, d))))
|
||||
return await oob_page_sx(oobs=oobs, content=content)
|
||||
|
||||
|
||||
@@ -195,10 +181,7 @@ def render_like_toggle_button(slug: str, liked: bool, *,
|
||||
|
||||
|
||||
def render_cart_added_response(cart: list, item: Any, d: dict) -> str:
|
||||
"""Render the HTMX response after add-to-cart.
|
||||
|
||||
Returns OOB fragments: cart-mini icon + product add/remove buttons + cart item row.
|
||||
"""
|
||||
"""Render the HTMX response after add-to-cart via ~market-cart-added-response."""
|
||||
from shared.browser.app.csrf import generate_csrf_token
|
||||
from quart import url_for
|
||||
from shared.infrastructure.urls import cart_url as _cart_url
|
||||
@@ -206,37 +189,28 @@ def render_cart_added_response(cart: list, item: Any, d: dict) -> str:
|
||||
csrf = generate_csrf_token()
|
||||
slug = d.get("slug", "")
|
||||
count = sum(getattr(ci, "quantity", 0) for ci in cart)
|
||||
quantity = getattr(item, "quantity", 0) if item else 0
|
||||
action = url_for("market.browse.product.cart", product_slug=slug)
|
||||
|
||||
# 1. Cart mini icon OOB
|
||||
if count > 0:
|
||||
cart_href = _cart_url("/")
|
||||
cart_mini = sx_call("market-cart-mini-count", href=cart_href, count=str(count))
|
||||
blog_href = ""
|
||||
logo = ""
|
||||
else:
|
||||
from shared.config import config
|
||||
cart_href = ""
|
||||
blog_href = config().get("blog_url", "/")
|
||||
logo = config().get("logo", "")
|
||||
cart_mini = sx_call("market-cart-mini-empty", href=blog_href, logo=logo)
|
||||
|
||||
# 2. Add/remove buttons OOB
|
||||
action = url_for("market.browse.product.cart", product_slug=slug)
|
||||
quantity = getattr(item, "quantity", 0) if item else 0
|
||||
if not quantity:
|
||||
cart_add = sx_call(
|
||||
"market-cart-add-empty",
|
||||
cart_id=f"cart-{slug}", action=action, csrf=csrf,
|
||||
)
|
||||
else:
|
||||
cart_href = _cart_url("/") if callable(_cart_url) else "/"
|
||||
cart_add = sx_call(
|
||||
"market-cart-add-quantity",
|
||||
cart_id=f"cart-{slug}", action=action, csrf=csrf,
|
||||
minus_val=str(quantity - 1), plus_val=str(quantity + 1),
|
||||
quantity=str(quantity), cart_href=cart_href,
|
||||
)
|
||||
add_sx = sx_call(
|
||||
"market-cart-add-oob",
|
||||
id=f"cart-add-{slug}",
|
||||
inner=cart_add,
|
||||
return sx_call("market-cart-added-response",
|
||||
has_count=str(count) if count > 0 else None,
|
||||
cart_href=cart_href,
|
||||
blog_href=blog_href,
|
||||
logo=logo,
|
||||
slug=slug,
|
||||
action=action,
|
||||
csrf=csrf,
|
||||
quantity=str(quantity),
|
||||
minus_val=str(quantity - 1) if quantity > 0 else "0",
|
||||
plus_val=str(quantity + 1),
|
||||
)
|
||||
|
||||
return "(<> " + cart_mini + " " + add_sx + ")"
|
||||
|
||||
Reference in New Issue
Block a user