Files
rose-ash/events/bp/slots/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

154 lines
4.8 KiB
Python

from __future__ import annotations
from quart import (
request, render_template, make_response, Blueprint, g, jsonify
)
from sqlalchemy.exc import IntegrityError
from shared.browser.app.authz import require_admin
from shared.browser.app.redis_cacher import clear_cache
from .services.slots import (
list_slots as svc_list_slots,
create_slot as svc_create_slot,
)
from ..slot.routes import register as register_slot
from shared.browser.app.utils import (
parse_time,
parse_cost
)
from shared.browser.app.utils.htmx import is_htmx_request
from shared.sexp.helpers import sexp_response
def register():
bp = Blueprint("slots", __name__, url_prefix='/slots')
# ---------- Pages ----------
bp.register_blueprint(
register_slot()
)
@bp.context_processor
async def get_slots():
calendar = getattr(g, "calendar", None)
if calendar:
return {
"slots": await svc_list_slots(g.s, calendar.id)
}
return {"slots": []}
@bp.get("/")
async def get(**kwargs):
from shared.sexp.page import get_template_context
from sexp.sexp_components import render_slots_page, render_slots_oob
tctx = await get_template_context()
if not is_htmx_request():
html = await render_slots_page(tctx)
return await make_response(html)
else:
sexp_src = await render_slots_oob(tctx)
return sexp_response(sexp_src)
@bp.post("/")
@require_admin
@clear_cache(tag="calendars", tag_scope="all")
async def post(**kwargs):
form = await request.form
name = (form.get("name") or "").strip()
description = (form.get("description") or "").strip() or None
days = {k: form.get(k) for k in ["mon", "tue", "wed", "thu", "fri", "sat", "sun"]}
time_start = parse_time(form.get("time_start"))
time_end = parse_time(form.get("time_end"))
cost = parse_cost(form.get("cost"))
# NEW: flexible flag from checkbox
flexible = bool(form.get("flexible"))
field_errors: dict[str, list[str]] = {}
if not name:
field_errors.setdefault("name", []).append("Please enter a name for the slot.")
if not time_start:
field_errors.setdefault("time_start", []).append("Please select a start time.")
if not time_end:
field_errors.setdefault("time_end", []).append("Please select an end time.")
if time_start and time_end and time_end <= time_start:
field_errors.setdefault("time_end", []).append("End time must be after the start time.")
if not any(form.get(d) for d in ["mon", "tue", "wed", "thu", "fri", "sat", "sun"]):
field_errors.setdefault("days", []).append("Please select at least one day.")
if field_errors:
return jsonify({
"message": "Please fix the highlighted fields.",
"errors": field_errors,
}), 422
# DB insert with friendly duplicate detection
try:
await svc_create_slot(
g.s,
g.calendar.id,
name=name,
description=description,
days=days,
time_start=time_start,
time_end=time_end,
cost=cost,
flexible=flexible, # <<< NEW
)
except IntegrityError as e:
# Improve duplicate detection: check constraint name or message
msg = str(e.orig) if getattr(e, "orig", None) else str(e)
if "uq_calendar_slots_unique_band" in msg or "duplicate key value" in msg:
field_errors = {
"name": [f"A slot called “{name}” already exists on this calendar."]
}
return jsonify({
"message": "That slot name is already in use.",
"errors": field_errors,
}), 422
# Unknown DB error
return jsonify({
"message": "An unexpected error occurred while saving the slot.",
"errors": {"__all__": [msg]},
}), 422
# Success → re-render the slots table
slots = await svc_list_slots(g.s, g.calendar.id)
from sexp.sexp_components import render_slots_table
return sexp_response(render_slots_table(slots, g.calendar))
@bp.get("/add")
@require_admin
async def add_form(**kwargs):
html = await render_template(
"_types/slots/_add.html",
)
return await make_response(html)
@bp.get("/add-button")
@require_admin
async def add_button(**kwargs):
html = await render_template(
"_types/slots/_add_button.html",
)
return await make_response(html)
return bp