Remove render_to_sx from public API: enforce sx_call for all service code

Replace ~250 render_to_sx calls across all services with sync sx_call,
converting many async functions to sync where no other awaits remained.
Make render_to_sx/render_to_sx_with_env private (_render_to_sx).
Add (post-header-ctx) IO primitive and shared post/post-admin defmacros.
Convert built-in post/post-admin layouts from Python to register_sx_layout
with .sx defcomps. Remove dead post_admin_mobile_nav_sx.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-04 19:30:45 +00:00
parent 57e0d0c341
commit 959e63d440
61 changed files with 1352 additions and 1208 deletions

View File

@@ -11,7 +11,7 @@ from quart import (
)
from shared.browser.app.authz import require_admin, require_post_author
from markupsafe import escape
from shared.sx.helpers import sx_response, render_to_sx
from shared.sx.helpers import sx_response, sx_call
from shared.sx.parser import SxExpr, serialize as sx_serialize
from shared.utils import host_url
@@ -60,10 +60,10 @@ def _post_to_edit_dict(post) -> dict:
return d
async def _render_features(features, post, result):
def _render_features(features, post, result):
"""Render features panel via .sx defcomp."""
slug = post.get("slug", "")
return await render_to_sx("blog-features-panel-content",
return sx_call("blog-features-panel-content",
features_url=host_url(url_for("blog.post.admin.update_features", slug=slug)),
calendar_checked=bool(features.get("calendar")),
market_checked=bool(features.get("market")),
@@ -187,7 +187,7 @@ def _render_calendar_view(
return _raw_html_sx(html)
async def _render_associated_entries(all_calendars, associated_entry_ids, post_slug: str) -> str:
def _render_associated_entries(all_calendars, associated_entry_ids, post_slug: str) -> str:
"""Render the associated entries panel."""
from shared.browser.app.csrf import generate_csrf_token
from quart import url_for as qurl
@@ -216,13 +216,13 @@ async def _render_associated_entries(all_calendars, associated_entry_ids, post_s
toggle_url = host_url(qurl("blog.post.admin.toggle_entry", slug=post_slug, entry_id=e_id))
img_sx = await render_to_sx("blog-entry-image", src=cal_fi, title=cal_title)
img_sx = sx_call("blog-entry-image", src=cal_fi, title=cal_title)
date_str = e_start.strftime("%A, %B %d, %Y at %H:%M") if e_start else ""
if e_end:
date_str += f" \u2013 {e_end.strftime('%H:%M')}"
entry_items.append(await render_to_sx("blog-associated-entry",
entry_items.append(sx_call("blog-associated-entry",
confirm_text=f"This will remove {e_name} from this post",
toggle_url=toggle_url,
hx_headers=f'{{"X-CSRFToken": "{csrf}"}}',
@@ -231,16 +231,16 @@ async def _render_associated_entries(all_calendars, associated_entry_ids, post_s
))
if has_entries:
content_sx = await render_to_sx("blog-associated-entries-content",
content_sx = sx_call("blog-associated-entries-content",
items=SxExpr("(<> " + " ".join(entry_items) + ")"),
)
else:
content_sx = await render_to_sx("blog-associated-entries-empty")
content_sx = sx_call("blog-associated-entries-empty")
return await render_to_sx("blog-associated-entries-panel", content=SxExpr(content_sx))
return sx_call("blog-associated-entries-panel", content=SxExpr(content_sx))
async def _render_nav_entries_oob(associated_entries, calendars, post: dict) -> str:
def _render_nav_entries_oob(associated_entries, calendars, post: dict) -> str:
"""Render the OOB nav entries swap."""
entries_list = []
if associated_entries and hasattr(associated_entries, "entries"):
@@ -249,7 +249,7 @@ async def _render_nav_entries_oob(associated_entries, calendars, post: dict) ->
has_items = bool(entries_list or calendars)
if not has_items:
return await render_to_sx("blog-nav-entries-empty")
return sx_call("blog-nav-entries-empty")
select_colours = (
"[.hover-capable_&]:hover:bg-yellow-300"
@@ -291,7 +291,7 @@ async def _render_nav_entries_oob(associated_entries, calendars, post: dict) ->
entry_path = f"/{post_slug}/{cal_slug}/"
date_str = ""
item_parts.append(await render_to_sx("calendar-entry-nav",
item_parts.append(sx_call("calendar-entry-nav",
href=entry_path, nav_class=nav_cls, name=e_name, date_str=date_str,
))
@@ -300,13 +300,13 @@ async def _render_nav_entries_oob(associated_entries, calendars, post: dict) ->
cal_slug = getattr(calendar, "slug", "")
cal_path = f"/{post_slug}/{cal_slug}/"
item_parts.append(await render_to_sx("blog-nav-calendar-item",
item_parts.append(sx_call("blog-nav-calendar-item",
href=cal_path, nav_cls=nav_cls, name=cal_name,
))
items_sx = "(<> " + " ".join(item_parts) + ")" if item_parts else ""
return await render_to_sx("scroll-nav-wrapper",
return sx_call("scroll-nav-wrapper",
wrapper_id="entries-calendars-nav-wrapper", container_id="associated-items-container",
arrow_cls="entries-nav-arrow",
left_hs="on click set #associated-items-container.scrollLeft to #associated-items-container.scrollLeft - 200",
@@ -353,7 +353,7 @@ def register():
})
features = result.get("features", {})
html = await _render_features(features, post, result)
html = _render_features(features, post, result)
return sx_response(html)
@bp.put("/admin/sumup/")
@@ -386,7 +386,7 @@ def register():
result = await call_action("blog", "update-page-config", payload=payload)
features = result.get("features", {})
html = await _render_features(features, post, result)
html = _render_features(features, post, result)
return sx_response(html)
@bp.get("/entries/calendar/<int:calendar_id>/")
@@ -508,8 +508,8 @@ def register():
# Return the associated entries admin list + OOB update for nav entries
post = g.post_data["post"]
admin_list = await _render_associated_entries(all_calendars, associated_entry_ids, post["slug"])
nav_entries_html = await _render_nav_entries_oob(associated_entries, calendars, post)
admin_list = _render_associated_entries(all_calendars, associated_entry_ids, post["slug"])
nav_entries_html = _render_nav_entries_oob(associated_entries, calendars, post)
return sx_response(admin_list + nav_entries_html)
@@ -686,7 +686,7 @@ def register():
slug = post.get("slug", "")
create_url = host_url(url_for("blog.post.admin.create_market", slug=slug))
html = await render_to_sx("blog-markets-panel-content",
html = sx_call("blog-markets-panel-content",
markets=_serialize_markets(page_markets, slug), create_url=create_url)
return sx_response(html)
@@ -715,7 +715,7 @@ def register():
slug = post.get("slug", "")
create_url = host_url(url_for("blog.post.admin.create_market", slug=slug))
html = await render_to_sx("blog-markets-panel-content",
html = sx_call("blog-markets-panel-content",
markets=_serialize_markets(page_markets, slug), create_url=create_url)
return sx_response(html)
@@ -738,7 +738,7 @@ def register():
slug = post.get("slug", "")
create_url = host_url(url_for("blog.post.admin.create_market", slug=slug))
html = await render_to_sx("blog-markets-panel-content",
html = sx_call("blog-markets-panel-content",
markets=_serialize_markets(page_markets, slug), create_url=create_url)
return sx_response(html)

View File

@@ -106,7 +106,7 @@ def register():
async def post_detail(slug: str):
from shared.sx.page import get_template_context
from shared.sx.helpers import (
render_to_sx, root_header_sx, full_page_sx, oob_page_sx,
sx_call, root_header_sx, full_page_sx, oob_page_sx,
post_header_sx, oob_header_sx, mobile_menu_sx,
post_mobile_nav_sx, mobile_root_nav_sx,
)
@@ -124,9 +124,9 @@ def register():
csrf = generate_csrf_token()
svc = services.blog_page
detail_data = svc.post_detail_data(post, user, rights, csrf, blog_url_base)
content = await render_to_sx("blog-post-detail-content", **detail_data)
content = sx_call("blog-post-detail-content", **detail_data)
meta_data = svc.post_meta_data(post, tctx.get("base_title", ""))
meta = await render_to_sx("blog-meta", **meta_data)
meta = sx_call("blog-meta", **meta_data)
if not is_htmx_request():
root_hdr = await root_header_sx(tctx)
@@ -149,24 +149,24 @@ def register():
@clear_cache(tag="post.post_detail", tag_scope="user")
async def like_toggle(slug: str):
from shared.utils import host_url
from shared.sx.helpers import render_to_sx
from shared.sx.helpers import sx_call
from shared.browser.app.csrf import generate_csrf_token
like_url = host_url(url_for('blog.post.like_toggle', slug=slug))
csrf = generate_csrf_token()
async def _like_btn(liked):
def _like_btn(liked):
if liked:
colour, icon, label = "text-red-600", "fa-solid fa-heart", "Unlike this post"
else:
colour, icon, label = "text-stone-300", "fa-regular fa-heart", "Like this post"
return await render_to_sx("market-like-toggle-button",
return sx_call("market-like-toggle-button",
colour=colour, action=like_url,
hx_headers=f'{{"X-CSRFToken": "{csrf}"}}',
label=label, icon_cls=icon)
if not g.user:
return sx_response(await _like_btn(False), status=403)
return sx_response(_like_btn(False), status=403)
post_id = g.post_data["post"]["id"]
user_id = g.user.id
@@ -175,7 +175,7 @@ def register():
"user_id": user_id, "target_type": "post", "target_id": post_id,
})
return sx_response(await _like_btn(result["liked"]))
return sx_response(_like_btn(result["liked"]))
@bp.get("/w/<widget_domain>/")
async def widget_paginate(slug: str, widget_domain: str):