feat(app): register timeago Jinja filter
Templates can now call {{ ts | timeago }} directly without route-side
precomputation.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
1926fe895c
commit
263a9a9f27
2 changed files with 19 additions and 0 deletions
|
|
@ -28,6 +28,7 @@ from l4d2web.services.job_worker import (
|
|||
start_state_poller,
|
||||
)
|
||||
from l4d2web.services.live_state_poller import start_live_state_poller
|
||||
from l4d2web.services.timeago import format_time_html
|
||||
|
||||
|
||||
def _in_flask_cli_context() -> bool:
|
||||
|
|
@ -58,6 +59,8 @@ def create_app(test_config: dict[str, object] | None = None) -> Flask:
|
|||
with app.app_context():
|
||||
init_db()
|
||||
|
||||
app.add_template_filter(format_time_html, "timeago")
|
||||
|
||||
@app.before_request
|
||||
def csrf_protect() -> Response | None:
|
||||
if "csrf_token" not in session:
|
||||
|
|
|
|||
|
|
@ -115,3 +115,19 @@ def test_format_time_html_normalises_naive_input_to_utc():
|
|||
out = str(format_time_html(then_naive, now=now))
|
||||
assert 'datetime="2026-05-16T14:32:11+00:00"' in out
|
||||
assert 'title="2026-05-16 14:32:11 UTC"' in out
|
||||
|
||||
|
||||
def test_timeago_filter_registered_on_app():
|
||||
from flask import render_template_string
|
||||
|
||||
from l4d2web.app import create_app
|
||||
|
||||
app = create_app({"TESTING": True, "SECRET_KEY": "test"})
|
||||
with app.app_context():
|
||||
rendered = render_template_string(
|
||||
"{{ ts | timeago }}",
|
||||
ts=datetime.now(UTC) - timedelta(minutes=3),
|
||||
)
|
||||
assert "<time " in rendered
|
||||
assert "<time" not in rendered
|
||||
assert "3 minutes ago" in rendered
|
||||
|
|
|
|||
Loading…
Reference in a new issue