diff --git a/bp/cart/services/page_cart.py b/bp/cart/services/page_cart.py index 13cf49f..7626ebd 100644 --- a/bp/cart/services/page_cart.py +++ b/bp/cart/services/page_cart.py @@ -60,12 +60,13 @@ async def get_calendar_entries_for_page(session, post_id: int): async def get_cart_grouped_by_page(session) -> list[dict]: """ Load all cart items + calendar entries for the current identity, - grouped by owning page (post_id). + grouped by market_place (one card per market). Returns a list of dicts: { "post": Post | None, "page_config": PageConfig | None, + "market_place": MarketPlace | None, "cart_items": [...], "calendar_entries": [...], "product_count": int, @@ -75,6 +76,7 @@ async def get_cart_grouped_by_page(session) -> list[dict]: "total": float, } + Calendar entries (no market concept) attach to a page-level group. Items without a market_place go in an orphan bucket (post=None). """ from .get_cart import get_cart @@ -85,31 +87,50 @@ async def get_cart_grouped_by_page(session) -> list[dict]: cart_items = await get_cart(session) cal_entries = await get_calendar_cart_entries(session) - # Group by container_id (all current data has container_type="page") - groups: dict[int | None, dict] = defaultdict(lambda: { - "post_id": None, - "cart_items": [], - "calendar_entries": [], - }) - + # Group cart items by market_place_id + market_groups: dict[int | None, dict] = {} for ci in cart_items: - if ci.market_place and ci.market_place.container_id: - pid = ci.market_place.container_id - else: - pid = None - groups[pid]["post_id"] = pid - groups[pid]["cart_items"].append(ci) + mp_id = ci.market_place_id if ci.market_place else None + if mp_id not in market_groups: + market_groups[mp_id] = { + "market_place": ci.market_place, + "post_id": ci.market_place.container_id if ci.market_place else None, + "cart_items": [], + "calendar_entries": [], + } + market_groups[mp_id]["cart_items"].append(ci) + + # Attach calendar entries to an existing market group for the same page, + # or create a page-level group if no market group exists for that page. + page_to_market: dict[int | None, int | None] = {} + for mp_id, grp in market_groups.items(): + pid = grp["post_id"] + if pid is not None and pid not in page_to_market: + page_to_market[pid] = mp_id for ce in cal_entries: - if ce.calendar_container_id: - pid = ce.calendar_container_id + pid = ce.calendar_container_id or None + if pid in page_to_market: + market_groups[page_to_market[pid]]["calendar_entries"].append(ce) else: - pid = None - groups[pid]["post_id"] = pid - groups[pid]["calendar_entries"].append(ce) + # Create a page-level group for calendar-only entries + key = ("cal", pid) + if key not in market_groups: + market_groups[key] = { + "market_place": None, + "post_id": pid, + "cart_items": [], + "calendar_entries": [], + } + if pid is not None: + page_to_market[pid] = key + market_groups[key]["calendar_entries"].append(ce) # Batch-load Post DTOs and PageConfig objects - post_ids = [pid for pid in groups if pid is not None] + post_ids = list({ + grp["post_id"] for grp in market_groups.values() + if grp["post_id"] is not None + }) posts_by_id: dict[int, object] = {} configs_by_post: dict[int, PageConfig] = {} @@ -126,18 +147,22 @@ async def get_cart_grouped_by_page(session) -> list[dict]: for pc in pc_result.scalars().all(): configs_by_post[pc.container_id] = pc - # Build result list (pages first, orphan last) + # Build result list (markets with pages first, orphan last) result = [] - for pid in sorted(groups, key=lambda x: (x is None, x)): - grp = groups[pid] + for _key, grp in sorted( + market_groups.items(), + key=lambda kv: (kv[1]["post_id"] is None, kv[1]["post_id"] or 0), + ): items = grp["cart_items"] entries = grp["calendar_entries"] prod_total = calc_product_total(items) or 0 cal_total = calc_calendar_total(entries) or 0 + pid = grp["post_id"] result.append({ "post": posts_by_id.get(pid) if pid else None, "page_config": configs_by_post.get(pid) if pid else None, + "market_place": grp["market_place"], "cart_items": items, "calendar_entries": entries, "product_count": sum(ci.quantity for ci in items), diff --git a/templates/_types/cart/overview/_main_panel.html b/templates/_types/cart/overview/_main_panel.html index 68a9cd9..c26e451 100644 --- a/templates/_types/cart/overview/_main_panel.html +++ b/templates/_types/cart/overview/_main_panel.html @@ -33,7 +33,7 @@ {% if grp.cart_items or grp.calendar_entries %} {% if grp.post %} - {# Page cart card #} + {# Market / page cart card #}

- {{ grp.post.title }} + {% if grp.market_place %} + {{ grp.market_place.name }} + {% else %} + {{ grp.post.title }} + {% endif %}

+ {% if grp.market_place %} +

{{ grp.post.title }}

+ {% endif %}
{% if grp.product_count > 0 %}