2021-08-20 12:44:03 +01:00
|
|
|
from http import HTTPStatus
|
|
|
|
from io import BytesIO
|
|
|
|
|
2021-10-18 10:58:09 +01:00
|
|
|
import pyqrcode
|
2023-01-04 16:29:47 +01:00
|
|
|
from fastapi import Request, Depends
|
2021-08-21 02:05:45 +01:00
|
|
|
from fastapi.templating import Jinja2Templates
|
2021-09-30 14:42:04 +01:00
|
|
|
from starlette.exceptions import HTTPException
|
2021-10-10 18:49:41 +02:00
|
|
|
from starlette.responses import HTMLResponse, StreamingResponse
|
2021-10-18 10:58:09 +01:00
|
|
|
|
2021-09-30 14:42:04 +01:00
|
|
|
from lnbits.core.models import User
|
2021-10-18 10:58:09 +01:00
|
|
|
from lnbits.decorators import check_user_exists
|
|
|
|
|
|
|
|
from . import withdraw_ext, withdraw_renderer
|
|
|
|
from .crud import chunks, get_withdraw_link
|
2021-09-30 14:42:04 +01:00
|
|
|
|
2021-08-21 02:05:45 +01:00
|
|
|
templates = Jinja2Templates(directory="templates")
|
2021-08-20 12:44:03 +01:00
|
|
|
|
2021-10-17 18:33:29 +01:00
|
|
|
|
2021-09-30 14:42:04 +01:00
|
|
|
@withdraw_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 withdraw_renderer().TemplateResponse(
|
|
|
|
"withdraw/index.html", {"request": request, "user": user.dict()}
|
|
|
|
)
|
2021-08-20 12:44:03 +01:00
|
|
|
|
|
|
|
|
2021-09-30 14:42:04 +01:00
|
|
|
@withdraw_ext.get("/{link_id}", response_class=HTMLResponse)
|
|
|
|
async def display(request: Request, link_id):
|
2021-08-25 15:44:18 +01:00
|
|
|
link = await get_withdraw_link(link_id, 0)
|
2021-10-08 17:10:25 +01:00
|
|
|
|
2021-08-25 15:44:18 +01:00
|
|
|
if not link:
|
2021-09-30 14:42:04 +01:00
|
|
|
raise HTTPException(
|
2021-10-17 18:33:29 +01:00
|
|
|
status_code=HTTPStatus.NOT_FOUND, detail="Withdraw link does not exist."
|
2021-09-30 14:42:04 +01:00
|
|
|
)
|
2021-10-17 18:33:29 +01:00
|
|
|
return withdraw_renderer().TemplateResponse(
|
|
|
|
"withdraw/display.html",
|
|
|
|
{
|
|
|
|
"request": request,
|
|
|
|
"link": link.dict(),
|
|
|
|
"lnurl": link.lnurl(req=request),
|
|
|
|
"unique": True,
|
|
|
|
},
|
|
|
|
)
|
2021-08-20 12:44:03 +01:00
|
|
|
|
|
|
|
|
2021-10-10 18:49:41 +02:00
|
|
|
@withdraw_ext.get("/img/{link_id}", response_class=StreamingResponse)
|
2021-10-05 09:19:21 +01:00
|
|
|
async def img(request: Request, link_id):
|
2021-08-25 15:44:18 +01:00
|
|
|
link = await get_withdraw_link(link_id, 0)
|
|
|
|
if not link:
|
2021-09-30 14:42:04 +01:00
|
|
|
raise HTTPException(
|
2021-10-17 18:33:29 +01:00
|
|
|
status_code=HTTPStatus.NOT_FOUND, detail="Withdraw link does not exist."
|
2021-09-30 14:42:04 +01:00
|
|
|
)
|
2021-10-08 17:10:25 +01:00
|
|
|
qr = pyqrcode.create(link.lnurl(request))
|
2021-08-20 12:44:03 +01:00
|
|
|
stream = BytesIO()
|
|
|
|
qr.svg(stream, scale=3)
|
2021-10-10 18:49:41 +02:00
|
|
|
stream.seek(0)
|
|
|
|
|
|
|
|
async def _generator(stream: BytesIO):
|
|
|
|
yield stream.getvalue()
|
|
|
|
|
|
|
|
return StreamingResponse(
|
|
|
|
_generator(stream),
|
|
|
|
headers={
|
2021-08-20 12:44:03 +01:00
|
|
|
"Content-Type": "image/svg+xml",
|
|
|
|
"Cache-Control": "no-cache, no-store, must-revalidate",
|
|
|
|
"Pragma": "no-cache",
|
|
|
|
"Expires": "0",
|
2021-10-17 18:33:29 +01:00
|
|
|
},
|
|
|
|
)
|
2021-08-20 12:44:03 +01:00
|
|
|
|
|
|
|
|
2021-09-30 14:42:04 +01:00
|
|
|
@withdraw_ext.get("/print/{link_id}", response_class=HTMLResponse)
|
|
|
|
async def print_qr(request: Request, link_id):
|
2021-08-25 15:44:18 +01:00
|
|
|
link = await get_withdraw_link(link_id)
|
|
|
|
if not link:
|
2021-09-30 14:42:04 +01:00
|
|
|
raise HTTPException(
|
2021-10-17 18:33:29 +01:00
|
|
|
status_code=HTTPStatus.NOT_FOUND, detail="Withdraw link does not exist."
|
2021-09-30 14:42:04 +01:00
|
|
|
)
|
|
|
|
# response.status_code = HTTPStatus.NOT_FOUND
|
|
|
|
# return "Withdraw link does not exist."
|
2021-10-08 17:10:25 +01:00
|
|
|
|
2021-08-20 12:44:03 +01:00
|
|
|
if link.uses == 0:
|
2021-10-08 17:10:25 +01:00
|
|
|
|
2021-10-17 18:33:29 +01:00
|
|
|
return withdraw_renderer().TemplateResponse(
|
|
|
|
"withdraw/print_qr.html",
|
2021-11-17 21:20:48 +00:00
|
|
|
{"request": request, "link": link.dict(), "unique": False},
|
2021-10-17 18:33:29 +01:00
|
|
|
)
|
2021-08-20 12:44:03 +01:00
|
|
|
links = []
|
|
|
|
count = 0
|
2021-10-08 17:10:25 +01:00
|
|
|
|
2021-08-20 12:44:03 +01:00
|
|
|
for x in link.usescsv.split(","):
|
2021-08-25 15:44:18 +01:00
|
|
|
linkk = await get_withdraw_link(link_id, count)
|
|
|
|
if not linkk:
|
2021-09-30 14:42:04 +01:00
|
|
|
raise HTTPException(
|
2021-10-17 18:33:29 +01:00
|
|
|
status_code=HTTPStatus.NOT_FOUND, detail="Withdraw link does not exist."
|
2021-09-30 14:42:04 +01:00
|
|
|
)
|
2021-10-08 17:10:25 +01:00
|
|
|
links.append(str(linkk.lnurl(request)))
|
2021-08-20 12:44:03 +01:00
|
|
|
count = count + 1
|
|
|
|
page_link = list(chunks(links, 2))
|
|
|
|
linked = list(chunks(page_link, 5))
|
2021-11-26 05:58:20 +00:00
|
|
|
|
2022-07-25 10:22:21 +01:00
|
|
|
if link.custom_url:
|
|
|
|
return withdraw_renderer().TemplateResponse(
|
|
|
|
"withdraw/print_qr_custom.html",
|
|
|
|
{
|
|
|
|
"request": request,
|
|
|
|
"link": page_link,
|
|
|
|
"unique": True,
|
|
|
|
"custom_url": link.custom_url,
|
|
|
|
"amt": link.max_withdrawable,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2021-10-17 18:33:29 +01:00
|
|
|
return withdraw_renderer().TemplateResponse(
|
|
|
|
"withdraw/print_qr.html", {"request": request, "link": linked, "unique": True}
|
|
|
|
)
|
2022-04-16 10:37:36 +01:00
|
|
|
|
2022-06-01 14:53:05 +02:00
|
|
|
|
2022-04-16 10:37:36 +01:00
|
|
|
@withdraw_ext.get("/csv/{link_id}", response_class=HTMLResponse)
|
2022-12-30 00:12:46 +01:00
|
|
|
async def csv(request: Request, link_id):
|
2022-04-16 10:37:36 +01:00
|
|
|
link = await get_withdraw_link(link_id)
|
|
|
|
if not link:
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=HTTPStatus.NOT_FOUND, detail="Withdraw link does not exist."
|
|
|
|
)
|
|
|
|
# response.status_code = HTTPStatus.NOT_FOUND
|
|
|
|
# return "Withdraw link does not exist."
|
|
|
|
|
|
|
|
if link.uses == 0:
|
|
|
|
|
|
|
|
return withdraw_renderer().TemplateResponse(
|
|
|
|
"withdraw/csv.html",
|
|
|
|
{"request": request, "link": link.dict(), "unique": False},
|
|
|
|
)
|
|
|
|
links = []
|
|
|
|
count = 0
|
|
|
|
|
|
|
|
for x in link.usescsv.split(","):
|
|
|
|
linkk = await get_withdraw_link(link_id, count)
|
|
|
|
if not linkk:
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=HTTPStatus.NOT_FOUND, detail="Withdraw link does not exist."
|
|
|
|
)
|
|
|
|
links.append(str(linkk.lnurl(request)))
|
|
|
|
count = count + 1
|
|
|
|
page_link = list(chunks(links, 2))
|
|
|
|
linked = list(chunks(page_link, 5))
|
|
|
|
|
|
|
|
return withdraw_renderer().TemplateResponse(
|
|
|
|
"withdraw/csv.html", {"request": request, "link": linked, "unique": True}
|
2022-06-01 14:53:05 +02:00
|
|
|
)
|