Two unrelated features bundled because they ship together and share
migration 0028.
Strategic-log feedback (thumb up/down):
- New strategic_log_feedback table with UNIQUE(log_id, user_id) so each
user has one vote per log, flippable in place (up -> down -> clear).
UI shows aggregate counts only.
- app/services/log_feedback.py: set_vote, get_counts, sign/verify
feedback tokens (same itsdangerous pattern as auth.sign_pending,
30-day TTL for email links).
- POST /api/log/{id}/feedback: web vote, auth required, returns counts
+ the requesting user's own vote.
- GET /feedback?token=...&vote=...: email-link target, no auth, signed
token encodes (user, log, vote), renders feedback_thanks.html.
- partials/log.html: thumbs row below content, JS-driven swap via the
POST endpoint. Dashboard latest-log card and /log page both render
this partial via htmx, so the buttons appear in all three surfaces.
- digest emails: a "How was today's read?" row above the unsub footer,
with signed-token URLs against the latest StrategicLog at send time.
Plain-text fallback included.
Reviewer self-score (0-10):
- _SYSTEM_PROMPT asks for an integer score with anchors (10 exemplary,
5 borderline, 0 unfit). Verdict gains score: int | None.
- Deterministic-layer hits get score=0 (hard rule, no nuance);
error rows get None; LLM rows get the model's score clamped 0..10.
- ReviewerVerdict.score, StrategicLog.reviewer_score, and
IndicatorSummary.reviewer_score all new SMALLINT NULL columns.
- ai_log_job + indicator_summary_job persist verdict.score onto their
content rows when committing the row alongside content.
Tests:
- tests/test_strategic_log_feedback.py: vote, flip, clear, aggregate
across users, invalid vote, token round-trip + tamper + garbage +
'clear' not signable for email path.
- tests/test_output_review.py: score parsing, clamping (>10, <0),
missing/non-numeric -> None, deterministic-layer score=0.
Full suite: 427 passed (was 412), 5 skipped, no regressions.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
165 lines
6 KiB
Python
165 lines
6 KiB
Python
"""Daily/weekly digest email rendering.
|
|
|
|
Pure prose → HTML/text rendering. SMTP transport stays in
|
|
``email_service.send_email``; this module only assembles the message
|
|
body, subject, and a text-only fallback for clients without HTML
|
|
rendering.
|
|
|
|
Split from email_service.py during the Tier 2 cleanup pass — the
|
|
SMTP/OTP/welcome surface and the digest renderer changed at very
|
|
different cadences and made the file noisy to navigate.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import html as _html_lib
|
|
import re as _re
|
|
|
|
from app import branding
|
|
|
|
|
|
_DIGEST_HTML_TEMPLATE = """\
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<meta name="color-scheme" content="light dark">
|
|
<title>{brand} — {label}</title>
|
|
<style>
|
|
@media (prefers-color-scheme: dark) {{
|
|
body {{ background:{D_bg} !important; }}
|
|
.card {{ background:{D_surface} !important; border-color:{D_border} !important; }}
|
|
.h1, p, li {{ color:{D_text} !important; }}
|
|
.muted {{ color:{D_muted} !important; }}
|
|
a {{ color:{D_accent} !important; }}
|
|
}}
|
|
</style>
|
|
</head>
|
|
<body style="margin:0; padding:24px 12px; background:{L_bg}; font-family:{FONT_MONO}; color:{L_text};">
|
|
<table role="presentation" cellpadding="0" cellspacing="0" border="0" align="center" width="100%" style="max-width:520px; margin:0 auto; border-collapse:separate;">
|
|
<tr><td class="card" style="background:{L_surface}; border:1px solid {L_border}; padding:32px 28px;">
|
|
<div class="muted" style="font-size:11px; letter-spacing:0.32em; color:{L_muted}; text-transform:uppercase;">
|
|
▰ {brand_upper} · {label_upper}
|
|
</div>
|
|
<div style="height:20px; line-height:20px; font-size:0;"> </div>
|
|
<div class="content" style="font-size:14px; line-height:1.65; color:{L_text};">
|
|
{content_html}
|
|
</div>
|
|
<div style="height:24px; line-height:24px; font-size:0;"> </div>
|
|
<div style="border-top:1px solid {L_border};"></div>
|
|
{feedback_row}
|
|
<div style="height:14px; line-height:14px; font-size:0;"> </div>
|
|
<div class="muted" style="font-size:11px; color:{L_muted};">
|
|
<a href="{unsubscribe_url}" style="color:{L_accent};">Unsubscribe in one click</a>
|
|
· <a href="{settings_url}" style="color:{L_accent};">Manage preferences</a>
|
|
</div>
|
|
</td></tr>
|
|
</table>
|
|
</body>
|
|
</html>
|
|
"""
|
|
|
|
|
|
def _strip_html_to_text(html_body: str) -> str:
|
|
"""Best-effort HTML → plain text for the multipart fallback. We don't
|
|
need perfection — just readable prose for clients that won't render
|
|
HTML."""
|
|
text = _re.sub(r"(?i)<(/(p|h[1-6]|li|ul|ol)|br\s*/?)>", "\n", html_body)
|
|
text = _re.sub(r"<[^>]+>", "", text)
|
|
text = _html_lib.unescape(text)
|
|
text = _re.sub(r"\n{3,}", "\n\n", text)
|
|
return text.strip()
|
|
|
|
|
|
def _feedback_row_html(
|
|
feedback_up_url: str | None,
|
|
feedback_down_url: str | None,
|
|
light_accent: str,
|
|
light_muted: str,
|
|
) -> str:
|
|
"""Build the optional 'How was today's read?' row that sits between
|
|
the digest content and the unsubscribe footer. Empty string when no
|
|
feedback URLs were supplied (e.g. there's no latest log to vote on)."""
|
|
if not feedback_up_url or not feedback_down_url:
|
|
return ""
|
|
return (
|
|
'<div style="height:14px; line-height:14px; font-size:0;"> </div>'
|
|
f'<div class="muted" style="font-size:12px; color:{light_muted};">'
|
|
"How was today’s read? "
|
|
f'<a href="{feedback_up_url}" '
|
|
f'style="color:{light_accent}; text-decoration:none;">'
|
|
"👍 Helpful</a>"
|
|
" · "
|
|
f'<a href="{feedback_down_url}" '
|
|
f'style="color:{light_accent}; text-decoration:none;">'
|
|
"👎 Not useful</a>"
|
|
"</div>"
|
|
)
|
|
|
|
|
|
def render_digest_email(
|
|
*,
|
|
kind: str,
|
|
date_str: str,
|
|
content_html: str,
|
|
unsubscribe_url: str,
|
|
settings_url: str,
|
|
feedback_up_url: str | None = None,
|
|
feedback_down_url: str | None = None,
|
|
) -> tuple[str, str, str]:
|
|
"""Returns (subject, text_body, html_body) for a digest email.
|
|
|
|
`kind` is "daily" or "weekly". Anything else raises ValueError.
|
|
|
|
When ``feedback_up_url`` and ``feedback_down_url`` are both supplied,
|
|
a small thumb up/down row is rendered above the unsubscribe footer.
|
|
Both must be signed-token URLs pointing at /feedback (see
|
|
``app.services.log_feedback.sign_feedback_token``)."""
|
|
if kind == "daily":
|
|
label = "Daily"
|
|
subject = f"{branding.BRAND_NAME} · Daily — {date_str}"
|
|
elif kind == "weekly":
|
|
label = "Weekly recap"
|
|
subject = f"{branding.BRAND_NAME} · Weekly recap — {date_str}"
|
|
else:
|
|
raise ValueError(f"unknown digest kind: {kind!r}")
|
|
|
|
feedback_row = _feedback_row_html(
|
|
feedback_up_url, feedback_down_url,
|
|
light_accent=branding.LIGHT["accent"],
|
|
light_muted=branding.LIGHT["muted"],
|
|
)
|
|
|
|
html_body = _DIGEST_HTML_TEMPLATE.format(
|
|
brand=branding.BRAND_NAME,
|
|
brand_upper=branding.BRAND_NAME.upper(),
|
|
label=label,
|
|
label_upper=label.upper(),
|
|
FONT_MONO=branding.FONT_MONO,
|
|
content_html=content_html,
|
|
unsubscribe_url=unsubscribe_url,
|
|
settings_url=settings_url,
|
|
feedback_row=feedback_row,
|
|
**{f"L_{k.replace('-', '_')}": v for k, v in branding.LIGHT.items()},
|
|
**{f"D_{k.replace('-', '_')}": v for k, v in branding.DARK.items()},
|
|
)
|
|
|
|
text_lines = [
|
|
f"{branding.BRAND_NAME} — {label}",
|
|
date_str,
|
|
"",
|
|
_strip_html_to_text(content_html),
|
|
"",
|
|
]
|
|
if feedback_up_url and feedback_down_url:
|
|
text_lines.extend([
|
|
f"Was this read useful? Helpful: {feedback_up_url}",
|
|
f" Not useful: {feedback_down_url}",
|
|
"",
|
|
])
|
|
text_lines.extend([
|
|
f"Unsubscribe: {unsubscribe_url}",
|
|
f"Manage preferences: {settings_url}",
|
|
])
|
|
text_body = "\n".join(text_lines)
|
|
return subject, text_body, html_body
|