mirror of
https://github.com/lnbits/lnbits-legend.git
synced 2025-02-24 22:58:46 +01:00
73 lines
2.2 KiB
Python
73 lines
2.2 KiB
Python
import time
|
|
from datetime import datetime
|
|
from quart import g, render_template, request
|
|
from http import HTTPStatus
|
|
|
|
from lnbits.decorators import check_user_exists, validate_uuids
|
|
from lnbits.core.models import Payment
|
|
from lnbits.core.crud import get_standalone_payment
|
|
|
|
from . import offlineshop_ext
|
|
from .crud import get_item, get_shop
|
|
from fastapi import FastAPI, Request
|
|
from fastapi.templating import Jinja2Templates
|
|
|
|
templates = Jinja2Templates(directory="templates")
|
|
|
|
@offlineshop_ext.get("/")
|
|
@validate_uuids(["usr"], required=True)
|
|
@check_user_exists()
|
|
async def index(request: Request):
|
|
return await templates.TemplateResponse("offlineshop/index.html", {"request": request,"user":g.user})
|
|
|
|
|
|
@offlineshop_ext.get("/print")
|
|
async def print_qr_codes(request: Request):
|
|
items = []
|
|
for item_id in request.args.get("items").split(","):
|
|
item = await get_item(item_id)
|
|
if item:
|
|
items.append(
|
|
{
|
|
"lnurl": item.lnurl,
|
|
"name": item.name,
|
|
"price": f"{item.price} {item.unit}",
|
|
}
|
|
)
|
|
|
|
return await templates.TemplateResponse("offlineshop/print.html", {"request": request,"items":items})
|
|
|
|
|
|
@offlineshop_ext.get("/confirmation")
|
|
async def confirmation_code():
|
|
style = "<style>* { font-size: 100px}</style>"
|
|
|
|
payment_hash = request.args.get("p")
|
|
payment: Payment = await get_standalone_payment(payment_hash)
|
|
if not payment:
|
|
return (
|
|
f"Couldn't find the payment {payment_hash}." + style,
|
|
HTTPStatus.NOT_FOUND,
|
|
)
|
|
if payment.pending:
|
|
return (
|
|
f"Payment {payment_hash} wasn't received yet. Please try again in a minute."
|
|
+ style,
|
|
HTTPStatus.PAYMENT_REQUIRED,
|
|
)
|
|
|
|
if payment.time + 60 * 15 < time.time():
|
|
return "too much time has passed." + style
|
|
|
|
item = await get_item(payment.extra.get("item"))
|
|
shop = await get_shop(item.shop)
|
|
|
|
return (
|
|
f"""
|
|
[{shop.get_code(payment_hash)}]<br>
|
|
{item.name}<br>
|
|
{item.price} {item.unit}<br>
|
|
{datetime.utcfromtimestamp(payment.time).strftime('%Y-%m-%d %H:%M:%S')}
|
|
"""
|
|
+ style
|
|
)
|