Replaces the previous "one row, N seats via extra.quantity" model
with proper one-row-per-attendee semantics. Each attendee gets a
unique scannable id; the door PUT /register/{ticket_id} marks
them registered independently — so a buyer can purchase 3 tickets,
hand 2 QRs to friends arriving separately, and each attendee can
enter on their own schedule.
Schema (migrations_fork.py m002):
- ticket.payment_hash: new TEXT column shared across all rows of
a multi-ticket purchase. Backfilled `payment_hash = id` for
pre-migration rows (id WAS the payment_hash by invariant).
Wire:
- TicketPaymentRequest grows `ticket_ids: list[str]` so the
webapp gets every scannable id back in the create response.
- POST /tickets/{event_id}/{payment_hash} polling endpoint now
reports `ticket_ids` (every row) + keeps `ticket_id` for
back-compat.
- api_ticket_create loops quantity times; the first row reuses
payment_hash as id (preserves legacy `id == payment_hash`
invariant for single-ticket purchases), the rest get
urlsafe_short_hash() uuids.
Payment flow:
- on_invoice_paid fetches all rows by payment_hash and marks each
paid via set_ticket_paid, which now increments event.sold by 1
per row (was N per row via extra.quantity — simpler now). The
per-event asyncio lock still serializes counter + republish so
concurrent multi-ticket purchases for the same event don't
reorder the published Nostr state.
- Each paid row triggers its own send_ticket_notification_in_
background call — no-op for buyers without nostr_identifier /
email, useful when the buyer set those on the row.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
190 lines
6 KiB
Python
190 lines
6 KiB
Python
import json
|
|
from datetime import datetime
|
|
|
|
from pydantic import BaseModel, EmailStr, Field, root_validator, validator
|
|
|
|
|
|
class PromoCode(BaseModel):
|
|
code: str
|
|
discount_percent: float = 0.0
|
|
active: bool = True
|
|
|
|
# make the promo code uppercase
|
|
@validator("code")
|
|
def uppercase_code(cls, v):
|
|
return v.upper()
|
|
|
|
@validator("discount_percent")
|
|
def validate_discount_percent(cls, v):
|
|
assert 0 <= v <= 100, "Discount must be between 0 and 100."
|
|
return v
|
|
|
|
|
|
class EventExtra(BaseModel):
|
|
promo_codes: list[PromoCode] = Field(default_factory=list)
|
|
conditional: bool = False
|
|
min_tickets: int = 1
|
|
email_notifications: bool = False
|
|
nostr_notifications: bool = False
|
|
notification_subject: str = ""
|
|
notification_body: str = ""
|
|
|
|
|
|
class CreateEvent(BaseModel):
|
|
wallet: str | None = None # filled from caller's wallet if absent
|
|
name: str # title (required)
|
|
info: str = "" # description (optional)
|
|
closing_date: str | None = None # date-only YYYY-MM-DD; defaults to event_end_date
|
|
# ISO 8601: date-only ("2026-05-19") or datetime ("2026-05-19T18:30").
|
|
# Presence of a "T" toggles NIP-52 kind (31922 date / 31923 time).
|
|
event_start_date: str
|
|
event_end_date: str | None = None # same format as event_start_date
|
|
currency: str = "sat"
|
|
allow_fiat: bool = False
|
|
fiat_currency: str = "GBP"
|
|
amount_tickets: int = 0 # 0 = unlimited / not ticketed
|
|
price_per_ticket: float = 0 # 0 = free
|
|
banner: str | None = None
|
|
location: str | None = None # venue/address (NIP-52 'location' tag)
|
|
categories: list[str] = Field(default_factory=list) # NIP-52 't' tags
|
|
extra: EventExtra = Field(default_factory=EventExtra)
|
|
status: str = "approved" # proposed, approved, rejected
|
|
|
|
|
|
class Event(BaseModel):
|
|
id: str
|
|
wallet: str
|
|
name: str
|
|
info: str = ""
|
|
closing_date: str | None = None
|
|
canceled: bool = False
|
|
event_start_date: str
|
|
event_end_date: str | None = None
|
|
currency: str = "sat"
|
|
allow_fiat: bool = False
|
|
fiat_currency: str = "GBP"
|
|
amount_tickets: int = 0
|
|
price_per_ticket: float = 0
|
|
time: datetime
|
|
sold: int = 0
|
|
banner: str | None = None
|
|
location: str | None = None
|
|
categories: list[str] = Field(default_factory=list)
|
|
extra: EventExtra = Field(default_factory=EventExtra)
|
|
status: str = "approved"
|
|
nostr_event_id: str | None = None
|
|
nostr_event_created_at: int | None = None
|
|
|
|
@validator("categories", pre=True)
|
|
def parse_categories(cls, v):
|
|
if isinstance(v, str):
|
|
return json.loads(v) if v else []
|
|
return v or []
|
|
|
|
|
|
class PublicEvent(BaseModel):
|
|
id: str
|
|
name: str
|
|
info: str
|
|
closing_date: str | None = None
|
|
canceled: bool
|
|
event_start_date: str
|
|
event_end_date: str | None = None
|
|
currency: str
|
|
allow_fiat: bool = False
|
|
fiat_currency: str = "GBP"
|
|
price_per_ticket: float
|
|
banner: str | None
|
|
location: str | None = None
|
|
categories: list[str] = Field(default_factory=list)
|
|
extra: EventExtra = Field(default_factory=EventExtra)
|
|
status: str = "approved" # surfaces "proposed"/"rejected" so SFC can render banner
|
|
|
|
@validator("categories", pre=True)
|
|
def parse_categories(cls, v):
|
|
if isinstance(v, str):
|
|
return json.loads(v) if v else []
|
|
return v or []
|
|
|
|
|
|
class EventsSettings(BaseModel):
|
|
"""Extension-level settings for the events extension."""
|
|
|
|
auto_approve: bool = False # Skip approval workflow for non-admin users
|
|
|
|
|
|
class TicketExtra(BaseModel):
|
|
applied_promo_code: str | None = None
|
|
sats_paid: int | None = None
|
|
refund_address: str | None = None
|
|
nostr_identifier: str | None = None
|
|
ticket_base_url: str | None = None
|
|
email_notification_sent: bool = False
|
|
nostr_notification_sent: bool = False
|
|
refunded: bool = False
|
|
|
|
|
|
class CreateTicket(BaseModel):
|
|
name: str | None = None
|
|
email: EmailStr | None = None
|
|
user_id: str | None = None # LNbits user id (alternative to name+email)
|
|
promo_code: str | None = None
|
|
refund_address: str | None = None
|
|
nostr_identifier: str | None = None
|
|
payment_method: str | None = None
|
|
fiat_provider: str | None = None
|
|
# Number of tickets to buy on this single invoice. Bounded so a
|
|
# bad client can't run away with the organizer's capacity.
|
|
quantity: int = Field(default=1, ge=1, le=10)
|
|
|
|
@root_validator
|
|
def validate_identifiers(cls, values):
|
|
name = values.get("name")
|
|
email = values.get("email")
|
|
user_id = values.get("user_id")
|
|
if not user_id and not (name and email):
|
|
raise ValueError("Either user_id or both name and email must be provided")
|
|
if user_id and (name or email):
|
|
raise ValueError("Cannot provide both user_id and name/email")
|
|
return values
|
|
|
|
|
|
class Ticket(BaseModel):
|
|
id: str
|
|
wallet: str
|
|
event: str
|
|
name: str | None = None
|
|
email: str | None = None
|
|
user_id: str | None = None
|
|
registered: bool
|
|
paid: bool
|
|
time: datetime
|
|
reg_timestamp: datetime
|
|
extra: TicketExtra = Field(default_factory=TicketExtra)
|
|
# Shared LNbits invoice payment_hash. Equals `id` for single-ticket
|
|
# purchases (legacy + post-migration default). Multi-ticket
|
|
# purchases create N rows sharing one payment_hash so each attendee
|
|
# gets a distinct scannable id while the buyer pays once.
|
|
payment_hash: str | None = None
|
|
|
|
|
|
class PublicTicket(BaseModel):
|
|
event: str
|
|
name: str | None = None
|
|
registered: bool
|
|
paid: bool
|
|
time: datetime
|
|
reg_timestamp: datetime
|
|
|
|
|
|
class TicketPaymentRequest(BaseModel):
|
|
payment_hash: str
|
|
payment_request: str | None = None
|
|
fiat_payment_request: str | None = None
|
|
fiat_provider: str | None = None
|
|
is_fiat: bool = False
|
|
# Row ids created on this invoice — one for single-ticket
|
|
# purchases, N for multi-ticket (each independently scannable at
|
|
# the door). Buyers fetch these after payment to render N QRs in
|
|
# My Tickets.
|
|
ticket_ids: list[str] = Field(default_factory=list)
|