create infinite
This commit is contained in:
parent
54b3c4bc9b
commit
7bff037b78
14 changed files with 419 additions and 4 deletions
42
htmx_patterns/infinite/router.py
Normal file
42
htmx_patterns/infinite/router.py
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
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("/persons")
|
||||
async def get_persons(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,
|
||||
},
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue