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

74 lines
2.2 KiB
Python
Raw Normal View History

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
2021-08-21 01:55:07 +01:00
from fastapi import FastAPI, Request
from fastapi.templating import Jinja2Templates
2021-08-21 01:55:07 +01:00
templates = Jinja2Templates(directory="templates")
2021-08-21 00:34:48 +01:00
@offlineshop_ext.get("/")
@validate_uuids(["usr"], required=True)
@check_user_exists()
2021-08-21 01:55:07 +01:00
async def index(request: Request):
return await templates.TemplateResponse("offlineshop/index.html", {"request": request,"user":g.user})
2021-08-21 00:34:48 +01:00
@offlineshop_ext.get("/print")
2021-08-21 01:55:07 +01:00
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}",
}
)
2021-08-21 01:55:07 +01:00
return await templates.TemplateResponse("offlineshop/print.html", {"request": request,"items":items})
2021-08-21 00:34:48 +01:00
@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
)