Decouple blog UI via widget registry
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 54s

Replace explicit calendar/market service calls in post routes, auth
routes, and listing cards with widget-driven iteration. Zero cross-domain
imports remain in blog bp layer.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
giles
2026-02-19 18:04:26 +00:00
parent bb60835c58
commit 8af7c69090
3 changed files with 51 additions and 69 deletions

View File

@@ -27,6 +27,7 @@ from shared.models.ghost_membership_entities import GhostNewsletter
from shared.config import config from shared.config import config
from shared.utils import host_url from shared.utils import host_url
from shared.infrastructure.urls import coop_url from shared.infrastructure.urls import coop_url
from shared.services.widget_registry import widgets
from sqlalchemy.orm import selectinload from sqlalchemy.orm import selectinload
from shared.browser.app.redis_cacher import clear_cache from shared.browser.app.redis_cacher import clear_cache
@@ -68,6 +69,7 @@ def register(url_prefix="/auth"):
def context(): def context():
return { return {
"oob": oob, "oob": oob,
"account_nav_links": widgets.account_nav,
} }
# NOTE: load_current_user moved to shared/user_loader.py # NOTE: load_current_user moved to shared/user_loader.py
@@ -150,56 +152,32 @@ def register(url_prefix="/auth"):
return await make_response(html) return await make_response(html)
@auth_bp.get("/tickets/") @auth_bp.get("/<slug>/")
async def tickets(): async def widget_page(slug):
from shared.browser.app.utils.htmx import is_htmx_request from shared.browser.app.utils.htmx import is_htmx_request
from shared.services.registry import services
widget = widgets.account_page_by_slug(slug)
if not widget:
from quart import abort
abort(404)
if not g.get("user"): if not g.get("user"):
return redirect(host_url(url_for("auth.login_form"))) return redirect(host_url(url_for("auth.login_form")))
user_tickets = await services.calendar.user_tickets(g.s, user_id=g.user.id) ctx = await widget.context_fn(g.s, user_id=g.user.id)
w_oob = {**oob, "main": widget.template}
tk_oob = {**oob, "main": "_types/auth/_tickets_panel.html"}
if not is_htmx_request(): if not is_htmx_request():
html = await render_template( html = await render_template(
"_types/auth/index.html", "_types/auth/index.html",
oob=tk_oob, oob=w_oob,
tickets=user_tickets, **ctx,
) )
else: else:
html = await render_template( html = await render_template(
"_types/auth/_oob_elements.html", "_types/auth/_oob_elements.html",
oob=tk_oob, oob=w_oob,
tickets=user_tickets, **ctx,
)
return await make_response(html)
@auth_bp.get("/bookings/")
async def bookings():
from shared.browser.app.utils.htmx import is_htmx_request
from shared.services.registry import services
if not g.get("user"):
return redirect(host_url(url_for("auth.login_form")))
user_bookings = await services.calendar.user_bookings(g.s, user_id=g.user.id)
bk_oob = {**oob, "main": "_types/auth/_bookings_panel.html"}
if not is_htmx_request():
html = await render_template(
"_types/auth/index.html",
oob=bk_oob,
bookings=user_bookings,
)
else:
html = await render_template(
"_types/auth/_oob_elements.html",
oob=bk_oob,
bookings=user_bookings,
) )
return await make_response(html) return await make_response(html)

View File

