Implement flexible entity relation system (Phases A–E)
Declarative relation registry via defrelation s-expressions with cardinality enforcement (one-to-one, one-to-many, many-to-many), registry-aware relate/unrelate/can-relate API endpoints, generic container-nav fragment, and relation-driven UI components. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
0
relations/bp/fragments/__init__.py
Normal file
0
relations/bp/fragments/__init__.py
Normal file
88
relations/bp/fragments/routes.py
Normal file
88
relations/bp/fragments/routes.py
Normal file
@@ -0,0 +1,88 @@
|
||||
"""Relations app fragment endpoints.
|
||||
|
||||
Generic container-nav fragment that renders navigation items for all
|
||||
related entities, driven by the relation registry.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from quart import Blueprint, Response, g, request
|
||||
|
||||
from shared.infrastructure.fragments import FRAGMENT_HEADER
|
||||
|
||||
|
||||
def register():
|
||||
bp = Blueprint("fragments", __name__, url_prefix="/internal/fragments")
|
||||
|
||||
_handlers: dict[str, object] = {}
|
||||
|
||||
@bp.before_request
|
||||
async def _require_fragment_header():
|
||||
if not request.headers.get(FRAGMENT_HEADER):
|
||||
return Response("", status=403)
|
||||
|
||||
@bp.get("/<fragment_type>")
|
||||
async def get_fragment(fragment_type: str):
|
||||
handler = _handlers.get(fragment_type)
|
||||
if handler is None:
|
||||
return Response("", status=200, content_type="text/html")
|
||||
html = await handler()
|
||||
return Response(html, status=200, content_type="text/html")
|
||||
|
||||
# --- generic container-nav fragment ----------------------------------------
|
||||
|
||||
async def _container_nav_handler():
|
||||
"""Render nav items for all visible relations of a container entity.
|
||||
|
||||
Query params:
|
||||
container_type: entity type (e.g. "page")
|
||||
container_id: entity id
|
||||
post_slug: used for URL construction
|
||||
exclude: comma-separated relation types to skip
|
||||
"""
|
||||
from shared.sexp.jinja_bridge import sexp as render_sexp
|
||||
from shared.sexp.relations import relations_from
|
||||
from shared.services.relationships import get_children
|
||||
|
||||
container_type = request.args.get("container_type", "page")
|
||||
container_id = int(request.args.get("container_id", 0))
|
||||
post_slug = request.args.get("post_slug", "")
|
||||
exclude_raw = request.args.get("exclude", "")
|
||||
exclude = set(exclude_raw.split(",")) if exclude_raw else set()
|
||||
|
||||
nav_defs = [
|
||||
d for d in relations_from(container_type)
|
||||
if d.nav != "hidden" and d.name not in exclude
|
||||
]
|
||||
|
||||
if not nav_defs:
|
||||
return ""
|
||||
|
||||
parts = []
|
||||
for defn in nav_defs:
|
||||
children = await get_children(
|
||||
g.s,
|
||||
parent_type=container_type,
|
||||
parent_id=container_id,
|
||||
child_type=defn.to_type,
|
||||
relation_type=defn.name,
|
||||
)
|
||||
for child in children:
|
||||
slug = (child.metadata_ or {}).get("slug", "")
|
||||
href = f"/{post_slug}/{slug}/" if post_slug else f"/{slug}/"
|
||||
parts.append(render_sexp(
|
||||
'(~relation-nav :href href :name name :icon icon :nav-class nav-class :relation-type relation-type)',
|
||||
href=href,
|
||||
name=child.label or "",
|
||||
icon=defn.nav_icon or "",
|
||||
**{
|
||||
"nav-class": "",
|
||||
"relation-type": defn.name,
|
||||
},
|
||||
))
|
||||
|
||||
return "\n".join(parts)
|
||||
|
||||
_handlers["container-nav"] = _container_nav_handler
|
||||
|
||||
return bp
|
||||
Reference in New Issue
Block a user