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:
@@ -6,49 +6,25 @@ from markupsafe import escape
|
||||
from shared.sx.helpers import sx_call
|
||||
from shared.sx.parser import SxExpr
|
||||
|
||||
from .utils import (
|
||||
_ticket_state_badge_html, _list_container, _cart_icon_oob,
|
||||
)
|
||||
from .utils import _list_container, _cart_icon_ctx
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Ticket widget (inline +/- for entry cards)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _ticket_widget_html(entry, qty: int, ticket_url: str, *, ctx: dict) -> str:
|
||||
"""Render the inline +/- ticket widget."""
|
||||
csrf_token_val = ""
|
||||
if ctx:
|
||||
ct = ctx.get("csrf_token")
|
||||
csrf_token_val = ct() if callable(ct) else (ct or "")
|
||||
else:
|
||||
try:
|
||||
from flask_wtf.csrf import generate_csrf
|
||||
csrf_token_val = generate_csrf()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def _ticket_widget_data(entry, qty: int, ticket_url: str) -> dict:
|
||||
"""Extract ticket widget data for sx composition."""
|
||||
from shared.browser.app.csrf import generate_csrf_token
|
||||
eid = entry.id
|
||||
tp = getattr(entry, "ticket_price", 0) or 0
|
||||
tgt = f"#page-ticket-{eid}"
|
||||
|
||||
def _tw_form(count_val, btn_html):
|
||||
return sx_call("events-tw-form",
|
||||
ticket_url=ticket_url, target=tgt,
|
||||
csrf=csrf_token_val, entry_id=str(eid),
|
||||
count_val=str(count_val), btn=btn_html)
|
||||
|
||||
if qty == 0:
|
||||
inner = _tw_form(1, sx_call("events-tw-cart-plus"))
|
||||
else:
|
||||
minus = _tw_form(qty - 1, sx_call("events-tw-minus"))
|
||||
cart_icon = sx_call("events-tw-cart-icon", qty=str(qty))
|
||||
plus = _tw_form(qty + 1, sx_call("events-tw-plus"))
|
||||
inner = minus + cart_icon + plus
|
||||
|
||||
return sx_call("events-tw-widget",
|
||||
entry_id=str(eid), price=f"\u00a3{tp:.2f}",
|
||||
inner=SxExpr(inner))
|
||||
return {
|
||||
"entry_id": str(eid),
|
||||
"price": f"\u00a3{tp:.2f}",
|
||||
"qty": qty,
|
||||
"ticket_url": ticket_url,
|
||||
"csrf": generate_csrf_token(),
|
||||
}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -56,37 +32,33 @@ def _ticket_widget_html(entry, qty: int, ticket_url: str, *, ctx: dict) -> str:
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _tickets_main_panel_html(ctx: dict, tickets: list) -> str:
|
||||
"""Render my tickets list."""
|
||||
"""Render my tickets list via data extraction + sx defcomp."""
|
||||
from quart import url_for
|
||||
|
||||
ticket_cards = []
|
||||
ticket_data = []
|
||||
if tickets:
|
||||
for ticket in tickets:
|
||||
href = url_for("defpage_ticket_detail", code=ticket.code)
|
||||
entry = getattr(ticket, "entry", None)
|
||||
entry_name = entry.name if entry else "Unknown event"
|
||||
tt = getattr(ticket, "ticket_type", None)
|
||||
state = getattr(ticket, "state", "")
|
||||
cal = getattr(entry, "calendar", None) if entry else None
|
||||
|
||||
time_str = ""
|
||||
if entry and entry.start_at:
|
||||
time_str = entry.start_at.strftime("%A, %B %d, %Y at %H:%M")
|
||||
if entry.end_at:
|
||||
time_str += f" \u2013 {entry.end_at.strftime('%H:%M')}"
|
||||
ticket_data.append({
|
||||
"href": url_for("defpage_ticket_detail", code=ticket.code),
|
||||
"entry-name": entry.name if entry else "Unknown event",
|
||||
"type-name": tt.name if tt else None,
|
||||
"time-str": time_str or None,
|
||||
"cal-name": cal.name if cal else None,
|
||||
"state": getattr(ticket, "state", ""),
|
||||
"code-prefix": ticket.code[:8],
|
||||
})
|
||||
|
||||
ticket_cards.append(sx_call("events-ticket-card",
|
||||
href=href, entry_name=entry_name,
|
||||
type_name=tt.name if tt else None,
|
||||
time_str=time_str or None,
|
||||
cal_name=cal.name if cal else None,
|
||||
badge=_ticket_state_badge_html(state),
|
||||
code_prefix=ticket.code[:8]))
|
||||
|
||||
cards_html = "".join(ticket_cards)
|
||||
return sx_call("events-tickets-panel",
|
||||
return sx_call("events-tickets-panel-from-data",
|
||||
list_container=_list_container(ctx),
|
||||
has_tickets=bool(tickets), cards=SxExpr(cards_html))
|
||||
tickets=ticket_data or None)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -94,7 +66,7 @@ def _tickets_main_panel_html(ctx: dict, tickets: list) -> str:
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _ticket_detail_panel_html(ctx: dict, ticket) -> str:
|
||||
"""Render a single ticket detail with QR code."""
|
||||
"""Render a single ticket detail with QR code via data + sx defcomp."""
|
||||
from quart import url_for
|
||||
|
||||
entry = getattr(ticket, "entry", None)
|
||||
@@ -105,22 +77,11 @@ def _ticket_detail_panel_html(ctx: dict, ticket) -> str:
|
||||
checked_in_at = getattr(ticket, "checked_in_at", None)
|
||||
|
||||
bg_map = {"confirmed": "bg-emerald-50", "checked_in": "bg-blue-50", "reserved": "bg-amber-50"}
|
||||
header_bg = bg_map.get(state, "bg-stone-50")
|
||||
entry_name = entry.name if entry else "Ticket"
|
||||
back_href = url_for("defpage_my_tickets")
|
||||
|
||||
# Badge with larger sizing
|
||||
badge = (_ticket_state_badge_html(state)).replace('px-2 py-0.5 text-xs', 'px-3 py-1 text-sm')
|
||||
|
||||
# Time info
|
||||
time_date = entry.start_at.strftime("%A, %B %d, %Y") if entry and entry.start_at else None
|
||||
time_range = entry.start_at.strftime("%H:%M") if entry and entry.start_at else None
|
||||
if time_range and entry.end_at:
|
||||
time_range += f" \u2013 {entry.end_at.strftime('%H:%M')}"
|
||||
|
||||
tt_desc = f"{tt.name} \u2014 \u00a3{tt.cost:.2f}" if tt and getattr(tt, "cost", None) else None
|
||||
checkin_str = checked_in_at.strftime("Checked in: %B %d, %Y at %H:%M") if checked_in_at else None
|
||||
|
||||
qr_script = (
|
||||
f"(function(){{var c=document.getElementById('ticket-qr-{code}');"
|
||||
"if(c&&typeof QRCode!=='undefined'){"
|
||||
@@ -129,13 +90,16 @@ def _ticket_detail_panel_html(ctx: dict, ticket) -> str:
|
||||
"}})()"
|
||||
)
|
||||
|
||||
return sx_call("events-ticket-detail",
|
||||
list_container=_list_container(ctx), back_href=back_href,
|
||||
header_bg=header_bg, entry_name=entry_name,
|
||||
badge=SxExpr(badge), type_name=tt.name if tt else None,
|
||||
return sx_call("events-ticket-detail-from-data",
|
||||
list_container=_list_container(ctx),
|
||||
back_href=url_for("defpage_my_tickets"),
|
||||
header_bg=bg_map.get(state, "bg-stone-50"),
|
||||
entry_name=entry.name if entry else "Ticket",
|
||||
state=state, type_name=tt.name if tt else None,
|
||||
code=code, time_date=time_date, time_range=time_range,
|
||||
cal_name=cal.name if cal else None,
|
||||
type_desc=tt_desc, checkin_str=checkin_str,
|
||||
type_desc=f"{tt.name} \u2014 \u00a3{tt.cost:.2f}" if tt and getattr(tt, "cost", None) else None,
|
||||
checkin_str=checked_in_at.strftime("Checked in: %B %d, %Y at %H:%M") if checked_in_at else None,
|
||||
qr_script=qr_script)
|
||||
|
||||
|
||||
@@ -144,62 +108,38 @@ def _ticket_detail_panel_html(ctx: dict, ticket) -> str:
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _ticket_admin_main_panel_html(ctx: dict, tickets: list, stats: dict) -> str:
|
||||
"""Render ticket admin dashboard."""
|
||||
"""Render ticket admin dashboard via data extraction + sx defcomp."""
|
||||
from quart import url_for
|
||||
csrf_token = ctx.get("csrf_token")
|
||||
csrf = csrf_token() if callable(csrf_token) else (csrf_token or "")
|
||||
lookup_url = url_for("ticket_admin.lookup")
|
||||
|
||||
# Stats cards
|
||||
stats_html = ""
|
||||
for label, key, border, bg, text_cls in [
|
||||
("Total", "total", "border-stone-200", "", "text-stone-900"),
|
||||
("Confirmed", "confirmed", "border-emerald-200", "bg-emerald-50", "text-emerald-700"),
|
||||
("Checked In", "checked_in", "border-blue-200", "bg-blue-50", "text-blue-700"),
|
||||
("Reserved", "reserved", "border-amber-200", "bg-amber-50", "text-amber-700"),
|
||||
]:
|
||||
val = stats.get(key, 0)
|
||||
lbl_cls = text_cls.replace("700", "600").replace("900", "500") if "stone" not in text_cls else "text-stone-500"
|
||||
stats_html += sx_call("events-ticket-admin-stat",
|
||||
border=border, bg=bg, text_cls=text_cls,
|
||||
label_cls=lbl_cls, value=str(val), label=label)
|
||||
|
||||
# Ticket rows
|
||||
rows_html = ""
|
||||
ticket_data = []
|
||||
for ticket in tickets:
|
||||
entry = getattr(ticket, "entry", None)
|
||||
tt = getattr(ticket, "ticket_type", None)
|
||||
state = getattr(ticket, "state", "")
|
||||
code = ticket.code
|
||||
checked_in_at = getattr(ticket, "checked_in_at", None)
|
||||
ticket_data.append({
|
||||
"code": code,
|
||||
"code-short": code[:12] + "...",
|
||||
"entry-name": entry.name if entry else "\u2014",
|
||||
"date-str": entry.start_at.strftime("%d %b %Y, %H:%M") if entry and entry.start_at else None,
|
||||
"type-name": tt.name if tt else "\u2014",
|
||||
"state": state,
|
||||
"checkin-url": url_for("ticket_admin.do_checkin", code=code) if state in ("confirmed", "reserved") else None,
|
||||
"csrf": csrf,
|
||||
"checked-in-time": checked_in_at.strftime("%H:%M") if checked_in_at else None,
|
||||
})
|
||||
|
||||
date_html = ""
|
||||
if entry and entry.start_at:
|
||||
date_html = sx_call("events-ticket-admin-date",
|
||||
date_str=entry.start_at.strftime("%d %b %Y, %H:%M"))
|
||||
|
||||
action_html = ""
|
||||
if state in ("confirmed", "reserved"):
|
||||
checkin_url = url_for("ticket_admin.do_checkin", code=code)
|
||||
action_html = sx_call("events-ticket-admin-checkin-form",
|
||||
checkin_url=checkin_url, code=code, csrf=csrf)
|
||||
elif state == "checked_in":
|
||||
checked_in_at = getattr(ticket, "checked_in_at", None)
|
||||
t_str = checked_in_at.strftime("%H:%M") if checked_in_at else ""
|
||||
action_html = sx_call("events-ticket-admin-checked-in",
|
||||
time_str=t_str)
|
||||
|
||||
rows_html += sx_call("events-ticket-admin-row",
|
||||
code=code, code_short=code[:12] + "...",
|
||||
entry_name=entry.name if entry else "\u2014",
|
||||
date=SxExpr(date_html),
|
||||
type_name=tt.name if tt else "\u2014",
|
||||
badge=_ticket_state_badge_html(state),
|
||||
action=SxExpr(action_html))
|
||||
|
||||
return sx_call("events-ticket-admin-panel",
|
||||
list_container=_list_container(ctx), stats=SxExpr(stats_html),
|
||||
lookup_url=lookup_url, has_tickets=bool(tickets),
|
||||
rows=SxExpr(rows_html))
|
||||
return sx_call("events-ticket-admin-panel-from-data",
|
||||
list_container=_list_container(ctx),
|
||||
lookup_url=url_for("ticket_admin.lookup"),
|
||||
tickets=ticket_data or None,
|
||||
total=stats.get("total", 0),
|
||||
confirmed=stats.get("confirmed", 0),
|
||||
checked_in=stats.get("checked_in", 0),
|
||||
reserved=stats.get("reserved", 0))
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -208,7 +148,8 @@ def _ticket_admin_main_panel_html(ctx: dict, tickets: list, stats: dict) -> str:
|
||||
|
||||
def render_ticket_widget(entry, qty: int, ticket_url: str) -> str:
|
||||
"""Render the +/- ticket widget for page_summary / all_events adjust_ticket."""
|
||||
return _ticket_widget_html(entry, qty, ticket_url, ctx={})
|
||||
data = _ticket_widget_data(entry, qty, ticket_url)
|
||||
return sx_call("events-tw-widget-from-data", **data)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -226,20 +167,13 @@ def render_checkin_result(success: bool, error: str | None, ticket) -> str:
|
||||
entry = getattr(ticket, "entry", None)
|
||||
tt = getattr(ticket, "ticket_type", None)
|
||||
checked_in_at = getattr(ticket, "checked_in_at", None)
|
||||
time_str = checked_in_at.strftime("%H:%M") if checked_in_at else "Just now"
|
||||
|
||||
date_html = ""
|
||||
if entry and entry.start_at:
|
||||
date_html = sx_call("events-ticket-admin-date",
|
||||
date_str=entry.start_at.strftime("%d %b %Y, %H:%M"))
|
||||
|
||||
return sx_call("events-checkin-success-row",
|
||||
return sx_call("events-checkin-success-row-from-data",
|
||||
code=code, code_short=code[:12] + "...",
|
||||
entry_name=entry.name if entry else "\u2014",
|
||||
date=SxExpr(date_html),
|
||||
date_str=entry.start_at.strftime("%d %b %Y, %H:%M") if entry and entry.start_at else None,
|
||||
type_name=tt.name if tt else "\u2014",
|
||||
badge=_ticket_state_badge_html("checked_in"),
|
||||
time_str=time_str)
|
||||
time_str=checked_in_at.strftime("%H:%M") if checked_in_at else "Just now")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -247,7 +181,7 @@ def render_checkin_result(success: bool, error: str | None, ticket) -> str:
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def render_lookup_result(ticket, error: str | None) -> str:
|
||||
"""Render ticket lookup result: error div or ticket info card."""
|
||||
"""Render ticket lookup result via data extraction + sx defcomp."""
|
||||
from quart import url_for
|
||||
from shared.browser.app.csrf import generate_csrf_token
|
||||
|
||||
@@ -261,38 +195,21 @@ def render_lookup_result(ticket, error: str | None) -> str:
|
||||
state = getattr(ticket, "state", "")
|
||||
code = ticket.code
|
||||
checked_in_at = getattr(ticket, "checked_in_at", None)
|
||||
csrf = generate_csrf_token()
|
||||
|
||||
# Info section
|
||||
info_html = sx_call("events-lookup-info",
|
||||
entry_name=entry.name if entry else "Unknown event")
|
||||
if tt:
|
||||
info_html += sx_call("events-lookup-type", type_name=tt.name)
|
||||
if entry and entry.start_at:
|
||||
info_html += sx_call("events-lookup-date",
|
||||
date_str=entry.start_at.strftime("%A, %B %d, %Y at %H:%M"))
|
||||
cal = getattr(entry, "calendar", None) if entry else None
|
||||
if cal:
|
||||
info_html += sx_call("events-lookup-cal", cal_name=cal.name)
|
||||
info_html += sx_call("events-lookup-status",
|
||||
badge=_ticket_state_badge_html(state), code=code)
|
||||
if checked_in_at:
|
||||
info_html += sx_call("events-lookup-checkin-time",
|
||||
date_str=checked_in_at.strftime("%B %d, %Y at %H:%M"))
|
||||
|
||||
# Action area
|
||||
action_html = ""
|
||||
checkin_url = None
|
||||
if state in ("confirmed", "reserved"):
|
||||
checkin_url = url_for("ticket_admin.do_checkin", code=code)
|
||||
action_html = sx_call("events-lookup-checkin-btn",
|
||||
checkin_url=checkin_url, code=code, csrf=csrf)
|
||||
elif state == "checked_in":
|
||||
action_html = sx_call("events-lookup-checked-in")
|
||||
elif state == "cancelled":
|
||||
action_html = sx_call("events-lookup-cancelled")
|
||||
|
||||
return sx_call("events-lookup-card",
|
||||
info=SxExpr(info_html), code=code, action=SxExpr(action_html))
|
||||
return sx_call("events-lookup-result-from-data",
|
||||
entry_name=entry.name if entry else "Unknown event",
|
||||
type_name=tt.name if tt else None,
|
||||
date_str=entry.start_at.strftime("%A, %B %d, %Y at %H:%M") if entry and entry.start_at else None,
|
||||
cal_name=cal.name if cal else None,
|
||||
state=state, code=code,
|
||||
checked_in_str=checked_in_at.strftime("%B %d, %Y at %H:%M") if checked_in_at else None,
|
||||
checkin_url=checkin_url,
|
||||
csrf=generate_csrf_token())
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -300,7 +217,7 @@ def render_lookup_result(ticket, error: str | None) -> str:
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def render_entry_tickets_admin(entry, tickets: list) -> str:
|
||||
"""Render admin ticket table for a specific entry."""
|
||||
"""Render admin ticket table via data extraction + sx defcomp."""
|
||||
from quart import url_for
|
||||
from shared.browser.app.csrf import generate_csrf_token
|
||||
csrf = generate_csrf_token()
|
||||
@@ -308,39 +225,29 @@ def render_entry_tickets_admin(entry, tickets: list) -> str:
|
||||
count = len(tickets)
|
||||
suffix = "s" if count != 1 else ""
|
||||
|
||||
rows_html = ""
|
||||
ticket_data = []
|
||||
for ticket in tickets:
|
||||
tt = getattr(ticket, "ticket_type", None)
|
||||
state = getattr(ticket, "state", "")
|
||||
code = ticket.code
|
||||
checked_in_at = getattr(ticket, "checked_in_at", None)
|
||||
|
||||
action_html = ""
|
||||
checkin_url = None
|
||||
if state in ("confirmed", "reserved"):
|
||||
checkin_url = url_for("ticket_admin.do_checkin", code=code)
|
||||
action_html = sx_call("events-entry-tickets-admin-checkin",
|
||||
checkin_url=checkin_url, code=code, csrf=csrf)
|
||||
elif state == "checked_in":
|
||||
t_str = checked_in_at.strftime("%H:%M") if checked_in_at else ""
|
||||
action_html = sx_call("events-ticket-admin-checked-in",
|
||||
time_str=t_str)
|
||||
ticket_data.append({
|
||||
"code": code,
|
||||
"code-short": code[:12] + "...",
|
||||
"type-name": tt.name if tt else "\u2014",
|
||||
"state": state,
|
||||
"checkin-url": checkin_url,
|
||||
"checked-in-time": checked_in_at.strftime("%H:%M") if checked_in_at else None,
|
||||
})
|
||||
|
||||
rows_html += sx_call("events-entry-tickets-admin-row",
|
||||
code=code, code_short=code[:12] + "...",
|
||||
type_name=tt.name if tt else "\u2014",
|
||||
badge=_ticket_state_badge_html(state),
|
||||
action=SxExpr(action_html))
|
||||
|
||||
if tickets:
|
||||
body_html = sx_call("events-entry-tickets-admin-table",
|
||||
rows=SxExpr(rows_html))
|
||||
else:
|
||||
body_html = sx_call("events-entry-tickets-admin-empty")
|
||||
|
||||
return sx_call("events-entry-tickets-admin-panel",
|
||||
return sx_call("events-entry-tickets-admin-from-data",
|
||||
entry_name=entry.name,
|
||||
count_label=f"{count} ticket{suffix}",
|
||||
body=body_html)
|
||||
tickets=ticket_data or None,
|
||||
csrf=csrf)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -383,7 +290,7 @@ def render_ticket_type_main_panel(ticket_type, entry, calendar, day, month, year
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def render_ticket_types_table(ticket_types, entry, calendar, day, month, year) -> str:
|
||||
"""Render ticket types table with rows and add button."""
|
||||
"""Render ticket types table via data extraction + sx defcomp."""
|
||||
from quart import url_for, g
|
||||
from shared.browser.app.csrf import generate_csrf_token
|
||||
csrf = generate_csrf_token()
|
||||
@@ -397,40 +304,38 @@ def render_ticket_types_table(ticket_types, entry, calendar, day, month, year) -
|
||||
cal_slug = getattr(calendar, "slug", "")
|
||||
eid = entry.id
|
||||
|
||||
rows_html = ""
|
||||
types_data = []
|
||||
if ticket_types:
|
||||
for tt in ticket_types:
|
||||
tt_href = url_for(
|
||||
"calendar.day.calendar_entries.calendar_entry.ticket_types.ticket_type.get",
|
||||
calendar_slug=cal_slug, year=year, month=month, day=day,
|
||||
entry_id=eid, ticket_type_id=tt.id,
|
||||
)
|
||||
del_url = url_for(
|
||||
"calendar.day.calendar_entries.calendar_entry.ticket_types.ticket_type.delete",
|
||||
calendar_slug=cal_slug, year=year, month=month, day=day,
|
||||
entry_id=eid, ticket_type_id=tt.id,
|
||||
)
|
||||
cost = getattr(tt, "cost", None)
|
||||
cost_str = f"\u00a3{cost:.2f}" if cost is not None else "\u00a30.00"
|
||||
|
||||
rows_html += sx_call("events-ticket-types-row",
|
||||
tr_cls=tr_cls, tt_href=tt_href,
|
||||
pill_cls=pill_cls, hx_select=hx_select,
|
||||
tt_name=tt.name, cost_str=cost_str,
|
||||
count=str(tt.count), action_btn=action_btn,
|
||||
del_url=del_url,
|
||||
csrf_hdr=f'{{"X-CSRFToken": "{csrf}"}}')
|
||||
else:
|
||||
rows_html = sx_call("events-ticket-types-empty-row")
|
||||
types_data.append({
|
||||
"tt-href": url_for(
|
||||
"calendar.day.calendar_entries.calendar_entry.ticket_types.ticket_type.get",
|
||||
calendar_slug=cal_slug, year=year, month=month, day=day,
|
||||
entry_id=eid, ticket_type_id=tt.id,
|
||||
),
|
||||
"tt-name": tt.name,
|
||||
"cost-str": f"\u00a3{cost:.2f}" if cost is not None else "\u00a30.00",
|
||||
"count": str(tt.count),
|
||||
"del-url": url_for(
|
||||
"calendar.day.calendar_entries.calendar_entry.ticket_types.ticket_type.delete",
|
||||
calendar_slug=cal_slug, year=year, month=month, day=day,
|
||||
entry_id=eid, ticket_type_id=tt.id,
|
||||
),
|
||||
})
|
||||
|
||||
add_url = url_for(
|
||||
"calendar.day.calendar_entries.calendar_entry.ticket_types.add_form",
|
||||
calendar_slug=cal_slug, entry_id=eid, year=year, month=month, day=day,
|
||||
)
|
||||
|
||||
return sx_call("events-ticket-types-table",
|
||||
list_container=list_container, rows=SxExpr(rows_html),
|
||||
action_btn=action_btn, add_url=add_url)
|
||||
return sx_call("events-ticket-types-table-from-data",
|
||||
list_container=list_container,
|
||||
ticket_types=types_data or None,
|
||||
action_btn=action_btn, add_url=add_url,
|
||||
tr_cls=tr_cls, pill_cls=pill_cls,
|
||||
hx_select=hx_select,
|
||||
csrf_hdr=f'{{"X-CSRFToken": "{csrf}"}}')
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -438,34 +343,22 @@ def render_ticket_types_table(ticket_types, entry, calendar, day, month, year) -
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def render_buy_result(entry, created_tickets, remaining, cart_count) -> str:
|
||||
"""Render buy result card with created tickets + OOB cart icon."""
|
||||
"""Render buy result card with OOB cart icon — single response component."""
|
||||
from quart import url_for
|
||||
|
||||
cart_html = _cart_icon_oob(cart_count)
|
||||
tickets = [
|
||||
{"href": url_for("defpage_ticket_detail", code=t.code),
|
||||
"code_short": t.code[:12] + "..."}
|
||||
for t in created_tickets
|
||||
]
|
||||
cart_ctx = _cart_icon_ctx(cart_count)
|
||||
|
||||
count = len(created_tickets)
|
||||
suffix = "s" if count != 1 else ""
|
||||
|
||||
tickets_html = ""
|
||||
for ticket in created_tickets:
|
||||
href = url_for("defpage_ticket_detail", code=ticket.code)
|
||||
tickets_html += sx_call("events-buy-result-ticket",
|
||||
href=href, code_short=ticket.code[:12] + "...")
|
||||
|
||||
remaining_html = ""
|
||||
if remaining is not None:
|
||||
r_suffix = "s" if remaining != 1 else ""
|
||||
remaining_html = sx_call("events-buy-result-remaining",
|
||||
text=f"{remaining} ticket{r_suffix} remaining")
|
||||
|
||||
my_href = url_for("defpage_my_tickets")
|
||||
|
||||
return cart_html + sx_call("events-buy-result",
|
||||
entry_id=str(entry.id),
|
||||
count_label=f"{count} ticket{suffix} reserved",
|
||||
tickets=SxExpr(tickets_html),
|
||||
remaining=SxExpr(remaining_html),
|
||||
my_tickets_href=my_href)
|
||||
return sx_call("events-buy-response",
|
||||
entry_id=str(entry.id),
|
||||
tickets=tickets,
|
||||
remaining=remaining,
|
||||
my_tickets_href=url_for("defpage_my_tickets"),
|
||||
**cart_ctx)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -474,90 +367,41 @@ def render_buy_result(entry, created_tickets, remaining, cart_count) -> str:
|
||||
|
||||
def render_buy_form(entry, ticket_remaining, ticket_sold_count,
|
||||
user_ticket_count, user_ticket_counts_by_type) -> str:
|
||||
"""Render the ticket buy/adjust form with +/- controls."""
|
||||
"""Render the ticket buy/adjust form — data only, .sx does layout."""
|
||||
from quart import url_for
|
||||
from shared.browser.app.csrf import generate_csrf_token
|
||||
csrf = generate_csrf_token()
|
||||
|
||||
eid = entry.id
|
||||
eid_s = str(eid)
|
||||
tp = getattr(entry, "ticket_price", None)
|
||||
state = getattr(entry, "state", "")
|
||||
ticket_types = getattr(entry, "ticket_types", None) or []
|
||||
|
||||
if tp is None:
|
||||
return ""
|
||||
|
||||
if state != "confirmed":
|
||||
return sx_call("events-buy-not-confirmed", entry_id=eid_s)
|
||||
ticket_types_orm = getattr(entry, "ticket_types", None) or []
|
||||
active_types = [tt for tt in ticket_types_orm if getattr(tt, "deleted_at", None) is None]
|
||||
|
||||
adjust_url = url_for("tickets.adjust_quantity")
|
||||
target = f"#ticket-buy-{eid}"
|
||||
types_data = [
|
||||
{"id": tt.id, "name": tt.name,
|
||||
"cost_str": f"\u00a3{tt.cost:.2f}" if tt.cost is not None else "\u00a30.00"}
|
||||
for tt in active_types
|
||||
]
|
||||
|
||||
# Info line
|
||||
info_html = ""
|
||||
info_items = ""
|
||||
if ticket_sold_count:
|
||||
info_items += sx_call("events-buy-info-sold",
|
||||
count=str(ticket_sold_count))
|
||||
if ticket_remaining is not None:
|
||||
info_items += sx_call("events-buy-info-remaining",
|
||||
count=str(ticket_remaining))
|
||||
if user_ticket_count:
|
||||
info_items += sx_call("events-buy-info-basket",
|
||||
count=str(user_ticket_count))
|
||||
if info_items:
|
||||
info_html = sx_call("events-buy-info-bar", items=SxExpr(info_items))
|
||||
# String keys so .sx can look up via (get counts (str id))
|
||||
counts_by_type = {}
|
||||
if user_ticket_counts_by_type:
|
||||
counts_by_type = {str(k): v for k, v in user_ticket_counts_by_type.items()}
|
||||
|
||||
active_types = [tt for tt in ticket_types if getattr(tt, "deleted_at", None) is None]
|
||||
|
||||
body_html = ""
|
||||
if active_types:
|
||||
type_items = ""
|
||||
for tt in active_types:
|
||||
type_count = user_ticket_counts_by_type.get(tt.id, 0) if user_ticket_counts_by_type else 0
|
||||
cost_str = f"\u00a3{tt.cost:.2f}" if tt.cost is not None else "\u00a30.00"
|
||||
type_items += sx_call("events-buy-type-item",
|
||||
type_name=tt.name, cost_str=cost_str,
|
||||
adjust_controls=_ticket_adjust_controls(csrf, adjust_url, target, eid, type_count, ticket_type_id=tt.id))
|
||||
body_html = sx_call("events-buy-types-wrapper", items=SxExpr(type_items))
|
||||
else:
|
||||
qty = user_ticket_count or 0
|
||||
body_html = sx_call("events-buy-default",
|
||||
price_str=f"\u00a3{tp:.2f}",
|
||||
adjust_controls=_ticket_adjust_controls(csrf, adjust_url, target, eid, qty))
|
||||
|
||||
return sx_call("events-buy-panel",
|
||||
entry_id=eid_s, info=SxExpr(info_html), body=body_html)
|
||||
|
||||
|
||||
def _ticket_adjust_controls(csrf, adjust_url, target, entry_id, count, *, ticket_type_id=None):
|
||||
"""Render +/- ticket controls for buy form."""
|
||||
from quart import url_for
|
||||
|
||||
tt_html = sx_call("events-adjust-tt-hidden",
|
||||
ticket_type_id=str(ticket_type_id)) if ticket_type_id else ""
|
||||
eid_s = str(entry_id)
|
||||
|
||||
def _adj_form(count_val, btn_html, *, extra_cls=""):
|
||||
return sx_call("events-adjust-form",
|
||||
adjust_url=adjust_url, target=target,
|
||||
extra_cls=extra_cls, csrf=csrf,
|
||||
entry_id=eid_s, tt=tt_html or None,
|
||||
count_val=str(count_val), btn=btn_html)
|
||||
|
||||
if count == 0:
|
||||
return _adj_form(1, sx_call("events-adjust-cart-plus"),
|
||||
extra_cls="flex items-center")
|
||||
|
||||
my_tickets_href = url_for("defpage_my_tickets")
|
||||
minus = _adj_form(count - 1, sx_call("events-adjust-minus"))
|
||||
cart_icon = sx_call("events-adjust-cart-icon",
|
||||
href=my_tickets_href, count=str(count))
|
||||
plus = _adj_form(count + 1, sx_call("events-adjust-plus"))
|
||||
|
||||
return sx_call("events-adjust-controls",
|
||||
minus=minus, cart_icon=cart_icon, plus=plus)
|
||||
return sx_call("events-buy-form",
|
||||
entry_id=entry.id,
|
||||
state=getattr(entry, "state", ""),
|
||||
price_str=f"\u00a3{tp:.2f}",
|
||||
adjust_url=url_for("tickets.adjust_quantity"),
|
||||
csrf=generate_csrf_token(),
|
||||
my_tickets_href=url_for("defpage_my_tickets"),
|
||||
info_sold=ticket_sold_count or None,
|
||||
info_remaining=ticket_remaining,
|
||||
info_basket=user_ticket_count or None,
|
||||
ticket_types=types_data if types_data else None,
|
||||
user_ticket_counts_by_type=counts_by_type if counts_by_type else None,
|
||||
user_ticket_count=user_ticket_count or 0)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -567,13 +411,44 @@ def _ticket_adjust_controls(csrf, adjust_url, target, entry_id, count, *, ticket
|
||||
def render_adjust_response(entry, ticket_remaining, ticket_sold_count,
|
||||
user_ticket_count, user_ticket_counts_by_type,
|
||||
cart_count) -> str:
|
||||
"""Render ticket adjust response: OOB cart icon + buy form."""
|
||||
cart_html = _cart_icon_oob(cart_count)
|
||||
form_html = render_buy_form(
|
||||
entry, ticket_remaining, ticket_sold_count,
|
||||
user_ticket_count, user_ticket_counts_by_type,
|
||||
)
|
||||
return cart_html + form_html
|
||||
"""Render ticket adjust response — single response component with OOB cart."""
|
||||
from quart import url_for
|
||||
from shared.browser.app.csrf import generate_csrf_token
|
||||
|
||||
tp = getattr(entry, "ticket_price", None)
|
||||
if tp is None:
|
||||
return ""
|
||||
|
||||
ticket_types_orm = getattr(entry, "ticket_types", None) or []
|
||||
active_types = [tt for tt in ticket_types_orm if getattr(tt, "deleted_at", None) is None]
|
||||
|
||||
types_data = [
|
||||
{"id": tt.id, "name": tt.name,
|
||||
"cost_str": f"\u00a3{tt.cost:.2f}" if tt.cost is not None else "\u00a30.00"}
|
||||
for tt in active_types
|
||||
]
|
||||
|
||||
# String keys so .sx can look up via (get counts (str id))
|
||||
counts_by_type = {}
|
||||
if user_ticket_counts_by_type:
|
||||
counts_by_type = {str(k): v for k, v in user_ticket_counts_by_type.items()}
|
||||
|
||||
cart_ctx = _cart_icon_ctx(cart_count)
|
||||
|
||||
return sx_call("events-adjust-response",
|
||||
entry_id=entry.id,
|
||||
state=getattr(entry, "state", ""),
|
||||
price_str=f"\u00a3{tp:.2f}",
|
||||
adjust_url=url_for("tickets.adjust_quantity"),
|
||||
csrf=generate_csrf_token(),
|
||||
my_tickets_href=url_for("defpage_my_tickets"),
|
||||
info_sold=ticket_sold_count or None,
|
||||
info_remaining=ticket_remaining,
|
||||
info_basket=user_ticket_count or None,
|
||||
ticket_types=types_data if types_data else None,
|
||||
user_ticket_counts_by_type=counts_by_type if counts_by_type else None,
|
||||
user_ticket_count=user_ticket_count or 0,
|
||||
**cart_ctx)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user