From fe255fc53c4c260705bcaf1f3d31ae38fde2a866 Mon Sep 17 00:00:00 2001 From: giles Date: Thu, 19 Feb 2026 04:50:06 +0000 Subject: [PATCH] Fix cart template: use direct CartItem queries in market context MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Market owns CartItem/Product — query directly with selectinload so templates can access item.product.slug and other ORM attributes. The cart service DTOs are for cross-domain consumers (blog, events). Co-Authored-By: Claude Opus 4.6 --- app.py | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/app.py b/app.py index 8d95cb3..dd6fd0c 100644 --- a/app.py +++ b/app.py @@ -17,26 +17,43 @@ async def market_context() -> dict: """ Market app context processor. - - menu_items: direct DB query via glue layer - - cart_count/cart_total: via cart service (shared DB) + - menu_items: direct DB query + - cart/cart_count/cart_total: direct DB (market owns CartItem/Product) """ from shared.infrastructure.context import base_context from shared.services.navigation import get_navigation_tree from shared.infrastructure.cart_identity import current_cart_identity - from shared.services.registry import services + from shared.models.market import CartItem + from sqlalchemy.orm import selectinload ctx = await base_context() ctx["menu_items"] = await get_navigation_tree(g.s) - # Cart data via service (replaces direct CartItem query) + # Market owns CartItem/Product — query directly for template compat ident = current_cart_identity() - summary = await services.cart.cart_summary( - g.s, user_id=ident["user_id"], session_id=ident["session_id"], + filters = [CartItem.deleted_at.is_(None)] + if ident["user_id"]: + filters.append(CartItem.user_id == ident["user_id"]) + elif ident["session_id"]: + filters.append(CartItem.session_id == ident["session_id"]) + else: + ctx["cart"] = [] + ctx["cart_count"] = 0 + ctx["cart_total"] = 0 + return ctx + + result = await g.s.execute( + select(CartItem).where(*filters).options(selectinload(CartItem.product)) ) - ctx["cart"] = summary.items - ctx["cart_count"] = summary.count - ctx["cart_total"] = float(summary.total) + cart_items = list(result.scalars().all()) + + ctx["cart"] = cart_items + ctx["cart_count"] = sum(ci.quantity for ci in cart_items) + ctx["cart_total"] = float(sum( + (ci.product.special_price or ci.product.regular_price or 0) * ci.quantity + for ci in cart_items if ci.product + )) return ctx