Files
rose-ash/events/bp/page/routes.py
giles 22802bd36b
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 5m35s
Send all responses as sexp wire format with client-side rendering
- Server sends sexp source text, client (sexp.js) renders everything
- SexpExpr marker class for nested sexp composition in serialize()
- sexp_page() HTML shell with data-mount="body" for full page loads
- sexp_response() returns text/sexp for OOB/partial responses
- ~app-body layout component replaces ~app-layout (no raw!)
- ~rich-text is the only component using raw! (for CMS HTML content)
- Fragment endpoints return text/sexp, auto-wrapped in SexpExpr
- All _*_html() helpers converted to _*_sexp() returning sexp source
- Head auto-hoist: sexp.js moves meta/title/link/script[ld+json]
  from rendered body to document.head automatically
- Unknown components render warning box instead of crashing page
- Component kwargs preserve AST for lazy rendering (fixes <> in kwargs)
- Fix unterminated paren in events/sexp/tickets.sexpr

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-01 09:45:07 +00:00

115 lines
4.5 KiB
Python

"""
Page summary blueprint — shows upcoming events for a single page's calendars.
Routes:
GET /<slug>/ — full page scoped to this page
GET /<slug>/entries — HTMX fragment for infinite scroll
POST /<slug>/tickets/adjust — adjust ticket quantity inline
"""
from __future__ import annotations
from quart import Blueprint, g, request, render_template, make_response
from shared.browser.app.utils.htmx import is_htmx_request
from shared.sexp.helpers import sexp_response
from shared.infrastructure.cart_identity import current_cart_identity
from shared.services.registry import services
def register() -> Blueprint:
bp = Blueprint("page_summary", __name__)
async def _load_entries(post_id, page, per_page=20):
"""Load upcoming entries for this page + pending ticket counts."""
entries, has_more = await services.calendar.upcoming_entries_for_container(
g.s, "page", post_id, page=page, per_page=per_page,
)
# Pending ticket counts keyed by entry_id
ident = current_cart_identity()
pending_tickets = {}
if entries:
tickets = await services.calendar.pending_tickets(
g.s, user_id=ident["user_id"], session_id=ident["session_id"],
)
for t in tickets:
if t.entry_id is not None:
pending_tickets[t.entry_id] = pending_tickets.get(t.entry_id, 0) + 1
return entries, has_more, pending_tickets
@bp.get("/")
async def index():
post = g.post_data["post"]
view = request.args.get("view", "list")
page = int(request.args.get("page", 1))
entries, has_more, pending_tickets = await _load_entries(post["id"], page)
from shared.sexp.page import get_template_context
from sexp.sexp_components import render_page_summary_page, render_page_summary_oob
ctx = await get_template_context()
if is_htmx_request():
sexp_src = await render_page_summary_oob(ctx, entries, has_more, pending_tickets, {}, page, view)
return sexp_response(sexp_src)
else:
html = await render_page_summary_page(ctx, entries, has_more, pending_tickets, {}, page, view)
return await make_response(html, 200)
@bp.get("/entries")
async def entries_fragment():
post = g.post_data["post"]
view = request.args.get("view", "list")
page = int(request.args.get("page", 1))
entries, has_more, pending_tickets = await _load_entries(post["id"], page)
from sexp.sexp_components import render_page_summary_cards
sexp_src = await render_page_summary_cards(entries, has_more, pending_tickets, {}, page, view, post)
return sexp_response(sexp_src)
@bp.post("/tickets/adjust")
async def adjust_ticket():
"""Adjust ticket quantity, return updated widget + OOB cart-mini."""
ident = current_cart_identity()
form = await request.form
entry_id = int(form.get("entry_id", 0))
count = max(int(form.get("count", 0)), 0)
tt_raw = (form.get("ticket_type_id") or "").strip()
ticket_type_id = int(tt_raw) if tt_raw else None
await services.calendar.adjust_ticket_quantity(
g.s, entry_id, count,
user_id=ident["user_id"],
session_id=ident["session_id"],
ticket_type_id=ticket_type_id,
)
# Get updated ticket count for this entry
tickets = await services.calendar.pending_tickets(
g.s, user_id=ident["user_id"], session_id=ident["session_id"],
)
qty = sum(1 for t in tickets if t.entry_id == entry_id)
# Load entry DTO for the widget template
entry = await services.calendar.entry_by_id(g.s, entry_id)
# Commit so cross-service calls see the updated tickets
await g.tx.commit()
g.tx = await g.s.begin()
from shared.infrastructure.fragments import fetch_fragment
frag_params = {"oob": "1"}
if ident["user_id"] is not None:
frag_params["user_id"] = str(ident["user_id"])
if ident["session_id"] is not None:
frag_params["session_id"] = ident["session_id"]
from sexp.sexp_components import render_ticket_widget
widget_html = render_ticket_widget(entry, qty, f"/{g.post_slug}/tickets/adjust")
mini_html = await fetch_fragment("cart", "cart-mini", params=frag_params, required=False)
return sexp_response(widget_html + (mini_html or ""))
return bp