Replace every direct cross-app services.* call with HTTP-based communication: call_action() for writes, fetch_data() for reads. Each app now registers only its own domain service. Infrastructure: - shared/infrastructure/actions.py — POST client for /internal/actions/ - shared/infrastructure/data_client.py — GET client for /internal/data/ - shared/contracts/dtos.py — dto_to_dict/dto_from_dict serialization Action endpoints (writes): - events: 8 handlers (ticket adjust, claim/confirm, toggle, adopt) - market: 2 handlers (create/soft-delete marketplace) - cart: 1 handler (adopt cart for user) Data endpoints (reads): - blog: 4 (post-by-slug/id, posts-by-ids, search-posts) - events: 10 (pending entries/tickets, entries/tickets for page/order, entry-ids, associated-entries, calendars, visible-entries-for-period) - market: 1 (marketplaces-for-container) - cart: 1 (cart-summary) Service registration cleanup: - blog→blog+federation, events→calendar+federation, market→market+federation, cart→cart only, federation→federation only, account→nothing - Stubs reduced to minimal StubFederationService Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
119 lines
3.8 KiB
Python
119 lines
3.8 KiB
Python
from __future__ import annotations
|
|
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from models.calendars import Calendar
|
|
from shared.infrastructure.data_client import fetch_data
|
|
from shared.contracts.dtos import PostDTO, dto_from_dict
|
|
from shared.services.relationships import attach_child, detach_child
|
|
import unicodedata
|
|
import re
|
|
|
|
|
|
class CalendarError(ValueError):
|
|
"""Base error for calendar service operations."""
|
|
|
|
from shared.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:
|
|
raw = await fetch_data("blog", "post-by-slug", params={"slug": post_slug}, required=False)
|
|
post = dto_from_dict(PostDTO, raw) if raw else None
|
|
if not post:
|
|
return False
|
|
|
|
cal = (
|
|
await sess.execute(
|
|
select(Calendar).where(
|
|
Calendar.container_type == "page",
|
|
Calendar.container_id == post.id,
|
|
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()
|
|
await detach_child(sess, "page", cal.container_id, "calendar", cal.id)
|
|
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)
|
|
raw = await fetch_data("blog", "post-by-id", params={"id": post_id}, required=False)
|
|
post = dto_from_dict(PostDTO, raw) if raw else None
|
|
if not post:
|
|
raise CalendarError(f"Post {post_id} does not exist.")
|
|
|
|
# Enforce: calendars can only be created on pages with the calendar feature
|
|
if not post.is_page:
|
|
raise CalendarError("Calendars can only be created on pages, not posts.")
|
|
|
|
# Look for existing (including soft-deleted)
|
|
q = await sess.execute(
|
|
select(Calendar).where(Calendar.container_type == "page", Calendar.container_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()
|
|
await attach_child(sess, "page", post_id, "calendar", existing.id)
|
|
return existing
|
|
raise CalendarError(f'Calendar with slug "{slug}" already exists for post {post_id}.')
|
|
|
|
cal = Calendar(container_type="page", container_id=post_id, name=name, slug=slug)
|
|
sess.add(cal)
|
|
await sess.flush()
|
|
await attach_child(sess, "page", post_id, "calendar", cal.id)
|
|
return cal
|
|
|
|
|