htmx-patterns/htmx_patterns/infinite/router.py
2024-04-05 22:39:40 -05:00

42 lines
1.1 KiB
Python

import time
from fastapi import APIRouter
from fastapi.requests import Request
infinite_router = APIRouter(prefix="/infinite", tags=["Shots Methods"])
from htmx_patterns.config import get_config
from htmx_patterns.infinite.models import PersonFactory
config = get_config()
@infinite_router.get("/")
async def infinite(request: Request, page: int = 1, n: int = 10):
# simulated last page
if page == 5:
return config.templates.TemplateResponse(
"infinite/persons_partial.html", {"request": request, "persons": []}
)
persons = [PersonFactory.build() for _ in range(n)]
if request.state.prefers_partial:
time.sleep(1)
return config.templates.TemplateResponse(
"infinite/persons_partial.html",
{
"request": request,
"persons": persons,
"next_page": page + 1,
},
)
return config.templates.TemplateResponse(
"infinite/persons.html",
{
"request": request,
"persons": persons,
"next_page": page + 1,
},
)