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

38 lines
1.1 KiB
Python
Raw Normal View History

2021-10-20 12:14:56 +02:00
from http import HTTPStatus
from fastapi import Request
from fastapi.param_functions import Query
from fastapi.params import Depends
2021-10-20 12:14:56 +02:00
from fastapi.templating import Jinja2Templates
from starlette.exceptions import HTTPException
from lnbits.core.models import User
from lnbits.decorators import check_user_exists
2021-10-20 12:14:56 +02:00
from . import tipjar_ext, tipjar_renderer
from .crud import get_tipjar
2021-10-20 12:14:56 +02:00
templates = Jinja2Templates(directory="templates")
@tipjar_ext.get("/")
async def index(request: Request, user: User = Depends(check_user_exists)):
return tipjar_renderer().TemplateResponse(
"tipjar/index.html", {"request": request, "user": user.dict()}
)
@tipjar_ext.get("/{tipjar_id}")
async def tip(request: Request, tipjar_id: int = Query(None)):
2021-10-20 12:14:56 +02:00
"""Return the donation form for the Tipjar corresponding to id"""
tipjar = await get_tipjar(tipjar_id)
2021-10-20 12:14:56 +02:00
if not tipjar:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="TipJar does not exist."
)
return tipjar_renderer().TemplateResponse(
"tipjar/display.html",
{"request": request, "donatee": tipjar.name, "tipjar": tipjar.id},
)