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

79 lines
2.3 KiB
Python
Raw Normal View History

2021-10-18 10:58:09 +01:00
from http import HTTPStatus
from fastapi import Request
from fastapi.params import Depends
from fastapi.templating import Jinja2Templates
2021-10-06 19:43:57 +01:00
from starlette.exceptions import HTTPException
from starlette.responses import HTMLResponse
2021-10-18 10:58:09 +01:00
2021-10-06 19:43:57 +01:00
from lnbits.core.models import User
from lnbits.decorators import check_user_exists
2022-07-16 14:23:03 +02:00
from lnbits.settings import LNBITS_CUSTOM_LOGO, LNBITS_SITE_TITLE
2021-10-06 14:49:00 +01:00
2021-10-06 19:43:57 +01:00
from . import tpos_ext, tpos_renderer
2021-10-06 14:49:00 +01:00
from .crud import get_tpos
2021-10-06 19:43:57 +01:00
templates = Jinja2Templates(directory="templates")
2021-10-06 14:49:00 +01:00
2021-10-17 18:33:29 +01:00
2021-10-06 19:43:57 +01:00
@tpos_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 tpos_renderer().TemplateResponse(
"tpos/index.html", {"request": request, "user": user.dict()}
)
2021-10-06 14:49:00 +01:00
2021-10-06 19:43:57 +01:00
@tpos_ext.get("/{tpos_id}")
async def tpos(request: Request, tpos_id):
2021-10-06 14:49:00 +01:00
tpos = await get_tpos(tpos_id)
if not tpos:
2021-10-06 19:43:57 +01:00
raise HTTPException(
2021-10-17 18:33:29 +01:00
status_code=HTTPStatus.NOT_FOUND, detail="TPoS does not exist."
2021-10-06 19:43:57 +01:00
)
2021-10-06 14:49:00 +01:00
2021-10-17 18:33:29 +01:00
return tpos_renderer().TemplateResponse(
2022-07-05 15:08:57 -06:00
"tpos/tpos.html",
{
"request": request,
"tpos": tpos,
"web_manifest": f"/tpos/manifest/{tpos_id}.webmanifest",
},
2021-10-17 18:33:29 +01:00
)
2022-07-04 16:39:05 -06:00
2022-07-04 16:39:05 -06:00
@tpos_ext.get("/manifest/{tpos_id}.webmanifest")
async def manifest(tpos_id: str):
tpos = await get_tpos(tpos_id)
if not tpos:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="TPoS does not exist."
)
return {
"short_name": LNBITS_SITE_TITLE,
"name": tpos.name + " - " + LNBITS_SITE_TITLE,
2022-07-04 16:39:05 -06:00
"icons": [
{
"src": LNBITS_CUSTOM_LOGO
if LNBITS_CUSTOM_LOGO
else "https://cdn.jsdelivr.net/gh/lnbits/lnbits@0.3.0/docs/logos/lnbits.png",
2022-07-04 16:39:05 -06:00
"type": "image/png",
"sizes": "900x900",
}
],
"start_url": "/tpos/" + tpos_id,
"background_color": "#1F2234",
"description": "Bitcoin Lightning tPOS",
"display": "standalone",
"scope": "/tpos/" + tpos_id,
"theme_color": "#1F2234",
"shortcuts": [
{
"name": tpos.name + " - " + LNBITS_SITE_TITLE,
2022-07-04 16:39:05 -06:00
"short_name": tpos.name,
"description": tpos.name + " - " + LNBITS_SITE_TITLE,
2022-07-04 16:39:05 -06:00
"url": "/tpos/" + tpos_id,
}
],
}