-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathviews.py
50 lines (41 loc) · 1.65 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from http import HTTPStatus
from fastapi import APIRouter, Depends, HTTPException, Request
from fastapi.responses import HTMLResponse
from lnbits.core.crud import get_wallet
from lnbits.core.models import User
from lnbits.decorators import check_user_exists
from lnbits.helpers import template_renderer
from .crud import get_card_by_external_id, get_hits, get_refunds
boltcards_generic_router = APIRouter()
def boltcards_renderer():
return template_renderer(["boltcards/templates"])
@boltcards_generic_router.get("/", response_class=HTMLResponse)
async def index(request: Request, user: User = Depends(check_user_exists)):
return boltcards_renderer().TemplateResponse(
"boltcards/index.html", {"request": request, "user": user.json()}
)
@boltcards_generic_router.get("/{card_id}", response_class=HTMLResponse)
async def display(request: Request, card_id: str):
card = await get_card_by_external_id(card_id)
if not card:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Card does not exist."
)
wallet = await get_wallet(card.wallet)
wallet_balance = 0
if wallet:
wallet_balance = wallet.balance
hits = await get_hits([card.id])
hits_json = [hit.json() for hit in hits]
refunds = [refund.hit_id for refund in await get_refunds([hit.id for hit in hits])]
card_json = card.json(exclude={"wallet"})
return boltcards_renderer().TemplateResponse(
"boltcards/display.html",
{
"request": request,
"card": card_json,
"hits": hits_json,
"refunds": refunds,
"balance": int(wallet_balance),
},
)