container_relations is a generic parent/child graph used by blog (menu_nodes), market (marketplaces), and events (calendars). Move it to cart as shared infrastructure. All services now call cart actions (attach-child/detach-child) instead of querying the table directly. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
214 lines
6.9 KiB
Python
214 lines
6.9 KiB
Python
"""Cart app data endpoints.
|
|
|
|
Exposes read-only JSON queries at ``/internal/data/<query_name>`` for
|
|
cross-app callers via the internal data client.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from quart import Blueprint, g, jsonify, request
|
|
|
|
from shared.infrastructure.data_client import DATA_HEADER
|
|
from shared.contracts.dtos import dto_to_dict
|
|
from shared.services.registry import services
|
|
|
|
|
|
def register() -> Blueprint:
|
|
bp = Blueprint("data", __name__, url_prefix="/internal/data")
|
|
|
|
@bp.before_request
|
|
async def _require_data_header():
|
|
if not request.headers.get(DATA_HEADER):
|
|
return jsonify({"error": "forbidden"}), 403
|
|
from shared.infrastructure.internal_auth import validate_internal_request
|
|
if not validate_internal_request():
|
|
return jsonify({"error": "forbidden"}), 403
|
|
|
|
_handlers: dict[str, object] = {}
|
|
|
|
@bp.get("/<query_name>")
|
|
async def handle_query(query_name: str):
|
|
handler = _handlers.get(query_name)
|
|
if handler is None:
|
|
return jsonify({"error": "unknown query"}), 404
|
|
result = await handler()
|
|
return jsonify(result)
|
|
|
|
# --- cart-summary ---
|
|
async def _cart_summary():
|
|
user_id = request.args.get("user_id", type=int)
|
|
session_id = request.args.get("session_id")
|
|
page_slug = request.args.get("page_slug")
|
|
summary = await services.cart.cart_summary(
|
|
g.s, user_id=user_id, session_id=session_id, page_slug=page_slug,
|
|
)
|
|
return dto_to_dict(summary)
|
|
|
|
_handlers["cart-summary"] = _cart_summary
|
|
|
|
# --- page-config-ensure ---
|
|
async def _page_config_ensure():
|
|
"""Get or create a PageConfig for a container_type + container_id."""
|
|
from sqlalchemy import select
|
|
from shared.models.page_config import PageConfig
|
|
|
|
container_type = request.args.get("container_type", "page")
|
|
container_id = request.args.get("container_id", type=int)
|
|
if container_id is None:
|
|
return {"error": "container_id required"}, 400
|
|
|
|
row = (await g.s.execute(
|
|
select(PageConfig).where(
|
|
PageConfig.container_type == container_type,
|
|
PageConfig.container_id == container_id,
|
|
)
|
|
)).scalar_one_or_none()
|
|
|
|
if row is None:
|
|
row = PageConfig(
|
|
container_type=container_type,
|
|
container_id=container_id,
|
|
features={},
|
|
)
|
|
g.s.add(row)
|
|
await g.s.flush()
|
|
|
|
return {
|
|
"id": row.id,
|
|
"container_type": row.container_type,
|
|
"container_id": row.container_id,
|
|
}
|
|
|
|
_handlers["page-config-ensure"] = _page_config_ensure
|
|
|
|
# --- cart-items (product slugs + quantities for template rendering) ---
|
|
async def _cart_items():
|
|
from sqlalchemy import select
|
|
from shared.models.market import CartItem
|
|
|
|
user_id = request.args.get("user_id", type=int)
|
|
session_id = request.args.get("session_id")
|
|
|
|
filters = [CartItem.deleted_at.is_(None)]
|
|
if user_id is not None:
|
|
filters.append(CartItem.user_id == user_id)
|
|
elif session_id is not None:
|
|
filters.append(CartItem.session_id == session_id)
|
|
else:
|
|
return []
|
|
|
|
result = await g.s.execute(
|
|
select(CartItem).where(*filters)
|
|
)
|
|
items = result.scalars().all()
|
|
return [
|
|
{
|
|
"product_id": item.product_id,
|
|
"product_slug": item.product_slug,
|
|
"quantity": item.quantity,
|
|
}
|
|
for item in items
|
|
]
|
|
|
|
_handlers["cart-items"] = _cart_items
|
|
|
|
# --- page-config ---
|
|
async def _page_config():
|
|
"""Return a single PageConfig by container_type + container_id."""
|
|
from sqlalchemy import select
|
|
from shared.models.page_config import PageConfig
|
|
|
|
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."""
|
|
from shared.models.page_config import PageConfig
|
|
|
|
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)."""
|
|
from sqlalchemy import select
|
|
from shared.models.page_config import PageConfig
|
|
|
|
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
|
|
|
|
# --- get-children ---
|
|
async def _get_children():
|
|
"""Return ContainerRelation children for a parent."""
|
|
from shared.services.relationships import get_children
|
|
|
|
parent_type = request.args.get("parent_type", "")
|
|
parent_id = request.args.get("parent_id", type=int)
|
|
child_type = request.args.get("child_type")
|
|
if not parent_type or parent_id is None:
|
|
return []
|
|
rels = await get_children(g.s, parent_type, parent_id, child_type)
|
|
return [
|
|
{
|
|
"id": r.id,
|
|
"parent_type": r.parent_type,
|
|
"parent_id": r.parent_id,
|
|
"child_type": r.child_type,
|
|
"child_id": r.child_id,
|
|
"sort_order": r.sort_order,
|
|
"label": r.label,
|
|
}
|
|
for r in rels
|
|
]
|
|
|
|
_handlers["get-children"] = _get_children
|
|
|
|
return bp
|
|
|
|
|
|
def _page_config_dict(pc) -> 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,
|
|
}
|