Files
ad/molodetz/routers/home.py
T

47 lines
1.5 KiB
Python

# retoor <retoor@molodetz.nl>
from fastapi import APIRouter, Request
from molodetz import config
from molodetz.cache import TTLCache
from molodetz.constants import SITE_INTRO_SEED
from molodetz.database import count_media, get_setting, published_posts
from molodetz.gallery import gallery_items
from molodetz.rendering import render_content
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)],
)
intro = get_setting("site_intro", SITE_INTRO_SEED)
ctx = {**blocks, "intro": intro, "intro_html": render_content(intro), "seo": seo}
return respond(request, "home.html", ctx, model=HomeOut)