43 lines
1.3 KiB
Python
43 lines
1.3 KiB
Python
# retoor <retoor@molodetz.nl>
|
|
from fastapi import APIRouter, Request
|
|
|
|
from molodetz import config
|
|
from molodetz.cache import TTLCache
|
|
from molodetz.database import count_media, published_posts
|
|
from molodetz.gallery import gallery_items
|
|
from molodetz.responses import respond
|
|
from molodetz.schemas.posts import HomeOut
|
|
from molodetz.seo import base_seo_context, organization_schema, website_schema
|
|
from molodetz.seo_meta_text import description_for
|
|
|
|
router = APIRouter()
|
|
_landing_cache = TTLCache(ttl=config.LANDING_TTL, max_size=4)
|
|
|
|
|
|
def _landing_blocks():
|
|
cached = _landing_cache.get("blocks")
|
|
if cached is not None:
|
|
return cached
|
|
posts, _ = published_posts(limit=8)
|
|
flyers = gallery_items("flyer")
|
|
blocks = {
|
|
"posts": posts,
|
|
"featured_flyer": flyers[0] if flyers else None,
|
|
"flyer_count": count_media("flyer"),
|
|
"meme_count": count_media("meme"),
|
|
}
|
|
_landing_cache.set("blocks", blocks)
|
|
return blocks
|
|
|
|
|
|
@router.get("/")
|
|
async def index(request: Request):
|
|
blocks = _landing_blocks()
|
|
seo = base_seo_context(
|
|
request,
|
|
config.APP_TITLE,
|
|
description_for("home"),
|
|
schemas=[website_schema(request), organization_schema(request)],
|
|
)
|
|
return respond(request, "home.html", {**blocks, "seo": seo}, model=HomeOut)
|