fix(content): handle None values in template pattern rendering
DuckDB rows can have NULL columns (e.g. market_score, median_peak_rate). Replace None with 0 in render context so numeric Jinja2 filters like round() and int don't crash with "NoneType doesn't define __round__". Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -12,7 +12,7 @@ from pathlib import Path
|
||||
|
||||
import mistune
|
||||
import yaml
|
||||
from jinja2 import Environment
|
||||
from jinja2 import ChainableUndefined, Environment
|
||||
|
||||
from ..analytics import fetch_analytics
|
||||
from ..core import execute, fetch_one, slugify
|
||||
@@ -146,10 +146,12 @@ def _datetimeformat(value: str, fmt: str = "%Y-%m-%d") -> str:
|
||||
|
||||
def _render_pattern(pattern: str, context: dict) -> str:
|
||||
"""Render a Jinja2 pattern string with context variables."""
|
||||
env = Environment()
|
||||
env = Environment(undefined=ChainableUndefined)
|
||||
env.filters["slugify"] = slugify
|
||||
env.filters["datetimeformat"] = _datetimeformat
|
||||
return env.from_string(pattern).render(**context)
|
||||
# Replace None values with 0 so numeric filters (round, int) don't crash
|
||||
safe_context = {k: (v if v is not None else 0) for k, v in context.items()}
|
||||
return env.from_string(pattern).render(**safe_context)
|
||||
|
||||
|
||||
def _extract_faq_pairs(markdown: str) -> list[dict]:
|
||||
|
||||
Reference in New Issue
Block a user