Compare commits

...
Author SHA1 Message Date
ksmithandClaude Sonnet 5 fd68400822 Render article/feed times in the viewer's local timezone, client-side
Everything is stored and server-rendered in UTC (config.settings.base's
TIME_ZONE), so article times, the article detail header, and the
dashboard's "last fetched" column were always showing UTC regardless of
where the viewer actually is.

Article/feed timestamps now render as <time datetime="..."> elements
carrying the UTC instant plus a UTC-formatted fallback as their initial
text; static/js/localize-times.js reformats them using the browser's
own timezone via toLocaleString(), both on page load and after every
htmx swap (pagination, the inline article expand, the dashboard's retry
action all inject new <time> elements without a full page load).

No server-side change needed — this doesn't touch the (currently
unused) per-user timezone field, and works correctly even if the
viewer travels or changes their system timezone, with no settings page
required. Verified live in the browser against a UTC-stored timestamp
rendering correctly shifted to the sandbox's local zone.

Known related issue, out of scope here: the stream's "Received
today/yesterday" date-group headers (apps.reader.views._group_by_date)
are still computed server-side via timezone.localdate(), which is also
effectively UTC — an article just before/after UTC midnight could land
in a date group that doesn't match its now-correctly-localized
time-of-day. Flagged for the user rather than fixed, since correcting
it properly would mean moving date-grouping to the client too.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-31 13:31:07 -06:00
ksmithandClaude Sonnet 5 90837b339c Make the Site settings admin page go straight to the edit form
It's a true singleton, not a collection, so the normal changelist (an
"Add" link, a list with 0 or 1 rows, an extra click to get to the one
row) was just friction — and visually inconsistent with every other
app's admin section, which do show an "Add" link. The changelist view
now redirects straight to the single row's change form (creating it
first if this is a fresh install), and "Add" is never offered at all.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-31 13:23:47 -06:00
7 changed files with 88 additions and 15 deletions

No files matched your search

