Fix cart badge: include calendar entries + OOB update on add
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 50s

- Context processor now sums product + calendar counts from cart API
- add_entry route returns OOB cart-mini swap with correct count,
  querying pending entries from local session (sees uncommitted entry)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
giles
2026-02-18 21:49:45 +00:00
parent b153203fc0
commit d2195c0969
3 changed files with 32 additions and 7 deletions

6
app.py
View File

@@ -27,11 +27,11 @@ async def events_context() -> dict:
ctx["menu_items"] = await get_navigation_tree(g.s)
# Cart data from cart API
# Cart data from cart API (includes both product + calendar counts)
cart_data = await api_get("cart", "/internal/cart/summary", forward_session=True)
if cart_data:
ctx["cart_count"] = cart_data.get("count", 0)
ctx["cart_total"] = cart_data.get("total", 0)
ctx["cart_count"] = cart_data.get("count", 0) + cart_data.get("calendar_count", 0)
ctx["cart_total"] = cart_data.get("total", 0) + cart_data.get("calendar_total", 0)
else:
ctx["cart_count"] = 0
ctx["cart_total"] = 0

View File

@@ -3,11 +3,12 @@ from datetime import datetime, timezone
from decimal import Decimal
from quart import (
request, render_template, make_response, Blueprint, g, redirect, url_for, jsonify
request, render_template, render_template_string, make_response,
Blueprint, g, redirect, url_for, jsonify,
)
from sqlalchemy import update
from sqlalchemy import update, func as sa_func
from models.calendars import CalendarEntry
@@ -205,8 +206,32 @@ def register():
entry.ticket_price = ticket_price
entry.ticket_count = ticket_count
# Count pending calendar entries from local session (sees the just-added entry)
user_id = getattr(g, "user", None) and g.user.id
cal_filters = [
CalendarEntry.deleted_at.is_(None),
CalendarEntry.state == "pending",
]
if user_id:
cal_filters.append(CalendarEntry.user_id == user_id)
cal_count = await g.s.scalar(
select(sa_func.count()).select_from(CalendarEntry).where(*cal_filters)
) or 0
# Get product cart count from API (committed data only, which is fine)
from shared.infrastructure.internal_api import get as api_get
cart_data = await api_get("cart", "/internal/cart/summary", forward_session=True)
product_count = cart_data.get("count", 0) if cart_data else 0
total_count = product_count + cal_count
html = await render_template("_types/day/_main_panel.html")
return await make_response(html, 200)
mini_html = await render_template_string(
'{% from "_types/cart/_mini.html" import mini with context %}'
'{{ mini(oob="true") }}',
cart_count=total_count,
)
return await make_response(html + mini_html, 200)
@bp.get("/add/")
async def add_form(day: int, month: int, year: int, **kwargs):

2
shared

Submodule shared updated: 0c0f3c8416...e3f8ff6e3c