lnbits-legend/lnbits/extensions/withdraw/views.py

64 lines
1.9 KiB
Python
Raw Normal View History

from quart import g, abort, render_template
from http import HTTPStatus
2021-02-26 17:54:57 +01:00
import pyqrcode
2021-02-26 19:23:17 +01:00
from io import BytesIO
from lnbits.decorators import check_user_exists, validate_uuids
from . import withdraw_ext
from .crud import get_withdraw_link, chunks
@withdraw_ext.route("/")
@validate_uuids(["usr"], required=True)
@check_user_exists()
async def index():
return await render_template("withdraw/index.html", user=g.user)
@withdraw_ext.route("/<link_id>")
async def display(link_id):
link = await get_withdraw_link(link_id, 0) or abort(
HTTPStatus.NOT_FOUND, "Withdraw link does not exist."
)
return await render_template("withdraw/display.html", link=link, unique=True)
2021-02-26 17:54:57 +01:00
@withdraw_ext.route("/img/<link_id>")
async def img(link_id):
link = await get_withdraw_link(link_id, 0) or abort(
HTTPStatus.NOT_FOUND, "Withdraw link does not exist."
)
2021-02-26 17:54:57 +01:00
qr = pyqrcode.create(link.lnurl)
2021-02-26 19:23:17 +01:00
stream = BytesIO()
qr.svg(stream, scale=3)
2021-03-03 14:59:31 +01:00
return (
stream.getvalue(),
200,
{
"Content-Type": "image/svg+xml",
"Cache-Control": "no-cache, no-store, must-revalidate",
"Pragma": "no-cache",
"Expires": "0",
},
)
2021-02-26 17:54:57 +01:00
@withdraw_ext.route("/print/<link_id>")
async def print_qr(link_id):
link = await get_withdraw_link(link_id) or abort(
HTTPStatus.NOT_FOUND, "Withdraw link does not exist."
)
if link.uses == 0:
return await render_template("withdraw/print_qr.html", link=link, unique=False)
links = []
2020-08-19 09:49:52 +02:00
count = 0
for x in link.usescsv.split(","):
linkk = await get_withdraw_link(link_id, count) or abort(
HTTPStatus.NOT_FOUND, "Withdraw link does not exist."
)
2020-08-19 09:49:52 +02:00
links.append(str(linkk.lnurl))
count = count + 1
2020-08-10 20:44:55 +02:00
page_link = list(chunks(links, 2))
linked = list(chunks(page_link, 5))
return await render_template("withdraw/print_qr.html", link=linked, unique=True)