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:
98
bp/calendars/routes.py
Normal file
98
bp/calendars/routes.py
Normal file
@@ -0,0 +1,98 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from quart import (
|
||||
request, render_template, make_response, Blueprint, g
|
||||
)
|
||||
from sqlalchemy import select
|
||||
|
||||
from models.calendars import Calendar
|
||||
|
||||
from .services.calendars import (
|
||||
create_calendar as svc_create_calendar,
|
||||
)
|
||||
|
||||
from ..calendar.routes import register as register_calendar
|
||||
|
||||
from suma_browser.app.redis_cacher import cache_page, clear_cache
|
||||
|
||||
from suma_browser.app.authz import require_admin
|
||||
from suma_browser.app.utils.htmx import is_htmx_request
|
||||
|
||||
|
||||
def register():
|
||||
bp = Blueprint("calendars", __name__, url_prefix='/calendars')
|
||||
bp.register_blueprint(
|
||||
register_calendar(),
|
||||
)
|
||||
@bp.context_processor
|
||||
async def inject_root():
|
||||
# Must always return a dict
|
||||
return {}
|
||||
|
||||
# ---------- Pages ----------
|
||||
|
||||
@bp.get("/")
|
||||
@cache_page(tag="calendars")
|
||||
async def home(**kwargs):
|
||||
if not is_htmx_request():
|
||||
html = await render_template(
|
||||
"_types/calendars/index.html",
|
||||
)
|
||||
else:
|
||||
html = await render_template(
|
||||
"_types/calendars/_oob_elements.html",
|
||||
)
|
||||
return await make_response(html)
|
||||
|
||||
|
||||
@bp.post("/new/")
|
||||
@require_admin
|
||||
@clear_cache(tag="calendars", tag_scope="all")
|
||||
async def create_calendar(**kwargs):
|
||||
form = await request.form
|
||||
name = (form.get("name") or "").strip()
|
||||
|
||||
# Get post_id from context if available (blog-embedded mode)
|
||||
post_data = getattr(g, "post_data", None)
|
||||
post_id = (post_data.get("post") or {}).get("id") if post_data else None
|
||||
|
||||
if not post_id:
|
||||
# Standalone mode: post_id from form (or None — calendar without post)
|
||||
post_id = form.get("post_id")
|
||||
if post_id:
|
||||
post_id = int(post_id)
|
||||
|
||||
try:
|
||||
await svc_create_calendar(g.s, post_id, name)
|
||||
except Exception as e:
|
||||
return await make_response(f'<div class="text-red-600 text-sm">{e}</div>', 422)
|
||||
|
||||
html = await render_template(
|
||||
"_types/calendars/index.html",
|
||||
)
|
||||
|
||||
# Blog-embedded mode: also update post nav
|
||||
if post_data:
|
||||
from ..post.services.entry_associations import get_associated_entries
|
||||
|
||||
cals = (
|
||||
await g.s.execute(
|
||||
select(Calendar)
|
||||
.where(Calendar.post_id == post_id, Calendar.deleted_at.is_(None))
|
||||
.order_by(Calendar.name.asc())
|
||||
)
|
||||
).scalars().all()
|
||||
|
||||
associated_entries = await get_associated_entries(g.s, post_id)
|
||||
|
||||
nav_oob = await render_template(
|
||||
"_types/post/admin/_nav_entries_oob.html",
|
||||
associated_entries=associated_entries,
|
||||
calendars=cals,
|
||||
post=post_data["post"],
|
||||
)
|
||||
|
||||
html = html + nav_oob
|
||||
|
||||
return await make_response(html)
|
||||
return bp
|
||||
104
bp/calendars/services/calendars.py
Normal file
104
bp/calendars/services/calendars.py
Normal file
@@ -0,0 +1,104 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from models.calendars import Calendar
|
||||
from models.ghost_content import Post # for FK existence checks
|
||||
import unicodedata
|
||||
import re
|
||||
|
||||
|
||||
class CalendarError(ValueError):
|
||||
"""Base error for calendar service operations."""
|
||||
|
||||
from suma_browser.app.utils import (
|
||||
utcnow
|
||||
)
|
||||
|
||||
def slugify(value: str, max_len: int = 255) -> str:
|
||||
"""
|
||||
Make a URL-friendly slug:
|
||||
- lowercase
|
||||
- remove accents
|
||||
- replace any non [a-z0-9]+ with '-'
|
||||
- no forward slashes
|
||||
- collapse multiple dashes
|
||||
- trim leading/trailing dashes
|
||||
"""
|
||||
if value is None:
|
||||
value = ""
|
||||
# normalize accents -> ASCII
|
||||
value = unicodedata.normalize("NFKD", value)
|
||||
value = value.encode("ascii", "ignore").decode("ascii")
|
||||
value = value.lower()
|
||||
|
||||
# explicitly block forward slashes
|
||||
value = value.replace("/", "-")
|
||||
|
||||
# replace non-alnum with hyphen
|
||||
value = re.sub(r"[^a-z0-9]+", "-", value)
|
||||
# collapse multiple hyphens
|
||||
value = re.sub(r"-{2,}", "-", value)
|
||||
# trim hyphens and enforce length
|
||||
value = value.strip("-")[:max_len].strip("-")
|
||||
|
||||
# fallback if empty
|
||||
return value or "calendar"
|
||||
|
||||
|
||||
async def soft_delete(sess: AsyncSession, post_slug: str, calendar_slug: str) -> bool:
|
||||
cal = (
|
||||
await sess.execute(
|
||||
select(Calendar)
|
||||
.join(Post, Calendar.post_id == Post.id)
|
||||
.where(
|
||||
Post.slug == post_slug,
|
||||
Calendar.slug == calendar_slug,
|
||||
Calendar.deleted_at.is_(None),
|
||||
)
|
||||
)
|
||||
).scalar_one_or_none()
|
||||
|
||||
if not cal:
|
||||
return False
|
||||
|
||||
cal.deleted_at = utcnow()
|
||||
await sess.flush()
|
||||
return True
|
||||
|
||||
async def create_calendar(sess: AsyncSession, post_id: int, name: str) -> Calendar:
|
||||
"""
|
||||
Create a calendar for a post. Name must be unique per post.
|
||||
If a calendar with the same (post_id, name) exists but is soft-deleted,
|
||||
it will be revived (deleted_at=None).
|
||||
"""
|
||||
name = (name or "").strip()
|
||||
if not name:
|
||||
raise CalendarError("Calendar name must not be empty.")
|
||||
slug=slugify(name)
|
||||
|
||||
# Ensure post exists (avoid silent FK errors in some DBs)
|
||||
post = (await sess.execute(select(Post.id).where(Post.id == post_id))).scalar_one_or_none()
|
||||
if not post:
|
||||
raise CalendarError(f"Post {post_id} does not exist.")
|
||||
|
||||
# Look for existing (including soft-deleted)
|
||||
q = await sess.execute(
|
||||
select(Calendar).where(Calendar.post_id == post_id, Calendar.name == name)
|
||||
)
|
||||
existing = q.scalar_one_or_none()
|
||||
|
||||
if existing:
|
||||
if existing.deleted_at is not None:
|
||||
existing.deleted_at = None # revive
|
||||
await sess.flush()
|
||||
return existing
|
||||
raise CalendarError(f'Calendar with slug "{slug}" already exists for post {post_id}.')
|
||||
|
||||
cal = Calendar(post_id=post_id, name=name, slug=slug)
|
||||
sess.add(cal)
|
||||
await sess.flush()
|
||||
return cal
|
||||
|
||||
|
||||
Reference in New Issue
Block a user