Group cart tickets by event with +/- quantity buttons
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 51s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 51s
- Cart page groups tickets by entry+type instead of listing individually - Each group shows event name, type, date, qty with +/- buttons, line total - New POST /cart/ticket-quantity/ route for adjusting from cart page - Summary includes ticket quantities in Items count Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2
app.py
2
app.py
@@ -28,6 +28,7 @@ from bp.cart.services.page_cart import (
|
||||
get_calendar_entries_for_page,
|
||||
get_tickets_for_page,
|
||||
)
|
||||
from bp.cart.services.ticket_groups import group_tickets
|
||||
|
||||
|
||||
async def _load_cart():
|
||||
@@ -78,6 +79,7 @@ async def cart_context() -> dict:
|
||||
ctx["calendar_cart_entries"] = all_cal
|
||||
ctx["ticket_cart_entries"] = all_tickets
|
||||
|
||||
ctx["ticket_groups"] = group_tickets(ctx.get("ticket_cart_entries", []))
|
||||
ctx["total"] = total
|
||||
ctx["calendar_total"] = calendar_total
|
||||
ctx["ticket_total"] = ticket_total
|
||||
|
||||
@@ -81,6 +81,28 @@ def register(url_prefix: str) -> Blueprint:
|
||||
resp.headers["HX-Refresh"] = "true"
|
||||
return resp
|
||||
|
||||
@bp.post("/ticket-quantity/")
|
||||
async def update_ticket_quantity():
|
||||
"""Adjust reserved ticket count (+/- pattern, like products)."""
|
||||
ident = current_cart_identity()
|
||||
form = await request.form
|
||||
entry_id = int(form.get("entry_id", 0))
|
||||
count = max(int(form.get("count", 0)), 0)
|
||||
tt_raw = (form.get("ticket_type_id") or "").strip()
|
||||
ticket_type_id = int(tt_raw) if tt_raw else None
|
||||
|
||||
await services.calendar.adjust_ticket_quantity(
|
||||
g.s, entry_id, count,
|
||||
user_id=ident["user_id"],
|
||||
session_id=ident["session_id"],
|
||||
ticket_type_id=ticket_type_id,
|
||||
)
|
||||
await g.s.flush()
|
||||
|
||||
resp = await make_response("", 200)
|
||||
resp.headers["HX-Refresh"] = "true"
|
||||
return resp
|
||||
|
||||
@bp.post("/delete/<int:product_id>/")
|
||||
async def delete_item(product_id: int):
|
||||
ident = current_cart_identity()
|
||||
|
||||
@@ -14,6 +14,7 @@ from .services import (
|
||||
ticket_total,
|
||||
)
|
||||
from .services.page_cart import get_cart_for_page, get_calendar_entries_for_page, get_tickets_for_page
|
||||
from .services.ticket_groups import group_tickets
|
||||
from .services.checkout import (
|
||||
create_order_from_cart,
|
||||
build_sumup_description,
|
||||
@@ -33,12 +34,15 @@ def register(url_prefix: str) -> Blueprint:
|
||||
cal_entries = await get_calendar_entries_for_page(g.s, post.id)
|
||||
page_tickets = await get_tickets_for_page(g.s, post.id)
|
||||
|
||||
ticket_groups = group_tickets(page_tickets)
|
||||
|
||||
tpl_ctx = dict(
|
||||
page_post=post,
|
||||
page_config=getattr(g, "page_config", None),
|
||||
cart=cart,
|
||||
calendar_cart_entries=cal_entries,
|
||||
ticket_cart_entries=page_tickets,
|
||||
ticket_groups=ticket_groups,
|
||||
total=total,
|
||||
calendar_total=calendar_total,
|
||||
ticket_total=ticket_total,
|
||||
|
||||
43
bp/cart/services/ticket_groups.py
Normal file
43
bp/cart/services/ticket_groups.py
Normal file
@@ -0,0 +1,43 @@
|
||||
"""Group individual TicketDTOs by (entry_id, ticket_type_id) for cart display."""
|
||||
from __future__ import annotations
|
||||
|
||||
from collections import OrderedDict
|
||||
|
||||
|
||||
def group_tickets(tickets) -> list[dict]:
|
||||
"""
|
||||
Group a flat list of TicketDTOs into aggregate rows.
|
||||
|
||||
Returns list of dicts:
|
||||
{
|
||||
"entry_id": int,
|
||||
"entry_name": str,
|
||||
"entry_start_at": datetime,
|
||||
"entry_end_at": datetime | None,
|
||||
"ticket_type_id": int | None,
|
||||
"ticket_type_name": str | None,
|
||||
"price": Decimal | None,
|
||||
"quantity": int,
|
||||
"line_total": float,
|
||||
}
|
||||
"""
|
||||
groups: OrderedDict[tuple, dict] = OrderedDict()
|
||||
|
||||
for tk in tickets:
|
||||
key = (tk.entry_id, getattr(tk, "ticket_type_id", None))
|
||||
if key not in groups:
|
||||
groups[key] = {
|
||||
"entry_id": tk.entry_id,
|
||||
"entry_name": tk.entry_name,
|
||||
"entry_start_at": tk.entry_start_at,
|
||||
"entry_end_at": tk.entry_end_at,
|
||||
"ticket_type_id": getattr(tk, "ticket_type_id", None),
|
||||
"ticket_type_name": tk.ticket_type_name,
|
||||
"price": tk.price,
|
||||
"quantity": 0,
|
||||
"line_total": 0,
|
||||
}
|
||||
groups[key]["quantity"] += 1
|
||||
groups[key]["line_total"] += float(tk.price or 0)
|
||||
|
||||
return list(groups.values())
|
||||
@@ -60,37 +60,102 @@
|
||||
</ul>
|
||||
</div>
|
||||
{% endif %}
|
||||
{% if ticket_cart_entries %}
|
||||
{% if ticket_groups is defined and ticket_groups %}
|
||||
<div class="mt-6 border-t border-stone-200 pt-4">
|
||||
<h2 class="text-base font-semibold mb-2">
|
||||
<i class="fa fa-ticket mr-1" aria-hidden="true"></i>
|
||||
Event tickets
|
||||
</h2>
|
||||
|
||||
<ul class="space-y-2">
|
||||
{% for tk in ticket_cart_entries %}
|
||||
<li class="flex items-start justify-between text-sm">
|
||||
<div>
|
||||
<div class="font-medium">
|
||||
{{ tk.entry_name }}
|
||||
</div>
|
||||
{% if tk.ticket_type_name %}
|
||||
<div class="text-xs text-stone-500">
|
||||
{{ tk.ticket_type_name }}
|
||||
<div class="space-y-3">
|
||||
{% for tg in ticket_groups %}
|
||||
<article class="flex flex-col sm:flex-row gap-3 sm:gap-4 rounded-2xl bg-white shadow-sm border border-stone-200 p-3 sm:p-4">
|
||||
<div class="flex-1 min-w-0">
|
||||
<div class="flex flex-col sm:flex-row sm:items-start justify-between gap-2 sm:gap-3">
|
||||
<div class="min-w-0">
|
||||
<h3 class="text-sm sm:text-base font-semibold text-stone-900">
|
||||
{{ tg.entry_name }}
|
||||
</h3>
|
||||
{% if tg.ticket_type_name %}
|
||||
<p class="mt-0.5 text-[0.7rem] sm:text-xs text-stone-500">
|
||||
{{ tg.ticket_type_name }}
|
||||
</p>
|
||||
{% endif %}
|
||||
<p class="mt-0.5 text-[0.7rem] sm:text-xs text-stone-500">
|
||||
{{ tg.entry_start_at.strftime('%-d %b %Y, %H:%M') }}
|
||||
{% if tg.entry_end_at %}
|
||||
– {{ tg.entry_end_at.strftime('%-d %b %Y, %H:%M') }}
|
||||
{% endif %}
|
||||
</p>
|
||||
</div>
|
||||
<div class="text-left sm:text-right">
|
||||
<p class="text-sm sm:text-base font-semibold text-stone-900">
|
||||
£{{ "%.2f"|format(tg.price or 0) }}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mt-3 flex flex-col sm:flex-row sm:items-center justify-between gap-2 sm:gap-4">
|
||||
<div class="flex items-center gap-2 text-xs sm:text-sm text-stone-700">
|
||||
<span class="text-[0.65rem] sm:text-xs uppercase tracking-wide text-stone-500">Quantity</span>
|
||||
{% set qty_url = url_for('cart_global.update_ticket_quantity') %}
|
||||
|
||||
<form
|
||||
action="{{ qty_url }}"
|
||||
method="post"
|
||||
hx-post="{{ qty_url }}"
|
||||
hx-swap="none"
|
||||
>
|
||||
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
|
||||
<input type="hidden" name="entry_id" value="{{ tg.entry_id }}">
|
||||
{% if tg.ticket_type_id %}
|
||||
<input type="hidden" name="ticket_type_id" value="{{ tg.ticket_type_id }}">
|
||||
{% endif %}
|
||||
<input type="hidden" name="count" value="{{ [tg.quantity - 1, 0] | max }}">
|
||||
<button
|
||||
type="submit"
|
||||
class="inline-flex items-center justify-center w-8 h-8 text-sm font-medium rounded-full border border-emerald-600 text-emerald-700 hover:bg-emerald-50 text-xl"
|
||||
>
|
||||
-
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<span class="inline-flex items-center justify-center px-2 py-1 rounded-full bg-stone-100 text-[0.7rem] sm:text-xs font-medium">
|
||||
{{ tg.quantity }}
|
||||
</span>
|
||||
|
||||
<form
|
||||
action="{{ qty_url }}"
|
||||
method="post"
|
||||
hx-post="{{ qty_url }}"
|
||||
hx-swap="none"
|
||||
>
|
||||
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
|
||||
<input type="hidden" name="entry_id" value="{{ tg.entry_id }}">
|
||||
{% if tg.ticket_type_id %}
|
||||
<input type="hidden" name="ticket_type_id" value="{{ tg.ticket_type_id }}">
|
||||
{% endif %}
|
||||
<input type="hidden" name="count" value="{{ tg.quantity + 1 }}">
|
||||
<button
|
||||
type="submit"
|
||||
class="inline-flex items-center justify-center w-8 h-8 text-sm font-medium rounded-full border border-emerald-600 text-emerald-700 hover:bg-emerald-50 text-xl"
|
||||
>
|
||||
+
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<div class="flex items-center justify-between sm:justify-end gap-3">
|
||||
<p class="text-sm sm:text-base font-semibold text-stone-900">
|
||||
Line total:
|
||||
£{{ "%.2f"|format(tg.line_total) }}
|
||||
</p>
|
||||
</div>
|
||||
{% endif %}
|
||||
<div class="text-xs text-stone-500">
|
||||
{{ tk.entry_start_at.strftime('%-d %b %Y, %H:%M') }}
|
||||
{% if tk.entry_end_at %}
|
||||
– {{ tk.entry_end_at.strftime('%-d %b %Y, %H:%M') }}
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
<div class="ml-4 font-medium">
|
||||
£{{ "%.2f"|format(tk.price or 0) }}
|
||||
</div>
|
||||
</li>
|
||||
</article>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
</section>
|
||||
@@ -114,7 +179,9 @@
|
||||
<div class="flex items-center justify-between">
|
||||
<dt class="text-stone-600">Items</dt>
|
||||
<dd class="text-stone-900">
|
||||
{{ cart | sum(attribute="quantity") }}
|
||||
{% set product_qty = cart | sum(attribute="quantity") %}
|
||||
{% set ticket_qty = ticket_cart_entries | length if ticket_cart_entries else 0 %}
|
||||
{{ product_qty + ticket_qty }}
|
||||
</dd>
|
||||
</div>
|
||||
<div class="flex items-center justify-between">
|
||||
|
||||
Reference in New Issue
Block a user