feat: initialize events app with calendars, slots, tickets, and internal API
Some checks failed
Build and Deploy / build-and-deploy (push) Has been cancelled
Some checks failed
Build and Deploy / build-and-deploy (push) Has been cancelled
Extract events/calendar functionality into standalone microservice: - app.py and events_api.py from apps/events/ - Calendar blueprints (calendars, calendar, calendar_entries, calendar_entry, day, slots, slot, ticket_types, ticket_type) - Templates for all calendar/event views including admin - Dockerfile (APP_MODULE=app:app, IMAGE=events) - entrypoint.sh (no Alembic - migrations managed by blog app) - Gitea CI workflow for build and deploy Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
152
bp/slots/routes.py
Normal file
152
bp/slots/routes.py
Normal file
@@ -0,0 +1,152 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from quart import (
|
||||
request, render_template, make_response, Blueprint, g, jsonify
|
||||
)
|
||||
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from suma_browser.app.authz import require_admin
|
||||
from suma_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 suma_browser.app.utils import (
|
||||
parse_time,
|
||||
parse_cost
|
||||
)
|
||||
from suma_browser.app.utils.htmx import is_htmx_request
|
||||
|
||||
|
||||
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):
|
||||
if not is_htmx_request():
|
||||
# Normal browser request: full page with layout
|
||||
html = await render_template(
|
||||
"_types/slots/index.html",
|
||||
)
|
||||
else:
|
||||
|
||||
html = await render_template(
|
||||
"_types/slots/_oob_elements.html",
|
||||
)
|
||||
return await make_response(html)
|
||||
|
||||
|
||||
@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
|
||||
html = await render_template("_types/slots/_main_panel.html")
|
||||
return await make_response(html)
|
||||
|
||||
|
||||
@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
|
||||
64
bp/slots/services/slots.py
Normal file
64
bp/slots/services/slots.py
Normal file
@@ -0,0 +1,64 @@
|
||||
|
||||
from __future__ import annotations
|
||||
from datetime import time
|
||||
from typing import Sequence
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from models.calendars import CalendarSlot
|
||||
|
||||
class SlotError(ValueError):
|
||||
pass
|
||||
|
||||
def _b(v):
|
||||
if isinstance(v, bool):
|
||||
return v
|
||||
s = str(v).lower()
|
||||
return s in {"1","true","t","yes","y","on"}
|
||||
|
||||
async def list_slots(sess: AsyncSession, calendar_id: int) -> Sequence[CalendarSlot]:
|
||||
res = await sess.execute(
|
||||
select(CalendarSlot)
|
||||
.where(CalendarSlot.calendar_id == calendar_id, CalendarSlot.deleted_at.is_(None))
|
||||
.order_by(CalendarSlot.time_start.asc(), CalendarSlot.id.asc())
|
||||
)
|
||||
return res.scalars().all()
|
||||
|
||||
async def create_slot(
|
||||
sess: AsyncSession,
|
||||
calendar_id: int,
|
||||
*,
|
||||
name: str,
|
||||
description: str | None,
|
||||
days: dict,
|
||||
time_start: time,
|
||||
time_end: time,
|
||||
cost: float | None,
|
||||
flexible: bool = False, # NEW
|
||||
):
|
||||
if not name:
|
||||
raise SlotError("name is required")
|
||||
|
||||
if not time_start or not time_end or time_end <= time_start:
|
||||
raise SlotError("time range invalid")
|
||||
|
||||
slot = CalendarSlot(
|
||||
calendar_id=calendar_id,
|
||||
name=name,
|
||||
description=(description or None),
|
||||
mon=_b(days.get("mon")),
|
||||
tue=_b(days.get("tue")),
|
||||
wed=_b(days.get("wed")),
|
||||
thu=_b(days.get("thu")),
|
||||
fri=_b(days.get("fri")),
|
||||
sat=_b(days.get("sat")),
|
||||
sun=_b(days.get("sun")),
|
||||
time_start=time_start,
|
||||
time_end=time_end,
|
||||
cost=cost,
|
||||
flexible=flexible, # NEW
|
||||
)
|
||||
sess.add(slot)
|
||||
await sess.flush()
|
||||
return slot
|
||||
Reference in New Issue
Block a user