Skip to content

Commit

Permalink
Merge branch 'main' into add_image_banner
Browse files Browse the repository at this point in the history
  • Loading branch information
arcbtc committed Feb 17, 2024
2 parents ae8930f + 07d2f59 commit 38951a7
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 15 deletions.
14 changes: 14 additions & 0 deletions crud.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import List, Optional, Union
from datetime import datetime, timedelta

from lnbits.helpers import urlsafe_short_hash

Expand Down Expand Up @@ -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


Expand Down
18 changes: 14 additions & 4 deletions templates/events/display.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ <h5 class="q-mt-none">Buy Ticket</h5>
v-model.trim="formDialog.data.email"
type="email"
label="Your email "
:rules="[val => emailValidation(val)]"
:rules="[val => !!val || '* Required', val => emailValidation(val)]"
lazy-rules
></q-input>

<div class="row q-mt-lg">
Expand Down Expand Up @@ -122,10 +123,11 @@ <h5 class="q-mt-none">Buy Ticket</h5>
}
}
},
created() {
async created() {
this.info = '{{ event_info | tojson }}'
this.info = this.info.substring(1, this.info.length - 1)
this.banner = JSON.parse('{{ event_banner | tojson |safe }}')
await this.purgeUnpaidTickets()
},
computed: {
formatDescription() {
Expand Down Expand Up @@ -154,8 +156,8 @@ <h5 class="q-mt-none">Buy Ticket</h5>
)
},
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 () {
Expand Down Expand Up @@ -228,6 +230,14 @@ <h5 class="q-mt-none">Buy Ticket</h5>
.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)
}
}
}
})
Expand Down
15 changes: 8 additions & 7 deletions templates/events/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -425,9 +425,11 @@ <h6 class="text-subtitle1 q-my-none">
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) {
Expand Down Expand Up @@ -469,7 +471,6 @@ <h6 class="text-subtitle1 q-my-none">
self.events = response.data.map(function (obj) {
return mapEvents(obj)
})
console.log(self.events)
})
},
sendEventData: function () {
Expand All @@ -488,7 +489,7 @@ <h6 class="text-subtitle1 q-my-none">
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
Expand All @@ -512,7 +513,7 @@ <h6 class="text-subtitle1 q-my-none">
.request(
'PUT',
'/events/api/v1/events/' + data.id,
wallet.inkey,
wallet.adminkey,
data
)
.then(function (response) {
Expand All @@ -538,7 +539,7 @@ <h6 class="text-subtitle1 q-my-none">
.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) {
Expand Down
1 change: 0 additions & 1 deletion templates/events/register.html
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ <h3 class="q-my-none">{{ event_name }} Registration</h3>
},
getEventTickets: function () {
var self = this
console.log('obj')
LNbits.api
.request(
'GET',
Expand Down
26 changes: 23 additions & 3 deletions views_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -27,6 +32,7 @@
reg_ticket,
set_ticket_paid,
update_event,
purge_unpaid_tickets,
)
from .models import CreateEvent, CreateTicket

Expand All @@ -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)
Expand All @@ -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(
Expand Down Expand Up @@ -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


Expand Down

0 comments on commit 38951a7

Please sign in to comment.