feat(api): add widget_name parameter to render_composite_adhoc function.

This commit is contained in:
shay
2026-08-22 16:33:24 -07:00
parent 319f37761c
commit 584306beb0
4 changed files with 155 additions and 8 deletions
+12 -4
View File
@@ -885,9 +885,14 @@ def _serve(username: str, widget_name: str) -> Response:
"X-Content-Type-Options": "nosniff",
}
# Ad-hoc render path. Only triggers for the composite — named widgets
# keep serving the cached per-widget SVG, which is what edge fetches.
overrides = sanitize_settings_query(request.args) if widget_name == "composite" else {}
# Ad-hoc render path. The composite accepts the full override set; named
# widgets accept `theme` only, because the compat shims that reach them
# (/api/top-langs?..., /api/streak/<u>) carry upstream params whose
# meaning differs from ours. A themed README URL migrated off upstream
# must render in the theme it asks for, not the owner's stored one.
overrides = sanitize_settings_query(request.args)
if widget_name != "composite":
overrides = {k: v for k, v in overrides.items() if k == "theme"}
if overrides and current_hash != "not_found":
# Honor the possibly-overridden theme for error placeholders.
override_theme = overrides.get("theme") if isinstance(overrides.get("theme"), str) else None
@@ -900,7 +905,7 @@ def _serve(username: str, widget_name: str) -> Response:
return resp
try:
from . import worker
svg = worker.render_composite_adhoc(username, overrides)
svg = worker.render_composite_adhoc(username, overrides, widget_name)
except Exception:
log.exception("adhoc render failed for %s", username)
svg = None
@@ -991,6 +996,9 @@ def _serve_profile_widget(username: str) -> Response:
theme_raw = settings_row["settings"].get("theme", "dark")
theme = theme_raw if isinstance(theme_raw, str) and theme_raw in THEMES else "dark"
# `?theme=` override, same allow-list as the composite path. This route
# already renders per request, so honoring it costs nothing extra.
theme = sanitize_settings_query(request.args).get("theme", theme)
acquired = _GENERATE_SEMAPHORE.acquire(timeout=config.GENERATE_SEMAPHORE_WAIT_S)
if not acquired:
+8 -3
View File
@@ -139,8 +139,9 @@ def process_one() -> bool:
return True
def render_composite_adhoc(username: str, override_settings: dict) -> str | None:
"""Render the composite SVG for `username` using stored settings merged
def render_composite_adhoc(username: str, override_settings: dict,
widget_name: str = "composite") -> str | None:
"""Render `widget_name` for `username` using stored settings merged
with the per-request `override_settings`. Does NOT persist anything.
Used by the public `GET /api/<u>?...` endpoint so unauthenticated visitors
@@ -158,11 +159,15 @@ def render_composite_adhoc(username: str, override_settings: dict) -> str | None
if not payload or payload.get("error") == "not_found":
return None
effective = {**settings_row["settings"], **(override_settings or {})}
if widget_name != "composite":
# Only the requested widget is returned, so don't pay to render the
# rest: generate_widgets_from_github skips anything not enabled.
effective["enabled"] = [widget_name]
t0 = time.monotonic()
widgets = _render_widgets(username, payload, effective)
analytics.record_render(username, "composite",
int((time.monotonic() - t0) * 1000))
return widgets.get("composite")
return widgets.get(widget_name)
def render_widgets_now(username: str) -> dict[str, str]: