2021-08-20 12:44:03 +01:00
|
|
|
from http import HTTPStatus
|
|
|
|
|
2021-10-20 12:03:11 +01:00
|
|
|
from fastapi import Request
|
2021-09-28 18:10:22 +01:00
|
|
|
from fastapi.params import Depends
|
2021-08-21 01:55:07 +01:00
|
|
|
from fastapi.templating import Jinja2Templates
|
2021-09-30 14:42:04 +01:00
|
|
|
from starlette.exceptions import HTTPException
|
|
|
|
from starlette.responses import HTMLResponse
|
2021-10-20 12:03:11 +01:00
|
|
|
|
2021-09-30 14:42:04 +01:00
|
|
|
from lnbits.core.models import User
|
2021-10-20 12:03:11 +01:00
|
|
|
from lnbits.decorators import check_user_exists
|
|
|
|
|
|
|
|
from . import lnurlp_ext, lnurlp_renderer
|
|
|
|
from .crud import get_pay_link
|
2021-09-30 14:42:04 +01:00
|
|
|
|
2021-08-21 01:55:07 +01:00
|
|
|
templates = Jinja2Templates(directory="templates")
|
2021-08-20 12:44:03 +01:00
|
|
|
|
2021-10-17 18:33:29 +01:00
|
|
|
|
2021-09-28 18:10:22 +01:00
|
|
|
@lnurlp_ext.get("/", response_class=HTMLResponse)
|
|
|
|
async def index(request: Request, user: User = Depends(check_user_exists)):
|
2021-10-17 18:33:29 +01:00
|
|
|
return lnurlp_renderer().TemplateResponse(
|
|
|
|
"lnurlp/index.html", {"request": request, "user": user.dict()}
|
|
|
|
)
|
2021-08-20 12:44:03 +01:00
|
|
|
|
|
|
|
|
2021-09-28 18:10:22 +01:00
|
|
|
@lnurlp_ext.get("/{link_id}", response_class=HTMLResponse)
|
2021-10-17 18:33:29 +01:00
|
|
|
async def display(request: Request, link_id):
|
2021-08-20 12:44:03 +01:00
|
|
|
link = await get_pay_link(link_id)
|
|
|
|
if not link:
|
2021-09-28 18:10:22 +01:00
|
|
|
raise HTTPException(
|
2021-10-17 18:33:29 +01:00
|
|
|
status_code=HTTPStatus.NOT_FOUND, detail="Pay link does not exist."
|
2021-09-28 18:10:22 +01:00
|
|
|
)
|
2021-10-17 18:33:29 +01:00
|
|
|
ctx = {"request": request, "lnurl": link.lnurl(req=request)}
|
2021-10-10 11:05:43 +02:00
|
|
|
return lnurlp_renderer().TemplateResponse("lnurlp/display.html", ctx)
|
2021-08-20 12:44:03 +01:00
|
|
|
|
|
|
|
|
2021-09-28 18:10:22 +01:00
|
|
|
@lnurlp_ext.get("/print/{link_id}", response_class=HTMLResponse)
|
2021-10-17 18:33:29 +01:00
|
|
|
async def print_qr(request: Request, link_id):
|
2021-08-20 12:44:03 +01:00
|
|
|
link = await get_pay_link(link_id)
|
|
|
|
if not link:
|
2021-09-28 18:10:22 +01:00
|
|
|
raise HTTPException(
|
2021-10-17 18:33:29 +01:00
|
|
|
status_code=HTTPStatus.NOT_FOUND, detail="Pay link does not exist."
|
2021-09-28 18:10:22 +01:00
|
|
|
)
|
2021-10-17 18:33:29 +01:00
|
|
|
ctx = {"request": request, "lnurl": link.lnurl(req=request)}
|
2021-10-10 11:05:43 +02:00
|
|
|
return lnurlp_renderer().TemplateResponse("lnurlp/print_qr.html", ctx)
|