Decouple PageConfig cross-domain queries + merge cart into db_market
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 3m11s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 3m11s
PageConfig (db_blog) decoupling: - Blog: add page-config, page-config-by-id, page-configs-batch data endpoints - Blog: add update-page-config action endpoint for events payment admin - Cart: hydrate_page, resolve_page_config, get_cart_grouped_by_page all fetch PageConfig from blog via HTTP instead of direct DB query - Cart: check_sumup_status auto-fetches page_config from blog when needed - Events: payment routes read/write PageConfig via blog HTTP endpoints - Order model: remove cross-domain page_config ORM relationship (keep column) Cart + Market DB merge: - Cart tables (cart_items, orders, order_items) moved into db_market - Cart app DATABASE_URL now points to db_market (same bounded context) - CartItem.product / CartItem.market_place relationships work again (same database, no cross-domain join issues) - Updated split-databases.sh, init-databases.sql, docker-compose.yml Ghost sync fix: - Wrap PostAuthor/PostTag delete+re-add in no_autoflush block - Use synchronize_session="fetch" to keep identity map consistent - Prevents query-invoked autoflush IntegrityError on composite PK Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -17,6 +17,7 @@ from bp import (
|
||||
register_snippets,
|
||||
register_fragments,
|
||||
register_data,
|
||||
register_actions,
|
||||
)
|
||||
|
||||
|
||||
@@ -105,6 +106,7 @@ def create_app() -> "Quart":
|
||||
app.register_blueprint(register_snippets())
|
||||
app.register_blueprint(register_fragments())
|
||||
app.register_blueprint(register_data())
|
||||
app.register_blueprint(register_actions())
|
||||
|
||||
# --- KV admin endpoints ---
|
||||
@app.get("/settings/kv/<key>")
|
||||
|
||||
@@ -4,3 +4,4 @@ from .menu_items.routes import register as register_menu_items
|
||||
from .snippets.routes import register as register_snippets
|
||||
from .fragments import register_fragments
|
||||
from .data import register_data
|
||||
from .actions.routes import register as register_actions
|
||||
|
||||
0
blog/bp/actions/__init__.py
Normal file
0
blog/bp/actions/__init__.py
Normal file
79
blog/bp/actions/routes.py
Normal file
79
blog/bp/actions/routes.py
Normal file
@@ -0,0 +1,79 @@
|
||||
"""Blog app action endpoints.
|
||||
|
||||
Exposes write operations at ``/internal/actions/<action_name>`` for
|
||||
cross-app callers via the internal action client.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from quart import Blueprint, g, jsonify, request
|
||||
|
||||
from shared.infrastructure.actions import ACTION_HEADER
|
||||
|
||||
|
||||
def register() -> Blueprint:
|
||||
bp = Blueprint("actions", __name__, url_prefix="/internal/actions")
|
||||
|
||||
@bp.before_request
|
||||
async def _require_action_header():
|
||||
if not request.headers.get(ACTION_HEADER):
|
||||
return jsonify({"error": "forbidden"}), 403
|
||||
|
||||
_handlers: dict[str, object] = {}
|
||||
|
||||
@bp.post("/<action_name>")
|
||||
async def handle_action(action_name: str):
|
||||
handler = _handlers.get(action_name)
|
||||
if handler is None:
|
||||
return jsonify({"error": "unknown action"}), 404
|
||||
result = await handler()
|
||||
return jsonify(result or {"ok": True})
|
||||
|
||||
# --- update-page-config ---
|
||||
async def _update_page_config():
|
||||
"""Create or update a PageConfig (used by events payment admin)."""
|
||||
from shared.models.page_config import PageConfig
|
||||
from sqlalchemy import select
|
||||
|
||||
data = await request.get_json(force=True)
|
||||
container_type = data.get("container_type", "page")
|
||||
container_id = data.get("container_id")
|
||||
if container_id is None:
|
||||
return {"error": "container_id required"}, 400
|
||||
|
||||
pc = (await g.s.execute(
|
||||
select(PageConfig).where(
|
||||
PageConfig.container_type == container_type,
|
||||
PageConfig.container_id == container_id,
|
||||
)
|
||||
)).scalar_one_or_none()
|
||||
|
||||
if pc is None:
|
||||
pc = PageConfig(
|
||||
container_type=container_type,
|
||||
container_id=container_id,
|
||||
features=data.get("features", {}),
|
||||
)
|
||||
g.s.add(pc)
|
||||
await g.s.flush()
|
||||
|
||||
if "sumup_merchant_code" in data:
|
||||
pc.sumup_merchant_code = data["sumup_merchant_code"] or None
|
||||
if "sumup_checkout_prefix" in data:
|
||||
pc.sumup_checkout_prefix = data["sumup_checkout_prefix"] or None
|
||||
if "sumup_api_key" in data:
|
||||
pc.sumup_api_key = data["sumup_api_key"] or None
|
||||
|
||||
await g.s.flush()
|
||||
|
||||
return {
|
||||
"id": pc.id,
|
||||
"container_type": pc.container_type,
|
||||
"container_id": pc.container_id,
|
||||
"sumup_merchant_code": pc.sumup_merchant_code,
|
||||
"sumup_checkout_prefix": pc.sumup_checkout_prefix,
|
||||
"sumup_configured": bool(pc.sumup_api_key),
|
||||
}
|
||||
|
||||
_handlers["update-page-config"] = _update_page_config
|
||||
|
||||
return bp
|
||||
@@ -185,25 +185,34 @@ async def _upsert_post(sess: AsyncSession, gp: Dict[str, Any], author_map: Dict[
|
||||
obj.user_id = user_id
|
||||
await sess.flush()
|
||||
|
||||
# rebuild post_authors (dedup to avoid composite PK conflicts from Ghost dupes)
|
||||
await sess.execute(delete(PostAuthor).where(PostAuthor.post_id == obj.id))
|
||||
await sess.flush()
|
||||
seen_authors: set[int] = set()
|
||||
for idx, a in enumerate(gp.get("authors") or []):
|
||||
aa = author_map[a["id"]]
|
||||
if aa.id not in seen_authors:
|
||||
seen_authors.add(aa.id)
|
||||
sess.add(PostAuthor(post_id=obj.id, author_id=aa.id, sort_order=idx))
|
||||
# Rebuild post_authors + post_tags inside no_autoflush to prevent
|
||||
# premature INSERT from query-invoked autoflush.
|
||||
async with sess.no_autoflush:
|
||||
# Delete + re-add post_authors (dedup for Ghost duplicate authors)
|
||||
await sess.execute(
|
||||
delete(PostAuthor).where(PostAuthor.post_id == obj.id),
|
||||
execution_options={"synchronize_session": "fetch"},
|
||||
)
|
||||
seen_authors: set[int] = set()
|
||||
for idx, a in enumerate(gp.get("authors") or []):
|
||||
aa = author_map[a["id"]]
|
||||
if aa.id not in seen_authors:
|
||||
seen_authors.add(aa.id)
|
||||
sess.add(PostAuthor(post_id=obj.id, author_id=aa.id, sort_order=idx))
|
||||
|
||||
# Delete + re-add post_tags (dedup similarly)
|
||||
await sess.execute(
|
||||
delete(PostTag).where(PostTag.post_id == obj.id),
|
||||
execution_options={"synchronize_session": "fetch"},
|
||||
)
|
||||
seen_tags: set[int] = set()
|
||||
for idx, t in enumerate(gp.get("tags") or []):
|
||||
tt = tag_map[t["id"]]
|
||||
if tt.id not in seen_tags:
|
||||
seen_tags.add(tt.id)
|
||||
sess.add(PostTag(post_id=obj.id, tag_id=tt.id, sort_order=idx))
|
||||
|
||||
# rebuild post_tags (dedup similarly)
|
||||
await sess.execute(delete(PostTag).where(PostTag.post_id == obj.id))
|
||||
await sess.flush()
|
||||
seen_tags: set[int] = set()
|
||||
for idx, t in enumerate(gp.get("tags") or []):
|
||||
tt = tag_map[t["id"]]
|
||||
if tt.id not in seen_tags:
|
||||
seen_tags.add(tt.id)
|
||||
sess.add(PostTag(post_id=obj.id, tag_id=tt.id, sort_order=idx))
|
||||
|
||||
# Auto-create PageConfig for pages
|
||||
if obj.is_page:
|
||||
|
||||
@@ -7,9 +7,25 @@ from __future__ import annotations
|
||||
|
||||
from quart import Blueprint, g, jsonify, request
|
||||
|
||||
from sqlalchemy import select
|
||||
|
||||
from shared.infrastructure.data_client import DATA_HEADER
|
||||
from shared.contracts.dtos import dto_to_dict
|
||||
from shared.services.registry import services
|
||||
from shared.models.page_config import PageConfig
|
||||
|
||||
|
||||
def _page_config_dict(pc: PageConfig) -> dict:
|
||||
"""Serialize PageConfig to a JSON-safe dict."""
|
||||
return {
|
||||
"id": pc.id,
|
||||
"container_type": pc.container_type,
|
||||
"container_id": pc.container_id,
|
||||
"features": pc.features or {},
|
||||
"sumup_merchant_code": pc.sumup_merchant_code,
|
||||
"sumup_api_key": pc.sumup_api_key,
|
||||
"sumup_checkout_prefix": pc.sumup_checkout_prefix,
|
||||
}
|
||||
|
||||
|
||||
def register() -> Blueprint:
|
||||
@@ -71,4 +87,56 @@ def register() -> Blueprint:
|
||||
|
||||
_handlers["search-posts"] = _search_posts
|
||||
|
||||
# --- page-config ---
|
||||
async def _page_config():
|
||||
"""Return a single PageConfig by container_type + container_id."""
|
||||
ct = request.args.get("container_type", "page")
|
||||
cid = request.args.get("container_id", type=int)
|
||||
if cid is None:
|
||||
return None
|
||||
pc = (await g.s.execute(
|
||||
select(PageConfig).where(
|
||||
PageConfig.container_type == ct,
|
||||
PageConfig.container_id == cid,
|
||||
)
|
||||
)).scalar_one_or_none()
|
||||
if not pc:
|
||||
return None
|
||||
return _page_config_dict(pc)
|
||||
|
||||
_handlers["page-config"] = _page_config
|
||||
|
||||
# --- page-config-by-id ---
|
||||
async def _page_config_by_id():
|
||||
"""Return a single PageConfig by its primary key."""
|
||||
pc_id = request.args.get("id", type=int)
|
||||
if pc_id is None:
|
||||
return None
|
||||
pc = await g.s.get(PageConfig, pc_id)
|
||||
if not pc:
|
||||
return None
|
||||
return _page_config_dict(pc)
|
||||
|
||||
_handlers["page-config-by-id"] = _page_config_by_id
|
||||
|
||||
# --- page-configs-batch ---
|
||||
async def _page_configs_batch():
|
||||
"""Return PageConfigs for multiple container_ids (comma-separated)."""
|
||||
ct = request.args.get("container_type", "page")
|
||||
ids_raw = request.args.get("ids", "")
|
||||
if not ids_raw:
|
||||
return []
|
||||
ids = [int(x.strip()) for x in ids_raw.split(",") if x.strip()]
|
||||
if not ids:
|
||||
return []
|
||||
result = await g.s.execute(
|
||||
select(PageConfig).where(
|
||||
PageConfig.container_type == ct,
|
||||
PageConfig.container_id.in_(ids),
|
||||
)
|
||||
)
|
||||
return [_page_config_dict(pc) for pc in result.scalars().all()]
|
||||
|
||||
_handlers["page-configs-batch"] = _page_configs_batch
|
||||
|
||||
return bp
|
||||
|
||||
Reference in New Issue
Block a user