feat(affiliate): use build_affiliate_url() in /go/<slug> redirect

Program-based products now get URLs assembled from the template at
redirect time. Changing a program's tracking_tag propagates instantly
to all its products without rebuilding. Legacy products (no program_id)
still use their baked affiliate_url via fallback.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Deeman
2026-02-28 22:27:57 +01:00
parent 8dbbd0df05
commit 6076a0b30f

View File

@@ -291,7 +291,7 @@ def create_app() -> Quart:
Uses 302 (not 301) so every hit is tracked — browsers don't cache 302s. Uses 302 (not 301) so every hit is tracked — browsers don't cache 302s.
Extracts article_slug and lang from Referer header best-effort. Extracts article_slug and lang from Referer header best-effort.
""" """
from .affiliate import get_product, log_click from .affiliate import build_affiliate_url, get_product, log_click
from .core import check_rate_limit from .core import check_rate_limit
# Extract lang from Referer path (e.g. /de/blog/... → "de"), default de # Extract lang from Referer path (e.g. /de/blog/... → "de"), default de
@@ -314,14 +314,17 @@ def create_app() -> Quart:
if not product: if not product:
abort(404) abort(404)
# Assemble URL from program template; falls back to baked affiliate_url
url = build_affiliate_url(product, product.get("_program"))
ip = request.remote_addr or "unknown" ip = request.remote_addr or "unknown"
allowed, _info = await check_rate_limit(f"aff:{ip}", limit=60, window=60) allowed, _info = await check_rate_limit(f"aff:{ip}", limit=60, window=60)
if not allowed: if not allowed:
# Still redirect even if rate-limited; just don't log the click # Still redirect even if rate-limited; just don't log the click
return redirect(product["affiliate_url"], 302) return redirect(url, 302)
await log_click(product["id"], ip, article_slug, referer or None) await log_click(product["id"], ip, article_slug, referer or None)
return redirect(product["affiliate_url"], 302) return redirect(url, 302)
# Legacy 301 redirects — bookmarked/cached URLs before lang prefixes existed # Legacy 301 redirects — bookmarked/cached URLs before lang prefixes existed
@app.route("/terms") @app.route("/terms")