Fix cart template: use direct CartItem queries in market context
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 47s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 47s
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 <noreply@anthropic.com>
This commit is contained in:
35
app.py
35
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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user