feat: per-page carts with overview, page-scoped checkout, and split blueprints (Phase 4)
Some checks failed
Build and Deploy / build-and-deploy (push) Failing after 21s

Splits the monolithic cart blueprint into three: cart_overview (GET /),
page_cart (/<page_slug>/), and cart_global (webhook, return, add).

- New page_cart.py service: get_cart_for_page(), get_calendar_entries_for_page(), get_cart_grouped_by_page()
- clear_cart_for_order() and create_order_from_cart() accept page_post_id for scoping
- Cart app hydrates page_slug via url_value_preprocessor/url_defaults/hydrate_page
- Context processor provides page-scoped cart data when g.page_post exists
- Internal API /internal/cart/summary accepts ?page_slug= for page-scoped counts
- Overview template shows page cards with item counts and totals
- Page cart template reuses show_cart() macro with page-specific header

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
giles
2026-02-10 21:45:30 +00:00
parent 7bdb736ef5
commit cb2fcd9d32
23 changed files with 968 additions and 56 deletions

124
app.py
View File

@@ -1,17 +1,31 @@
from __future__ import annotations
import path_setup # noqa: F401 # adds shared_lib to sys.path
from quart import g
from pathlib import Path
from quart import g, abort
from jinja2 import FileSystemLoader, ChoiceLoader
from sqlalchemy import select
from shared.factory import create_base_app
from suma_browser.app.bp import register_cart_bp, register_orders, register_cart_api
from suma_browser.app.bp import (
register_cart_overview,
register_page_cart,
register_cart_global,
register_cart_api,
register_orders,
)
from suma_browser.app.bp.cart.services import (
get_cart,
total,
get_calendar_cart_entries,
calendar_total,
)
from suma_browser.app.bp.cart.services.page_cart import (
get_cart_for_page,
get_calendar_entries_for_page,
)
async def _load_cart():
@@ -27,6 +41,9 @@ async def cart_context() -> dict:
(cart app owns this data)
- cart_count: derived from cart + calendar entries (for _mini.html)
- menu_items: fetched from coop internal API
When g.page_post exists, cart and calendar_cart_entries are page-scoped.
Global cart_count / cart_total stay global for cart-mini.
"""
from shared.context import base_context
from shared.internal_api import get as api_get, dictobj
@@ -34,19 +51,30 @@ async def cart_context() -> dict:
ctx = await base_context()
# Cart app owns cart data — use g.cart from _load_cart
cart = getattr(g, "cart", None) or []
cal_entries = await get_calendar_cart_entries(g.s)
all_cart = getattr(g, "cart", None) or []
all_cal = await get_calendar_cart_entries(g.s)
# Global counts for cart-mini (always global)
cart_qty = sum(ci.quantity for ci in all_cart) if all_cart else 0
ctx["cart_count"] = cart_qty + len(all_cal)
ctx["cart_total"] = (total(all_cart) or 0) + (calendar_total(all_cal) or 0)
# Page-scoped data when viewing a page cart
page_post = getattr(g, "page_post", None)
if page_post:
page_cart = await get_cart_for_page(g.s, page_post.id)
page_cal = await get_calendar_entries_for_page(g.s, page_post.id)
ctx["cart"] = page_cart
ctx["calendar_cart_entries"] = page_cal
ctx["page_post"] = page_post
ctx["page_config"] = getattr(g, "page_config", None)
else:
ctx["cart"] = all_cart
ctx["calendar_cart_entries"] = all_cal
ctx["cart"] = cart
ctx["calendar_cart_entries"] = cal_entries
ctx["total"] = total
ctx["calendar_total"] = calendar_total
# Also set cart_count so _mini.html works the same way
cart_qty = sum(ci.quantity for ci in cart) if cart else 0
ctx["cart_count"] = cart_qty + len(cal_entries)
ctx["cart_total"] = (total(cart) or 0) + (calendar_total(cal_entries) or 0)
# Menu items from coop API (wrapped for attribute access in templates)
menu_data = await api_get("coop", "/internal/menu-items")
ctx["menu_items"] = dictobj(menu_data) if menu_data else []
@@ -55,26 +83,82 @@ async def cart_context() -> dict:
def create_app() -> "Quart":
from models.ghost_content import Post
from models.page_config import PageConfig
app = create_base_app(
"cart",
context_fn=cart_context,
before_request_fns=[_load_cart],
)
# Cart blueprint at root (was /cart in monolith)
app.register_blueprint(
register_cart_bp(url_prefix="/"),
url_prefix="/",
)
# App-specific templates override shared templates
app_templates = str(Path(__file__).resolve().parent / "templates")
app.jinja_loader = ChoiceLoader([
FileSystemLoader(app_templates),
app.jinja_loader,
])
# Orders blueprint
app.register_blueprint(
register_orders(url_prefix="/orders"),
)
# --- Page slug hydration (follows events/market app pattern) ---
@app.url_value_preprocessor
def pull_page_slug(endpoint, values):
if values and "page_slug" in values:
g.page_slug = values.pop("page_slug")
@app.url_defaults
def inject_page_slug(endpoint, values):
slug = g.get("page_slug")
if slug and "page_slug" not in values:
if app.url_map.is_endpoint_expecting(endpoint, "page_slug"):
values["page_slug"] = slug
@app.before_request
async def hydrate_page():
slug = getattr(g, "page_slug", None)
if not slug:
return
post = (
await g.s.execute(
select(Post).where(Post.slug == slug, Post.is_page == True) # noqa: E712
)
).scalar_one_or_none()
if not post:
abort(404)
g.page_post = post
g.page_config = (
await g.s.execute(
select(PageConfig).where(PageConfig.post_id == post.id)
)
).scalar_one_or_none()
# --- Blueprint registration ---
# Static prefixes first, dynamic (page_slug) last
# Internal API (server-to-server, CSRF-exempt)
app.register_blueprint(register_cart_api())
# Orders blueprint
app.register_blueprint(register_orders(url_prefix="/orders"))
# Global routes (webhook, return, add — specific paths under /)
app.register_blueprint(
register_cart_global(url_prefix="/"),
url_prefix="/",
)
# Cart overview at GET /
app.register_blueprint(
register_cart_overview(url_prefix="/"),
url_prefix="/",
)
# Page cart at /<page_slug>/ (dynamic, matched last)
app.register_blueprint(
register_page_cart(url_prefix="/"),
url_prefix="/<page_slug>",
)
return app