@@ -1,7 +1,7 @@
from ..ghost_db import DBClient # adjust import path from ..ghost_db import DBClient # adjust import path
from sqlalchemy import select from sqlalchemy import select
from models.ghost_content import PostLike from models.ghost_content import PostLike
from shared.services.registry import services from shared.services.widget_registry import widgets
from quart import g from quart import g
async def posts_data( async def posts_data(
@@ -85,12 +85,11 @@ async def posts_data(
for post in posts: for post in posts:
post["is_liked"] = False post["is_liked"] = False
# Fetch associated entries for each post via calendar service # Widget-driven card decoration
entries_by_post = await services.calendar.confirmed_entries_for_posts(session, post_ids) for w in widgets.container_cards:
batch_data = await w.batch_fn(session, post_ids)
# Add associated_entries to each post for post in posts:
for post in posts: post[w.context_key] = batch_data.get(post["id"], [])
post["associated_entries"] = entries_by_post.get(post["id"], [])
tags=await client.list_tags( tags=await client.list_tags(
limit=50000 limit=50000

View File

@@ -8,10 +8,12 @@ from quart import (
Blueprint, Blueprint,
abort, abort,
url_for, url_for,
request,
) )
from .services.post_data import post_data from .services.post_data import post_data
from .services.post_operations import toggle_post_like from .services.post_operations import toggle_post_like
from shared.services.registry import services from shared.services.registry import services
from shared.services.widget_registry import widgets
from shared.browser.app.redis_cacher import cache_page, clear_cache from shared.browser.app.redis_cacher import cache_page, clear_cache
@@ -62,25 +64,29 @@ def register():
async def context(): async def context():
p_data = getattr(g, "post_data", None) p_data = getattr(g, "post_data", None)
if p_data: if p_data:
from .services.entry_associations import get_associated_entries
from shared.infrastructure.cart_identity import current_cart_identity from shared.infrastructure.cart_identity import current_cart_identity
db_post_id = (g.post_data.get("post") or {}).get("id") # <-- integer db_post_id = (g.post_data.get("post") or {}).get("id")
calendars = await services.calendar.calendars_for_container(g.s, "page", db_post_id) post_slug = (g.post_data.get("post") or {}).get("slug", "")
markets = await services.market.marketplaces_for_container(g.s, "page", db_post_id)
# Fetch associated entries for nav display # Widget-driven container nav — only include widgets with data
associated_entries = await get_associated_entries(g.s, db_post_id) container_nav_loaded = []
for w in widgets.container_nav:
wctx = await w.context_fn(
g.s, container_type="page", container_id=db_post_id,
post_slug=post_slug,
)
# Include widget if it has any list data
if any(v for v in wctx.values() if isinstance(v, list) and v):
container_nav_loaded.append({"widget": w, "ctx": wctx})
ctx = { ctx = {
**p_data, **p_data,
"base_title": f"{config()['title']} {p_data['post']['title']}", "base_title": f"{config()['title']} {p_data['post']['title']}",
"calendars": calendars, "container_nav_widgets": container_nav_loaded,
"markets": markets,
"associated_entries": associated_entries,
} }
# Page cart badge via service (replaces cross-app HTTP API) # Page cart badge via service
post_dict = p_data.get("post") or {} post_dict = p_data.get("post") or {}
if post_dict.get("is_page"): if post_dict.get("is_page"):
ident = current_cart_identity() ident = current_cart_identity()
@@ -143,24 +149,23 @@ def register():
) )
return html return html
@bp.get("/entries/") @bp.get("/w/<widget_domain>/")
async def get_entries(slug: str): async def widget_paginate(slug: str, widget_domain: str):
"""Get paginated associated entries for infinite scroll in nav""" """Generic paginated widget endpoint for infinite scroll."""
from .services.entry_associations import get_associated_entries
from quart import request
page = int(request.args.get("page", 1)) page = int(request.args.get("page", 1))
post_id = g.post_data["post"]["id"] post_id = g.post_data["post"]["id"]
result = await get_associated_entries(g.s, post_id, page=page, per_page=10) for w in widgets.container_nav:
if w.domain == widget_domain:
html = await render_template( ctx = await w.context_fn(
"_types/post/_entry_items.html", g.s, container_type="page", container_id=post_id,
entries=result["entries"], post_slug=slug, page=page,
page=result["page"], )
has_more=result["has_more"], html = await render_template(
) w.template, ctx=ctx, post=g.post_data["post"],
return await make_response(html) )
return await make_response(html)
abort(404)
return bp return bp