From 07d2f59bc34fce90e459e2182fad0e42cbb82b39 Mon Sep 17 00:00:00 2001 From: Tiago Vasconcelos Date: Mon, 5 Feb 2024 07:50:39 +0000 Subject: [PATCH] Refactor events extensions (#21) * require admin key * remove log * only show paid tickets * purge tickets --- crud.py | 14 ++++++++++++++ templates/events/display.html | 18 ++++++++++++++---- templates/events/index.html | 15 ++++++++------- templates/events/register.html | 1 - views_api.py | 26 +++++++++++++++++++++++--- 5 files changed, 59 insertions(+), 15 deletions(-) diff --git a/crud.py b/crud.py index 0d9c25b..c023b20 100644 --- a/crud.py +++ b/crud.py @@ -1,4 +1,5 @@ from typing import List, Optional, Union +from datetime import datetime, timedelta from lnbits.helpers import urlsafe_short_hash @@ -85,6 +86,19 @@ async def delete_event_tickets(event_id: str) -> None: await db.execute("DELETE FROM events.ticket WHERE event = ?", (event_id,)) +async def purge_unpaid_tickets(event_id: str) -> None: + time_diff = datetime.now() - timedelta(hours=24) + await db.execute( + """ + DELETE FROM events.ticket WHERE event = ? AND paid = false AND time < ? + """, + ( + event_id, + time_diff, + ), + ) + + # EVENTS diff --git a/templates/events/display.html b/templates/events/display.html index 60ec944..180170d 100644 --- a/templates/events/display.html +++ b/templates/events/display.html @@ -26,7 +26,8 @@
Buy Ticket
v-model.trim="formDialog.data.email" type="email" label="Your email " - :rules="[val => emailValidation(val)]" + :rules="[val => !!val || '* Required', val => emailValidation(val)]" + lazy-rules >
@@ -121,9 +122,10 @@
Buy Ticket
} } }, - created() { + async created() { this.info = '{{ event_info | tojson }}' this.info = this.info.substring(1, this.info.length - 1) + await this.purgeUnpaidTickets() }, computed: { formatDescription() { @@ -152,8 +154,8 @@
Buy Ticket
) }, emailValidation(val) { - let regex = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/g - return !regex.test(val) || 'Please enter valid email.' + let regex = /^[\w\.-]+@[a-zA-Z\d\.-]+\.[a-zA-Z]{2,}$/ + return regex.test(val) || 'Please enter valid email.' }, Invoice: function () { @@ -226,6 +228,14 @@
Buy Ticket
.catch(function (error) { LNbits.utils.notifyApiError(error) }) + }, + async purgeUnpaidTickets() { + try { + await LNbits.api.request('GET', `/events/api/v1/purge/{{ event_id }}`) + } catch (error) { + console.warn(error) + LNbits.utils.notifyApiError(error) + } } } }) diff --git a/templates/events/index.html b/templates/events/index.html index cf3bbba..5b4360c 100644 --- a/templates/events/index.html +++ b/templates/events/index.html @@ -416,9 +416,11 @@
this.g.user.wallets[0].inkey ) .then(function (response) { - self.tickets = response.data.map(function (obj) { - return mapEvents(obj) - }) + self.tickets = response.data + .map(function (obj) { + return mapEvents(obj) + }) + .filter(e => e.paid) }) }, deleteTicket: function (ticketId) { @@ -461,7 +463,6 @@
self.events = response.data.map(function (obj) { return mapEvents(obj) }) - console.log(self.events) }) }, sendEventData: function () { @@ -480,7 +481,7 @@
createEvent: function (wallet, data) { var self = this LNbits.api - .request('POST', '/events/api/v1/events', wallet.inkey, data) + .request('POST', '/events/api/v1/events', wallet.adminkey, data) .then(function (response) { self.events.push(mapEvents(response.data)) self.formDialog.show = false @@ -504,7 +505,7 @@
.request( 'PUT', '/events/api/v1/events/' + data.id, - wallet.inkey, + wallet.adminkey, data ) .then(function (response) { @@ -530,7 +531,7 @@
.request( 'DELETE', '/events/api/v1/events/' + eventsId, - _.findWhere(self.g.user.wallets, {id: events.wallet}).inkey + _.findWhere(self.g.user.wallets, {id: events.wallet}).adminkey ) .then(function (response) { self.events = _.reject(self.events, function (obj) { diff --git a/templates/events/register.html b/templates/events/register.html index 43d4307..aac2214 100644 --- a/templates/events/register.html +++ b/templates/events/register.html @@ -152,7 +152,6 @@

{{ event_name }} Registration

}, getEventTickets: function () { var self = this - console.log('obj') LNbits.api .request( 'GET', diff --git a/views_api.py b/views_api.py index c82338e..c539e9f 100644 --- a/views_api.py +++ b/views_api.py @@ -5,7 +5,12 @@ from lnbits.core.crud import get_standalone_payment, get_user from lnbits.core.services import create_invoice -from lnbits.decorators import WalletTypeInfo, get_key_type +from lnbits.decorators import ( + WalletTypeInfo, + get_key_type, + require_admin_key, + require_invoice_key, +) from lnbits.utils.exchange_rates import ( currencies, fiat_amount_as_satoshis, @@ -27,6 +32,7 @@ reg_ticket, set_ticket_paid, update_event, + purge_unpaid_tickets, ) from .models import CreateEvent, CreateTicket @@ -49,7 +55,9 @@ async def api_events( @events_ext.post("/api/v1/events") @events_ext.put("/api/v1/events/{event_id}") async def api_event_create( - data: CreateEvent, event_id=None, wallet: WalletTypeInfo = Depends(get_key_type) + data: CreateEvent, + event_id=None, + wallet: WalletTypeInfo = Depends(require_admin_key), ): if event_id: event = await get_event(event_id) @@ -70,7 +78,9 @@ async def api_event_create( @events_ext.delete("/api/v1/events/{event_id}") -async def api_form_delete(event_id, wallet: WalletTypeInfo = Depends(get_key_type)): +async def api_form_delete( + event_id, wallet: WalletTypeInfo = Depends(require_admin_key) +): event = await get_event(event_id) if not event: raise HTTPException( @@ -193,6 +203,16 @@ async def api_ticket_delete(ticket_id, wallet: WalletTypeInfo = Depends(get_key_ return "", HTTPStatus.NO_CONTENT +@events_ext.get("/api/v1/purge/{event_id}") +async def api_event_purge_tickets(event_id): + event = await get_event(event_id) + if not event: + raise HTTPException( + status_code=HTTPStatus.NOT_FOUND, detail="Event does not exist." + ) + return await purge_unpaid_tickets(event_id) + + # Event Tickets