+15 -5
View File
@@ -1,18 +1,28 @@
from django.contrib import admin
from django.shortcuts import redirect
from django.urls import reverse
from .models import SiteSettings
@admin.register(SiteSettings)
class SiteSettingsAdmin(admin.ModelAdmin):
list_display = ["fetch_interval_minutes"]
"""A true singleton, not a collection — browsing a changelist to pick
the one row and click into it is an extra, pointless step, so
"Core > Site settings" in the sidebar goes straight to the (only)
change form instead, creating the row first if this is a fresh
install. No changelist/add/delete views exist at all here.
"""
fields = ["fetch_interval_minutes"]
def has_add_permission(self, request):
# Singleton — only offer "Add" when the row doesn't exist yet
# (a fresh install before the first poll_due_feeds run or admin
# visit has created it via SiteSettings.load()).
return not SiteSettings.objects.exists()
return False
def has_delete_permission(self, request, obj=None):
return False
def changelist_view(self, request, extra_context=None):
settings_row = SiteSettings.load()
url = reverse("admin:core_sitesettings_change", args=[settings_row.pk])
return redirect(url)
+17 -7
View File
@@ -51,7 +51,21 @@ def test_poll_due_feeds_uses_the_site_wide_interval_for_every_feed():
@pytest.mark.django_db
def test_admin_offers_add_only_once_and_never_offers_delete(client):
def test_admin_changelist_redirects_straight_to_the_change_form(client):
assert not SiteSettings.objects.exists()
User.objects.create_superuser("admin", password="pw-admin-123")
client.login(username="admin", password="pw-admin-123")
changelist_url = reverse("admin:core_sitesettings_changelist")
response = client.get(changelist_url)
settings_row = SiteSettings.objects.get() # created by the redirect itself
assert response.status_code == 302
assert response.url == reverse("admin:core_sitesettings_change", args=[settings_row.pk])
@pytest.mark.django_db
def test_admin_never_offers_add_or_delete(client):
# ModelAdmin.has_add_permission/has_delete_permission returning False
# makes Django raise PermissionDenied rather than build a 403 response
# directly — the test client's default behavior re-raises that for
@@ -61,14 +75,10 @@ def test_admin_offers_add_only_once_and_never_offers_delete(client):
User.objects.create_superuser("admin", password="pw-admin-123")
client.login(username="admin", password="pw-admin-123")
add_url = reverse("admin:core_sitesettings_add")
response = client.get(add_url)
assert response.status_code == 200 # no row yet — "Add" is offered
response = client.get(reverse("admin:core_sitesettings_add"))
assert response.status_code == 403
settings_row = SiteSettings.load()
response = client.get(add_url)
assert response.status_code == 403 # a row exists now — add is refused
delete_url = reverse("admin:core_sitesettings_delete", args=[settings_row.pk])
response = client.get(delete_url)
assert response.status_code == 403
+41
View File
@@ -0,0 +1,41 @@
(function () {
// Everything is stored and server-rendered in UTC (config.settings.base
// TIME_ZONE) — these <time datetime="..."> elements carry that UTC
// instant plus a UTC-formatted fallback as their initial text, and this
// reformats them using the browser's own timezone, client-side, so the
// server never needs to know (or guess) where the viewer actually is.
var SHORT_OPTIONS = { hour: "numeric", minute: "2-digit" };
var LONG_OPTIONS = {
year: "numeric",
month: "short",
day: "numeric",
hour: "2-digit",
minute: "2-digit",
};
function localizeTimesWithin(root) {
if (!root || typeof root.querySelectorAll !== "function") return;
root.querySelectorAll("time[data-local-format]").forEach(function (el) {
var iso = el.getAttribute("datetime");
if (!iso) return;
var date = new Date(iso);
if (isNaN(date.getTime())) return;
var options = el.dataset.localFormat === "short" ? SHORT_OPTIONS : LONG_OPTIONS;
try {
el.textContent = date.toLocaleString(undefined, options);
} catch (e) {
/* Intl unavailable or threw — leave the server-rendered UTC text as-is */
}
});
}
localizeTimesWithin(document);
// Article rows/content are frequently swapped in by htmx (pagination,
// the inline expand-article view, the dashboard's retry action) without
// a full page load, so every swap needs the same pass over just the
// new content.
document.body.addEventListener("htmx:afterSwap", function (event) {
localizeTimesWithin(event.target);
});
})();
+1
View File
@@ -53,6 +53,7 @@
{% endif %}
<script src="{% static 'js/stream.js' %}" defer></script>
<script src="{% static 'js/localize-times.js' %}" defer></script>
{% if user.is_authenticated %}
<script src="{% static 'js/theme.js' %}" defer></script>
<script src="{% static 'js/sidebar-state.js' %}" defer></script>
+7 -1
View File
@@ -31,7 +31,13 @@
<td>{{ feed.subscriber_count }}</td>
<td>{{ feed.get_last_fetch_status_display }}</td>
<td>{{ feed.fetch_error_count }}</td>
<td>{{ feed.last_fetched_at|default:"never" }}</td>
<td>
{% if feed.last_fetched_at %}
<time datetime="{{ feed.last_fetched_at|date:"c" }}" data-local-format="long">{{ feed.last_fetched_at|date:"M j, Y H:i" }}</time>
{% else %}
never
{% endif %}
</td>
<td>{{ feed.last_error_message|default:"—" }}</td>
<td>
<form method="post" action="{% url 'core:retry_feed' feed.id %}">
+4 -1
View File
@@ -2,7 +2,10 @@
<h2 class="article-content-title">{{ article.title|default:"(untitled)" }}</h2>
<p class="article-meta">
{% if article.feed.favicon_url %}<img src="{{ article.feed.favicon_url }}" alt="" class="feed-favicon">{% endif %}
{{ article.feed.title }} &middot; {{ article.published_at|date:"M j, Y H:i" }}
{{ article.feed.title }}
{% if article.published_at %}
&middot; <time datetime="{{ article.published_at|date:"c" }}" data-local-format="long">{{ article.published_at|date:"M j, Y H:i" }}</time>
{% endif %}
{% if article.author %} &middot; {{ article.author }}{% endif %}
{% if article.link %} &middot; <a href="{{ article.link }}" target="_blank" rel="noopener noreferrer">Original &#8599;</a>{% endif %}
</p>
+3 -1
View File
@@ -8,7 +8,9 @@
<a href="{% url 'reader:article_detail' article.pk %}" class="article-title"
data-inline-url="{% url 'reader:article_inline' article.pk %}"
onclick="return toggleArticle(event, {{ article.pk }})">{{ article.title|default:"(untitled)" }}</a>
<span class="article-time">{{ article.published_at|date:"g:i A" }}</span>
{% if article.published_at %}
<time class="article-time" datetime="{{ article.published_at|date:"c" }}" data-local-format="short">{{ article.published_at|date:"g:i A" }}</time>
{% endif %}
</div>
<div id="article-body-{{ article.pk }}" class="article-inline-body" hidden></div>
</li>