feat(#179 Phase 15): Scorecards — HTML-Template + PNG-Endpoint
Mockup im GWÖ-Stil mit: - Drucksachen-Header (Kicker + Datum) - Titel + antragstellende Fraktionen als Pills - Empfehlungs-Verdict - 420-Zeichen-Zusammenfassung - Big Score-Zahl (farbcodiert nach 8/5/3-Schwellen) - 5x5 Mini-Matrix mit korrekten 5 Klassen (rating-pp/-p/-0/-n/-nn) - Footer mit Brand + Drucksachen-ID Endpoints: - GET /v2/scorecard?drucksache=&bundesland=&format=og|square (HTML) - GET /api/assessment/scorecard.png?... (PNG via Playwright, 1200x630 für og, 1080x1080 für square) Pattern entlehnt von app/og_card.py (Playwright-Headless-Render). Refs: #179 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
c268d889fa
commit
1faf4e9220
100
app/main.py
100
app/main.py
@ -3363,6 +3363,106 @@ async def og_template(request: Request, drucksache: str = ""):
|
|||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/v2/scorecard")
|
||||||
|
async def scorecard_template(
|
||||||
|
request: Request, drucksache: str, bundesland: str = "NRW",
|
||||||
|
format: str = "og",
|
||||||
|
):
|
||||||
|
"""Internes Render-Template für Scorecards (#179).
|
||||||
|
|
||||||
|
`format=og` → 1200×630 (LinkedIn/Twitter-OG)
|
||||||
|
`format=square` → 1080×1080 (Instagram)
|
||||||
|
"""
|
||||||
|
from .config import settings as _settings
|
||||||
|
drucksache = validate_drucksache(drucksache)
|
||||||
|
row = await get_assessment(drucksache)
|
||||||
|
if not row:
|
||||||
|
raise HTTPException(status_code=404, detail="Antrag nicht gefunden")
|
||||||
|
|
||||||
|
from .models import Assessment
|
||||||
|
assessment = Assessment.model_validate(row)
|
||||||
|
matrix_lookup = {e.field: {"rating": e.rating} for e in assessment.gwoe_matrix}
|
||||||
|
|
||||||
|
fraktionen = list(row.get("fraktionen", []) or [])
|
||||||
|
if isinstance(fraktionen, str):
|
||||||
|
try:
|
||||||
|
import json as _json
|
||||||
|
fraktionen = _json.loads(fraktionen) or []
|
||||||
|
except Exception:
|
||||||
|
fraktionen = []
|
||||||
|
|
||||||
|
score = assessment.gwoe_score
|
||||||
|
if score >= 8: score_color = "#1a7f37"
|
||||||
|
elif score >= 5: score_color = "#bf6c10"
|
||||||
|
elif score >= 3: score_color = "#9a2a2a"
|
||||||
|
else: score_color = "#9a2a2a"
|
||||||
|
|
||||||
|
dimensions = {"og": (1200, 630), "square": (1080, 1080)}
|
||||||
|
width, height = dimensions.get(format, dimensions["og"])
|
||||||
|
|
||||||
|
return templates.TemplateResponse("v2/screens/scorecard.html", {
|
||||||
|
"request": request,
|
||||||
|
"assessment": assessment,
|
||||||
|
"bundesland": bundesland,
|
||||||
|
"matrix_lookup": matrix_lookup,
|
||||||
|
"fraktionen": fraktionen[:4],
|
||||||
|
"datum": (row.get("datum") or "")[:10],
|
||||||
|
"score_color": score_color,
|
||||||
|
"width": width,
|
||||||
|
"height": height,
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/api/assessment/scorecard.png")
|
||||||
|
async def api_scorecard_png(
|
||||||
|
drucksache: str, bundesland: str = "NRW", format: str = "og",
|
||||||
|
):
|
||||||
|
"""Liefert die Scorecard als PNG via Playwright-Render (#179).
|
||||||
|
|
||||||
|
`format=og` → 1200×630, `format=square` → 1080×1080.
|
||||||
|
"""
|
||||||
|
drucksache = validate_drucksache(drucksache)
|
||||||
|
row = await get_assessment(drucksache)
|
||||||
|
if not row:
|
||||||
|
raise HTTPException(status_code=404, detail="Antrag nicht gefunden")
|
||||||
|
|
||||||
|
if format not in ("og", "square"):
|
||||||
|
raise HTTPException(status_code=400, detail="format muss 'og' oder 'square' sein")
|
||||||
|
width, height = ((1200, 630) if format == "og" else (1080, 1080))
|
||||||
|
|
||||||
|
try:
|
||||||
|
from playwright.sync_api import sync_playwright
|
||||||
|
import urllib.parse as _up
|
||||||
|
url = (
|
||||||
|
f"http://127.0.0.1:{settings.port}/v2/scorecard"
|
||||||
|
f"?drucksache={_up.quote(drucksache, safe='')}"
|
||||||
|
f"&bundesland={_up.quote(bundesland)}"
|
||||||
|
f"&format={format}"
|
||||||
|
)
|
||||||
|
with sync_playwright() as pw:
|
||||||
|
browser = pw.chromium.launch(args=["--no-sandbox"])
|
||||||
|
page = browser.new_page(viewport={"width": width, "height": height})
|
||||||
|
page.goto(url, wait_until="networkidle", timeout=15000)
|
||||||
|
png = page.screenshot(
|
||||||
|
clip={"x": 0, "y": 0, "width": width, "height": height}, type="png",
|
||||||
|
)
|
||||||
|
browser.close()
|
||||||
|
except Exception as e:
|
||||||
|
logger.exception("Scorecard-Render fehlgeschlagen")
|
||||||
|
raise HTTPException(status_code=500, detail=f"Render-Fehler: {e}")
|
||||||
|
|
||||||
|
safe_name = drucksache.replace("/", "-")
|
||||||
|
return Response(
|
||||||
|
content=png, media_type="image/png",
|
||||||
|
headers={
|
||||||
|
"Content-Disposition": (
|
||||||
|
f'inline; filename="scorecard-{safe_name}-{format}.png"'
|
||||||
|
),
|
||||||
|
"Cache-Control": "public, max-age=600",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.get("/api/og/{drucksache_encoded}.png")
|
@app.get("/api/og/{drucksache_encoded}.png")
|
||||||
async def api_og_card(drucksache_encoded: str, request: Request):
|
async def api_og_card(drucksache_encoded: str, request: Request):
|
||||||
"""Liefert die Open-Graph-PNG-Karte für einen Antrag (#141).
|
"""Liefert die Open-Graph-PNG-Karte für einen Antrag (#141).
|
||||||
|
|||||||
191
app/templates/v2/screens/scorecard.html
Normal file
191
app/templates/v2/screens/scorecard.html
Normal file
@ -0,0 +1,191 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="de">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Scorecard — {{ assessment.title }}</title>
|
||||||
|
<style>
|
||||||
|
* { box-sizing: border-box; margin: 0; padding: 0; }
|
||||||
|
html, body { font-family: 'Source Sans Pro', 'Helvetica Neue', sans-serif; }
|
||||||
|
body {
|
||||||
|
width: {{ width }}px;
|
||||||
|
height: {{ height }}px;
|
||||||
|
background: linear-gradient(135deg, #f8faf2 0%, #e9ede0 100%);
|
||||||
|
color: #1f1f1f;
|
||||||
|
overflow: hidden;
|
||||||
|
position: relative;
|
||||||
|
}
|
||||||
|
.card {
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
padding: {{ '38px 48px' if width >= 1200 else '32px 36px' }};
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
justify-content: space-between;
|
||||||
|
position: relative;
|
||||||
|
}
|
||||||
|
.header {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
align-items: baseline;
|
||||||
|
border-bottom: 2px solid #009DA5;
|
||||||
|
padding-bottom: 10px;
|
||||||
|
}
|
||||||
|
.kicker {
|
||||||
|
font-family: 'Source Code Pro', monospace;
|
||||||
|
font-size: 12pt;
|
||||||
|
color: #009DA5;
|
||||||
|
text-transform: uppercase;
|
||||||
|
letter-spacing: 0.1em;
|
||||||
|
}
|
||||||
|
.meta {
|
||||||
|
font-family: 'Source Code Pro', monospace;
|
||||||
|
font-size: 11pt;
|
||||||
|
color: #555;
|
||||||
|
}
|
||||||
|
.body-grid {
|
||||||
|
flex: 1;
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: {{ '1.4fr 1fr' if width >= 1200 else '1fr' }};
|
||||||
|
gap: 28px;
|
||||||
|
margin-top: 22px;
|
||||||
|
}
|
||||||
|
.left-col h1 {
|
||||||
|
font-size: {{ '28pt' if width >= 1200 else '22pt' }};
|
||||||
|
line-height: 1.18;
|
||||||
|
margin-bottom: 14px;
|
||||||
|
color: #1f1f1f;
|
||||||
|
font-weight: 700;
|
||||||
|
}
|
||||||
|
.left-col .fraktionen {
|
||||||
|
font-family: 'Source Code Pro', monospace;
|
||||||
|
font-size: 11pt;
|
||||||
|
color: #555;
|
||||||
|
margin-bottom: 18px;
|
||||||
|
}
|
||||||
|
.left-col .fraktionen .pill {
|
||||||
|
display: inline-block;
|
||||||
|
padding: 2px 10px;
|
||||||
|
background: rgba(136, 158, 51, 0.15);
|
||||||
|
color: #44570a;
|
||||||
|
border-radius: 3px;
|
||||||
|
margin-right: 5px;
|
||||||
|
}
|
||||||
|
.left-col .verdict {
|
||||||
|
font-size: 14pt;
|
||||||
|
color: #1a7f37;
|
||||||
|
font-weight: 600;
|
||||||
|
margin-bottom: 12px;
|
||||||
|
}
|
||||||
|
.left-col .summary {
|
||||||
|
font-size: 12pt;
|
||||||
|
line-height: 1.55;
|
||||||
|
color: #333;
|
||||||
|
/* Clamp fuer Lang-Texte */
|
||||||
|
display: -webkit-box;
|
||||||
|
-webkit-line-clamp: {{ 6 if width >= 1200 else 4 }};
|
||||||
|
-webkit-box-orient: vertical;
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
.right-col {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: flex-start;
|
||||||
|
padding-top: 8px;
|
||||||
|
}
|
||||||
|
.score-big {
|
||||||
|
font-family: 'Source Code Pro', monospace;
|
||||||
|
font-size: {{ '88pt' if width >= 1200 else '72pt' }};
|
||||||
|
font-weight: 700;
|
||||||
|
line-height: 1;
|
||||||
|
color: {{ score_color }};
|
||||||
|
margin-bottom: 4px;
|
||||||
|
}
|
||||||
|
.score-label {
|
||||||
|
font-family: 'Source Code Pro', monospace;
|
||||||
|
font-size: 11pt;
|
||||||
|
text-transform: uppercase;
|
||||||
|
letter-spacing: 0.1em;
|
||||||
|
color: #555;
|
||||||
|
margin-bottom: 16px;
|
||||||
|
}
|
||||||
|
/* Mini-Matrix 5x5 */
|
||||||
|
.matrix {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: repeat(5, 1fr);
|
||||||
|
grid-template-rows: repeat(5, 1fr);
|
||||||
|
gap: 2px;
|
||||||
|
width: {{ '220px' if width >= 1200 else '160px' }};
|
||||||
|
aspect-ratio: 1 / 1;
|
||||||
|
}
|
||||||
|
.matrix .cell {
|
||||||
|
border-radius: 2px;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
font-family: 'Source Code Pro', monospace;
|
||||||
|
font-size: 10pt;
|
||||||
|
font-weight: 700;
|
||||||
|
}
|
||||||
|
.cell.r-pp { background: #889E33; color: #fff; }
|
||||||
|
.cell.r-p { background: #cddaa1; color: #44570a; }
|
||||||
|
.cell.r-0 { background: #f0f0f0; color: #888; }
|
||||||
|
.cell.r-n { background: #efc9c3; color: #931515; }
|
||||||
|
.cell.r-nn { background: #9A2A2A; color: #fff; }
|
||||||
|
.footer {
|
||||||
|
position: absolute;
|
||||||
|
bottom: {{ '16px' if width >= 1200 else '12px' }};
|
||||||
|
left: {{ '48px' if width >= 1200 else '36px' }};
|
||||||
|
right: {{ '48px' if width >= 1200 else '36px' }};
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
align-items: center;
|
||||||
|
font-family: 'Source Code Pro', monospace;
|
||||||
|
font-size: 9pt;
|
||||||
|
color: #888;
|
||||||
|
border-top: 1px solid rgba(0,0,0,0.1);
|
||||||
|
padding-top: 8px;
|
||||||
|
}
|
||||||
|
.footer .brand { color: #009DA5; font-weight: 700; letter-spacing: 0.06em; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="card">
|
||||||
|
<div class="header">
|
||||||
|
<div class="kicker">GWÖ-Bewertung · {{ bundesland }} · {{ assessment.drucksache }}</div>
|
||||||
|
<div class="meta">{{ datum }}</div>
|
||||||
|
</div>
|
||||||
|
<div class="body-grid">
|
||||||
|
<div class="left-col">
|
||||||
|
<h1>{{ assessment.title|truncate(100, end="…") }}</h1>
|
||||||
|
{% if fraktionen %}
|
||||||
|
<div class="fraktionen">
|
||||||
|
{% for f in fraktionen %}<span class="pill">{{ f }}</span>{% endfor %}
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
<div class="verdict">{{ assessment.empfehlung.value }}</div>
|
||||||
|
<div class="summary">{{ assessment.gwoe_begruendung|truncate(420, end="…") }}</div>
|
||||||
|
</div>
|
||||||
|
<div class="right-col">
|
||||||
|
<div class="score-big">{{ "%.1f"|format(assessment.gwoe_score) }}</div>
|
||||||
|
<div class="score-label">GWÖ-Score · 0–10</div>
|
||||||
|
<div class="matrix">
|
||||||
|
{% for r in ['A','B','C','D','E'] %}
|
||||||
|
{% for c in ['1','2','3','4','5'] %}
|
||||||
|
{% set cell = matrix_lookup.get(r ~ c, {}) %}
|
||||||
|
{% set rt = cell.get('rating', 0) %}
|
||||||
|
<div class="cell {% if rt >= 4 %}r-pp{% elif rt >= 1 %}r-p{% elif rt == 0 %}r-0{% elif rt <= -4 %}r-nn{% else %}r-n{% endif %}">
|
||||||
|
{% if rt >= 4 %}++{% elif rt >= 1 %}+{% elif rt == 0 %}·{% elif rt <= -4 %}−−{% else %}−{% endif %}
|
||||||
|
</div>
|
||||||
|
{% endfor %}
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="footer">
|
||||||
|
<span><span class="brand">gwoe.toppyr.de</span> · automatische Gemeinwohl-Bilanzierung</span>
|
||||||
|
<span>{{ assessment.drucksache }}</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Loading…
Reference in New Issue
Block a user