Fix cross-DB query: market app cart_items via data endpoint
market_context() was querying CartItem directly via g.s (db_market),
but cart_items lives in db_cart. Replace with fetch_data("cart",
"cart-items") and add the corresponding data endpoint.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -80,4 +80,36 @@ def register() -> Blueprint:
|
|||||||
|
|
||||||
_handlers["page-config-ensure"] = _page_config_ensure
|
_handlers["page-config-ensure"] = _page_config_ensure
|
||||||
|
|
||||||
|
# --- cart-items (product slugs + quantities for template rendering) ---
|
||||||
|
async def _cart_items():
|
||||||
|
from sqlalchemy import select
|
||||||
|
from sqlalchemy.orm import selectinload
|
||||||
|
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).options(selectinload(CartItem.product))
|
||||||
|
)
|
||||||
|
items = result.scalars().all()
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
"product_id": item.product_id,
|
||||||
|
"product_slug": item.product.slug if item.product else None,
|
||||||
|
"quantity": item.quantity,
|
||||||
|
}
|
||||||
|
for item in items
|
||||||
|
]
|
||||||
|
|
||||||
|
_handlers["cart-items"] = _cart_items
|
||||||
|
|
||||||
return bp
|
return bp
|
||||||
|
|||||||
@@ -26,8 +26,6 @@ async def market_context() -> dict:
|
|||||||
from shared.infrastructure.fragments import fetch_fragments
|
from shared.infrastructure.fragments import fetch_fragments
|
||||||
from shared.infrastructure.data_client import fetch_data
|
from shared.infrastructure.data_client import fetch_data
|
||||||
from shared.contracts.dtos import CartSummaryDTO, dto_from_dict
|
from shared.contracts.dtos import CartSummaryDTO, dto_from_dict
|
||||||
from shared.models.market import CartItem
|
|
||||||
from sqlalchemy.orm import selectinload
|
|
||||||
|
|
||||||
ctx = await base_context()
|
ctx = await base_context()
|
||||||
|
|
||||||
@@ -64,20 +62,21 @@ async def market_context() -> dict:
|
|||||||
ctx["auth_menu_html"] = auth_menu_html
|
ctx["auth_menu_html"] = auth_menu_html
|
||||||
ctx["nav_tree_html"] = nav_tree_html
|
ctx["nav_tree_html"] = nav_tree_html
|
||||||
|
|
||||||
# ORM cart items for product templates (need .product relationship)
|
# Cart items for product templates — fetched via internal data endpoint
|
||||||
filters = [CartItem.deleted_at.is_(None)]
|
# (cart_items table lives in db_cart, not db_market)
|
||||||
if ident["user_id"] is not None:
|
cart_items_raw = await fetch_data("cart", "cart-items", params=summary_params, required=False)
|
||||||
filters.append(CartItem.user_id == ident["user_id"])
|
if cart_items_raw:
|
||||||
elif ident["session_id"] is not None:
|
# Wrap as namespace objects so Jinja selectattr("product.slug", ...) works
|
||||||
filters.append(CartItem.session_id == ident["session_id"])
|
from types import SimpleNamespace
|
||||||
|
ctx["cart"] = [
|
||||||
|
SimpleNamespace(
|
||||||
|
product=SimpleNamespace(slug=item["product_slug"]),
|
||||||
|
quantity=item["quantity"],
|
||||||
|
)
|
||||||
|
for item in cart_items_raw
|
||||||
|
]
|
||||||
else:
|
else:
|
||||||
ctx["cart"] = []
|
ctx["cart"] = []
|
||||||
return ctx
|
|
||||||
|
|
||||||
result = await g.s.execute(
|
|
||||||
select(CartItem).where(*filters).options(selectinload(CartItem.product))
|
|
||||||
)
|
|
||||||
ctx["cart"] = list(result.scalars().all())
|
|
||||||
|
|
||||||
return ctx
|
return ctx
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user