62 lines
1.8 KiB
Python
62 lines
1.8 KiB
Python
from typing import List
|
|
import uuid
|
|
|
|
from django.utils import timezone
|
|
|
|
from bookings.models import Booking, Ticket
|
|
|
|
|
|
def _generate_ticket_id(booking: Booking) -> str:
|
|
"""
|
|
Generate a ticket_id based on the event name and a random UUID segment.
|
|
|
|
Pattern: <EVT><RANDOM_HEX>
|
|
- EVT: first 3 characters of event name (uppercase), or 'EVT' fallback
|
|
- RANDOM_HEX: first 10 chars of uuid4 hex (uppercase)
|
|
"""
|
|
event = getattr(booking.ticket_meta, "event", None)
|
|
if event and getattr(event, "name", None):
|
|
prefix = (event.name or "EVT")[:3].upper()
|
|
else:
|
|
prefix = "EVT"
|
|
|
|
return prefix + uuid.uuid4().hex[:10].upper()
|
|
|
|
|
|
def generate_tickets_for_booking(booking: Booking) -> List[Ticket]:
|
|
"""
|
|
Generate Ticket instances for a given Booking based on its quantity.
|
|
|
|
This function does NOT perform any payment or business-rule validation.
|
|
It simply creates one Ticket per quantity on the booking.
|
|
|
|
Args:
|
|
booking: Booking instance for which tickets should be generated.
|
|
|
|
Returns:
|
|
List[Ticket]: List of created Ticket instances.
|
|
"""
|
|
if not isinstance(booking, Booking):
|
|
raise TypeError("booking must be a Booking instance")
|
|
|
|
if booking.quantity <= 0:
|
|
return []
|
|
|
|
tickets: List[Ticket] = []
|
|
for _ in range(booking.quantity):
|
|
tickets.append(
|
|
Ticket(
|
|
booking=booking,
|
|
ticket_id=_generate_ticket_id(booking),
|
|
is_checked_in=False,
|
|
checked_in_date_time=None,
|
|
)
|
|
)
|
|
|
|
# Bulk create for efficiency
|
|
Ticket.objects.bulk_create(tickets)
|
|
|
|
# Refresh from DB to ensure we have primary keys and any defaults
|
|
return list[Ticket](Ticket.objects.filter(booking=booking).order_by("id"))
|
|
|