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

48 lines
1.4 KiB
Python
Raw Normal View History

2021-10-27 22:09:55 +01:00
from http import HTTPStatus
from fastapi import Depends, Request
2021-10-27 22:09:55 +01:00
from fastapi.templating import Jinja2Templates
from starlette.exceptions import HTTPException
from starlette.responses import HTMLResponse
from lnbits.core.models import User
from lnbits.decorators import check_user_exists
from . import subdomains_ext, subdomains_renderer
from .crud import get_domain
templates = Jinja2Templates(directory="templates")
2021-11-12 04:14:55 +00:00
2021-10-27 22:09:55 +01:00
@subdomains_ext.get("/", response_class=HTMLResponse)
async def index(
request: Request, user: User = Depends(check_user_exists) # type:ignore
):
2021-11-12 04:14:55 +00:00
return subdomains_renderer().TemplateResponse(
"subdomains/index.html", {"request": request, "user": user.dict()}
)
2021-10-27 22:09:55 +01:00
@subdomains_ext.get("/{domain_id}")
async def display(request: Request, domain_id):
domain = await get_domain(domain_id)
if not domain:
raise HTTPException(
2021-11-12 04:14:55 +00:00
status_code=HTTPStatus.NOT_FOUND, detail="Domain does not exist."
2021-10-27 22:09:55 +01:00
)
allowed_records = (
domain.allowed_record_types.replace('"', "").replace(" ", "").split(",")
)
2021-11-12 04:14:55 +00:00
2021-10-27 22:09:55 +01:00
return subdomains_renderer().TemplateResponse(
2021-11-12 04:14:55 +00:00
"subdomains/display.html",
{
2021-10-27 22:09:55 +01:00
"request": request,
"domain_id": domain.id,
"domain_domain": domain.domain,
"domain_desc": domain.description,
"domain_cost": domain.cost,
"domain_allowed_record_types": allowed_records,
2021-11-12 04:14:55 +00:00
},
2021-10-27 22:09:55 +01:00
)