From beb0131b089293512c2dab2243b9b2ff314e2387 Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Wed, 18 Nov 2026 19:18:18 +0100 Subject: feat(core): show relative timestamps Add reusable UTC template filters backed by `humanize` so timestamps render as relative ages while preserving machine-readable `" in rendered diff --git a/uv.lock b/uv.lock index 2e550e3..91278ba 100644 --- a/uv.lock +++ b/uv.lock @@ -582,6 +582,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/f0/0f/310fb31e39e2d734ccaa2c0fb981ee41f7bd5056ce9bc29b2248bd569169/humanfriendly-10.0-py2.py3-none-any.whl", hash = "sha256:1697e1a8a8f550fd43c2865cd84542fc175a61dcb779b6fee18cf6b6ccba1477", size = 86794, upload-time = "2021-09-17T21:40:39.897Z" }, ] +[[package]] +name = "humanize" +version = "4.17.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/d0/a7/5391f6db94bf16289ca63ce2632eef2eba7b9991e701016ac66f04539d47/humanize-4.17.0.tar.gz", hash = "sha256:1d6a36c453dbfbb3f0b34601d2e0b4ce5d48d780305cd45714843f0068fde6ac" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/0c/74/bb970f876202aa0e8c4a1e9904f0b3482e73c752512962b7e957091d5acf/humanize-4.17.0-py3-none-any.whl", hash = "sha256:02d0899527760efe18011944f882df4970943b4cdff7b5a0ba7fed43a576ef98" }, +] + [[package]] name = "idna" version = "3.20" @@ -1295,6 +1304,7 @@ dependencies = [ { name = "flask-sqlalchemy" }, { name = "flask-wtf" }, { name = "html5lib" }, + { name = "humanize" }, { name = "httpcore" }, { name = "httpx" }, { name = "huey" }, @@ -1370,6 +1380,7 @@ requires-dist = [ { name = "flask-sqlalchemy", specifier = ">=3.1.1" }, { name = "flask-wtf", specifier = ">=1.3.0" }, { name = "html5lib", specifier = ">=1.1" }, + { name = "humanize", specifier = ">=4.17.0" }, { name = "httpcore", specifier = ">=1.0.9,<1.1" }, { name = "httpx", specifier = ">=0.28.1,<0.29" }, { name = "huey", specifier = ">=3.4.0" }, diff --git a/webmentions_ssg/__init__.py b/webmentions_ssg/__init__.py index 174250e..b4b9f72 100644 --- a/webmentions_ssg/__init__.py +++ b/webmentions_ssg/__init__.py @@ -23,6 +23,7 @@ from .config import ( ) from .extensions import bootstrap, csrf, db, huey, limiter, mail, migrate from .net import is_http_url +from .template_filters import relative_time, utc_isoformat, utc_timestamp from .version import APP_NAME as APP_NAME from .version import VERSION from .views import webmentions_bp @@ -140,6 +141,9 @@ def create_app(config_class: type[DefaultConfig] | None = None) -> Flask: return {"VERSION": VERSION} app.add_template_filter(is_http_url) + app.add_template_filter(relative_time) + app.add_template_filter(utc_isoformat) + app.add_template_filter(utc_timestamp) app.register_blueprint(webmentions_bp) diff --git a/webmentions_ssg/template_filters.py b/webmentions_ssg/template_filters.py new file mode 100644 index 0000000..e6cac2d --- /dev/null +++ b/webmentions_ssg/template_filters.py @@ -0,0 +1,31 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Helpers for displaying UTC timestamps in templates.""" + +from datetime import UTC, datetime + +import humanize + + +def as_utc(value: datetime) -> datetime: + """Normalize a timestamp to UTC, treating timezone-naive DB values as UTC.""" + if value.tzinfo is None: + return value.replace(tzinfo=UTC) + return value.astimezone(UTC) + + +def utc_isoformat(value: datetime) -> str: + """Return an unambiguous machine-readable timestamp for the time element.""" + return as_utc(value).isoformat() + + +def utc_timestamp(value: datetime) -> str: + """Return the complete UTC timestamp for the hover tooltip.""" + return as_utc(value).strftime("%Y-%m-%d %H:%M:%S UTC") + + +def relative_time(value: datetime) -> str: + """Render a timestamp as a human-readable age relative to the current UTC time.""" + return humanize.naturaltime(datetime.now(UTC) - as_utc(value)) diff --git a/webmentions_ssg/templates/dashboard.html b/webmentions_ssg/templates/dashboard.html index 70e2bf0..b020d46 100644 --- a/webmentions_ssg/templates/dashboard.html +++ b/webmentions_ssg/templates/dashboard.html @@ -118,8 +118,10 @@ SPDX-License-Identifier: BSD-3-Clause

Last completed source scan

{% if last_scan %} -
Recovery attempts: {{ event.recovery_attempts }}
diff --git a/webmentions_ssg/templates/sent.html b/webmentions_ssg/templates/sent.html index d949f01..d2a319d 100644 --- a/webmentions_ssg/templates/sent.html +++ b/webmentions_ssg/templates/sent.html @@ -58,13 +58,15 @@ SPDX-License-Identifier: BSD-3-Clause {{ source.sent_webmentions | length }} {{ source.revision }} -