Adds public events endpoint and user tickets
Some checks are pending
lint / lint (push) Waiting to run
/ release (push) Waiting to run
/ pullrequest (push) Blocked by required conditions

Adds a public events endpoint that allows read-only access to all events.
Improves ticket management by adding support for user IDs as an identifier, alongside name and email.
This simplifies ticket creation for authenticated users and enhances security.
Also introduces an API endpoint to fetch tickets by user ID.
This commit is contained in:
padreug 2025-11-03 23:05:31 +01:00
parent a9ac6dcfc1
commit 4fb6d90fcd
2 changed files with 14 additions and 14 deletions

26
crud.py
View file

@ -19,7 +19,7 @@ async def create_ticket(
extra: Optional[dict] = None, extra: Optional[dict] = None,
) -> Ticket: ) -> Ticket:
now = datetime.now(timezone.utc) now = datetime.now(timezone.utc)
# Handle database constraints: if user_id is provided, use empty strings for name/email # Handle database constraints: if user_id is provided, use empty strings for name/email
if user_id: if user_id:
db_name = "" db_name = ""
@ -27,7 +27,7 @@ async def create_ticket(
else: else:
db_name = name or "" db_name = name or ""
db_email = email or "" db_email = email or ""
ticket = Ticket( ticket = Ticket(
id=payment_hash, id=payment_hash,
wallet=wallet, wallet=wallet,
@ -41,12 +41,12 @@ async def create_ticket(
time=now, time=now,
extra=TicketExtra(**extra) if extra else TicketExtra(), extra=TicketExtra(**extra) if extra else TicketExtra(),
) )
# Create a dict for database insertion with proper handling of constraints # Create a dict for database insertion with proper handling of constraints
ticket_dict = ticket.dict() ticket_dict = ticket.dict()
ticket_dict["name"] = db_name ticket_dict["name"] = db_name
ticket_dict["email"] = db_email ticket_dict["email"] = db_email
await db.execute( await db.execute(
""" """
INSERT INTO events.ticket (id, wallet, event, name, email, user_id, registered, paid, time, reg_timestamp, extra) INSERT INTO events.ticket (id, wallet, event, name, email, user_id, registered, paid, time, reg_timestamp, extra)
@ -60,16 +60,16 @@ async def create_ticket(
async def update_ticket(ticket: Ticket) -> Ticket: async def update_ticket(ticket: Ticket) -> Ticket:
# Create a new Ticket object with corrected values for database constraints # Create a new Ticket object with corrected values for database constraints
ticket_dict = ticket.dict() ticket_dict = ticket.dict()
# Convert None values to empty strings for database constraints # Convert None values to empty strings for database constraints
if ticket_dict.get("name") is None: if ticket_dict.get("name") is None:
ticket_dict["name"] = "" ticket_dict["name"] = ""
if ticket_dict.get("email") is None: if ticket_dict.get("email") is None:
ticket_dict["email"] = "" ticket_dict["email"] = ""
# Create a new Ticket object with the corrected values # Create a new Ticket object with the corrected values
corrected_ticket = Ticket(**ticket_dict) corrected_ticket = Ticket(**ticket_dict)
await db.update("events.ticket", corrected_ticket) await db.update("events.ticket", corrected_ticket)
return ticket return ticket
@ -97,7 +97,7 @@ async def get_tickets(wallet_ids: str | list[str]) -> list[Ticket]:
wallet_ids = [wallet_ids] wallet_ids = [wallet_ids]
q = ",".join([f"'{wallet_id}'" for wallet_id in wallet_ids]) q = ",".join([f"'{wallet_id}'" for wallet_id in wallet_ids])
rows = await db.fetchall(f"SELECT * FROM events.ticket WHERE wallet IN ({q})") rows = await db.fetchall(f"SELECT * FROM events.ticket WHERE wallet IN ({q})")
tickets = [] tickets = []
for row in rows: for row in rows:
# Convert empty strings back to None for the model # Convert empty strings back to None for the model
@ -107,7 +107,7 @@ async def get_tickets(wallet_ids: str | list[str]) -> list[Ticket]:
if ticket_data.get("email") == "": if ticket_data.get("email") == "":
ticket_data["email"] = None ticket_data["email"] = None
tickets.append(Ticket(**ticket_data)) tickets.append(Ticket(**ticket_data))
return tickets return tickets
@ -117,7 +117,7 @@ async def get_tickets_by_user_id(user_id: str) -> list[Ticket]:
"SELECT * FROM events.ticket WHERE user_id = :user_id ORDER BY time DESC", "SELECT * FROM events.ticket WHERE user_id = :user_id ORDER BY time DESC",
{"user_id": user_id} {"user_id": user_id}
) )
tickets = [] tickets = []
for row in rows: for row in rows:
# Convert empty strings back to None for the model # Convert empty strings back to None for the model
@ -127,7 +127,7 @@ async def get_tickets_by_user_id(user_id: str) -> list[Ticket]:
if ticket_data.get("email") == "": if ticket_data.get("email") == "":
ticket_data["email"] = None ticket_data["email"] = None
tickets.append(Ticket(**ticket_data)) tickets.append(Ticket(**ticket_data))
return tickets return tickets
@ -199,7 +199,7 @@ async def get_event_tickets(event_id: str) -> list[Ticket]:
"SELECT * FROM events.ticket WHERE event = :event", "SELECT * FROM events.ticket WHERE event = :event",
{"event": event_id}, {"event": event_id},
) )
tickets = [] tickets = []
for row in rows: for row in rows:
# Convert empty strings back to None for the model # Convert empty strings back to None for the model
@ -209,5 +209,5 @@ async def get_event_tickets(event_id: str) -> list[Ticket]:
if ticket_data.get("email") == "": if ticket_data.get("email") == "":
ticket_data["email"] = None ticket_data["email"] = None
tickets.append(Ticket(**ticket_data)) tickets.append(Ticket(**ticket_data))
return tickets return tickets

View file

@ -37,7 +37,7 @@ class CreateEvent(BaseModel):
currency: str = "sat" currency: str = "sat"
amount_tickets: int = Query(..., ge=0) amount_tickets: int = Query(..., ge=0)
price_per_ticket: float = Query(..., ge=0) price_per_ticket: float = Query(..., ge=0)
banner: str | None = None banner: Optional[str] = None
extra: EventExtra = Field(default_factory=EventExtra) extra: EventExtra = Field(default_factory=EventExtra)