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:
@@ -26,8 +26,6 @@ async def market_context() -> dict:
|
||||
from shared.infrastructure.fragments import fetch_fragments
|
||||
from shared.infrastructure.data_client import fetch_data
|
||||
from shared.contracts.dtos import CartSummaryDTO, dto_from_dict
|
||||
from shared.models.market import CartItem
|
||||
from sqlalchemy.orm import selectinload
|
||||
|
||||
ctx = await base_context()
|
||||
|
||||
@@ -64,20 +62,21 @@ async def market_context() -> dict:
|
||||
ctx["auth_menu_html"] = auth_menu_html
|
||||
ctx["nav_tree_html"] = nav_tree_html
|
||||
|
||||
# ORM cart items for product templates (need .product relationship)
|
||||
filters = [CartItem.deleted_at.is_(None)]
|
||||
if ident["user_id"] is not None:
|
||||
filters.append(CartItem.user_id == ident["user_id"])
|
||||
elif ident["session_id"] is not None:
|
||||
filters.append(CartItem.session_id == ident["session_id"])
|
||||
# Cart items for product templates — fetched via internal data endpoint
|
||||
# (cart_items table lives in db_cart, not db_market)
|
||||
cart_items_raw = await fetch_data("cart", "cart-items", params=summary_params, required=False)
|
||||
if cart_items_raw:
|
||||
# Wrap as namespace objects so Jinja selectattr("product.slug", ...) works
|
||||
from types import SimpleNamespace
|
||||
ctx["cart"] = [
|
||||
SimpleNamespace(
|
||||
product=SimpleNamespace(slug=item["product_slug"]),
|
||||
quantity=item["quantity"],
|
||||
)
|
||||
for item in cart_items_raw
|
||||
]
|
||||
else:
|
||||
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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user