All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 50s
Replace direct Calendar, MarketPlace, and Post model queries with typed service calls (services.blog, services.calendar, services.market, services.cart). Blog registers all 4 services via domain_services_fn with has() guards for composable deployment. Key changes: - app.py: use domain_services_fn instead of inline service registration - admin routes: MarketPlace queries → services.market.marketplaces_for_container() - entry_associations: CalendarEntryPost → services.calendar.entry_ids_for_content() - markets service: Post query → services.blog.get_post_by_id/slug() - posts_data, post routes: use calendar/market/cart services - menu_items: glue imports → shared imports Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
121 lines
3.4 KiB
Python
121 lines
3.4 KiB
Python
from __future__ import annotations
|
|
import path_setup # noqa: F401 # adds shared_lib to sys.path
|
|
from pathlib import Path
|
|
|
|
from quart import g, request
|
|
from jinja2 import FileSystemLoader, ChoiceLoader
|
|
from sqlalchemy import select
|
|
|
|
from shared.infrastructure.factory import create_base_app
|
|
from shared.config import config
|
|
from shared.models import KV
|
|
|
|
from bp import (
|
|
register_auth_bp,
|
|
register_blog_bp,
|
|
register_admin,
|
|
register_menu_items,
|
|
register_snippets,
|
|
register_coop_api,
|
|
)
|
|
|
|
|
|
async def coop_context() -> dict:
|
|
"""
|
|
Coop app context processor.
|
|
|
|
- menu_items: direct DB query via glue layer
|
|
- cart_count/cart_total: via cart service (shared DB)
|
|
"""
|
|
from shared.infrastructure.context import base_context
|
|
from shared.services.navigation import get_navigation_tree
|
|
from shared.services.registry import services
|
|
from shared.infrastructure.cart_identity import current_cart_identity
|
|
|
|
ctx = await base_context()
|
|
|
|
ctx["menu_items"] = await get_navigation_tree(g.s)
|
|
|
|
# Cart data via service (replaces cross-app HTTP API)
|
|
ident = current_cart_identity()
|
|
summary = await services.cart.cart_summary(
|
|
g.s, user_id=ident["user_id"], session_id=ident["session_id"],
|
|
)
|
|
ctx["cart_count"] = summary.count + summary.calendar_count
|
|
ctx["cart_total"] = float(summary.total + summary.calendar_total)
|
|
|
|
return ctx
|
|
|
|
|
|
def create_app() -> "Quart":
|
|
from services import register_domain_services
|
|
|
|
app = create_base_app(
|
|
"coop",
|
|
context_fn=coop_context,
|
|
domain_services_fn=register_domain_services,
|
|
)
|
|
|
|
# App-specific templates override shared templates
|
|
app_templates = str(Path(__file__).resolve().parent / "templates")
|
|
app.jinja_loader = ChoiceLoader([
|
|
FileSystemLoader(app_templates),
|
|
app.jinja_loader,
|
|
])
|
|
|
|
# --- blueprints ---
|
|
app.register_blueprint(register_auth_bp())
|
|
|
|
app.register_blueprint(
|
|
register_blog_bp(
|
|
url_prefix=config()["blog_root"],
|
|
title=config()["blog_title"],
|
|
),
|
|
url_prefix=config()["blog_root"],
|
|
)
|
|
|
|
app.register_blueprint(register_admin("/settings"))
|
|
app.register_blueprint(register_menu_items())
|
|
app.register_blueprint(register_snippets())
|
|
|
|
# Internal API (server-to-server, CSRF-exempt)
|
|
app.register_blueprint(register_coop_api())
|
|
|
|
# --- KV admin endpoints ---
|
|
@app.get("/settings/kv/<key>")
|
|
async def kv_get(key: str):
|
|
row = (
|
|
await g.s.execute(select(KV).where(KV.key == key))
|
|
).scalar_one_or_none()
|
|
return {"key": key, "value": (row.value if row else None)}
|
|
|
|
@app.post("/settings/kv/<key>")
|
|
async def kv_set(key: str):
|
|
data = await request.get_json() or {}
|
|
val = data.get("value", "")
|
|
obj = await g.s.get(KV, key)
|
|
if obj is None:
|
|
obj = KV(key=key, value=val)
|
|
g.s.add(obj)
|
|
else:
|
|
obj.value = val
|
|
return {"ok": True, "key": key, "value": val}
|
|
|
|
# --- debug: url rules ---
|
|
@app.get("/__rules")
|
|
async def dump_rules():
|
|
rules = []
|
|
for r in app.url_map.iter_rules():
|
|
rules.append({
|
|
"endpoint": r.endpoint,
|
|
"rule": repr(r.rule),
|
|
"methods": sorted(r.methods - {"HEAD", "OPTIONS"}),
|
|
"strict_slashes": r.strict_slashes,
|
|
})
|
|
return {"rules": rules}
|
|
|
|
return app
|
|
|
|
|
|
app = create_app()
|