Add the reading UI: stream, article detail, htmx actions

Implements the main reader surface: a filterable stream (Unread/All/
Starred/Saved) built on the lazy UserArticleState query pattern, an
article detail view that marks articles read on open, and htmx-backed
read/star/save-for-later toggles that update in place without a full
page reload. Rule-hidden articles are excluded from every filter.

Vendors htmx locally rather than loading it from a CDN, and adds
mobile-first responsive CSS (verified at 375px width in-browser, plus
dark mode via prefers-color-scheme).

Verified live against the real Hacker News feed subscribed in the
previous milestone: ingestion, filter switching, read/star/save
toggles, and article detail all confirmed working in the browser, with
two UI bugs fixed along the way (low-contrast message banners in dark
mode, horizontal overflow from long URLs in article content).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
ksmithandClaude Sonnet 5 committed 2026-10-30 16:08:44 -06:00
1 parent d200ab80f7
commit 69e3520e3b
11 files changed
+478 -9

No files matched your search

+128 -1
View File
@@ -1 +1,128 @@
# Create your tests here.
import pytest
from django.contrib.auth import get_user_model
from django.urls import reverse
from apps.articles.models import Article, UserArticleState
from apps.feeds.models import Feed, Subscription
User = get_user_model()
@pytest.fixture
def alice(db):
return User.objects.create_user("alice", password="pw-alice-123")
@pytest.fixture
def bob(db):
return User.objects.create_user("bob", password="pw-bob-123")
@pytest.fixture
def alices_article(alice):
feed = Feed.objects.create(feed_url="https://example.com/feed.xml", title="Example")
Subscription.objects.create(user=alice, feed=feed)
return Article.objects.create(feed=feed, guid="guid-1", title="Hello world")
@pytest.mark.django_db
def test_stream_requires_login(client):
response = client.get(reverse("reader:stream"))
assert response.status_code == 302
@pytest.mark.django_db
def test_stream_shows_only_own_subscriptions(client, alice, bob, alices_article):
client.login(username="bob", password="pw-bob-123")
response = client.get(reverse("reader:stream"))
assert alices_article.title not in response.content.decode()
client.login(username="alice", password="pw-alice-123")
response = client.get(reverse("reader:stream"))
assert alices_article.title in response.content.decode()
@pytest.mark.django_db
def test_unread_filter_excludes_read_articles(client, alice, alices_article):
UserArticleState.objects.create(user=alice, article=alices_article, is_read=True)
client.login(username="alice", password="pw-alice-123")
response = client.get(reverse("reader:stream"), {"filter": "unread"})
assert alices_article.title not in response.content.decode()
response = client.get(reverse("reader:stream"), {"filter": "all"})
assert alices_article.title in response.content.decode()
@pytest.mark.django_db
def test_hidden_articles_are_excluded_from_every_filter(client, alice, alices_article):
UserArticleState.objects.create(user=alice, article=alices_article, is_hidden=True)
client.login(username="alice", password="pw-alice-123")
for active_filter in ["unread", "all"]:
response = client.get(reverse("reader:stream"), {"filter": active_filter})
assert alices_article.title not in response.content.decode()
@pytest.mark.django_db
def test_viewing_article_detail_marks_it_read(client, alice, alices_article):
client.login(username="alice", password="pw-alice-123")
assert not UserArticleState.objects.filter(user=alice, article=alices_article).exists()
response = client.get(reverse("reader:article_detail", args=[alices_article.pk]))
assert response.status_code == 200
state = UserArticleState.objects.get(user=alice, article=alices_article)
assert state.is_read is True
assert state.read_at is not None
@pytest.mark.django_db
def test_user_cannot_view_another_users_article(client, alice, bob, alices_article):
client.login(username="bob", password="pw-bob-123")
response = client.get(reverse("reader:article_detail", args=[alices_article.pk]))
assert response.status_code == 404
@pytest.mark.django_db
def test_toggle_star_flips_state_and_persists(client, alice, alices_article):
client.login(username="alice", password="pw-alice-123")
url = reverse("reader:toggle_star", args=[alices_article.pk])
response = client.post(url)
assert response.status_code == 200
state = UserArticleState.objects.get(user=alice, article=alices_article)
assert state.is_starred is True
assert state.starred_at is not None
client.post(url)
state.refresh_from_db()
assert state.is_starred is False
assert state.starred_at is None
@pytest.mark.django_db
def test_toggle_read_requires_post(client, alice, alices_article):
client.login(username="alice", password="pw-alice-123")
response = client.get(reverse("reader:toggle_read", args=[alices_article.pk]))
assert response.status_code == 405
@pytest.mark.django_db
def test_user_cannot_toggle_state_on_another_users_article(client, alice, bob, alices_article):
client.login(username="bob", password="pw-bob-123")
response = client.post(reverse("reader:toggle_star", args=[alices_article.pk]))
assert response.status_code == 404
assert not UserArticleState.objects.filter(article=alices_article).exists()
@pytest.mark.django_db
def test_saved_filter_only_shows_saved_articles(client, alice, alices_article):
client.login(username="alice", password="pw-alice-123")
client.post(reverse("reader:toggle_saved", args=[alices_article.pk]))
response = client.get(reverse("reader:stream"), {"filter": "saved"})
assert alices_article.title in response.content.decode()
response = client.get(reverse("reader:stream"), {"filter": "starred"})
assert alices_article.title not in response.content.decode()
+4
View File
@@ -6,4 +6,8 @@ app_name = "reader"
urlpatterns = [
path("", views.stream, name="stream"),
path("articles/<int:article_id>/", views.article_detail, name="article_detail"),
path("articles/<int:article_id>/toggle-read/", views.toggle_read, name="toggle_read"),
path("articles/<int:article_id>/toggle-star/", views.toggle_star, name="toggle_star"),
path("articles/<int:article_id>/toggle-saved/", views.toggle_saved, name="toggle_saved"),
]
+137 -2
View File
@@ -1,7 +1,142 @@
from django.contrib.auth.decorators import login_required
from django.shortcuts import render
from django.db.models import Prefetch
from django.http import Http404
from django.shortcuts import get_object_or_404, render
from django.utils import timezone
from django.views.decorators.http import require_POST
from apps.articles.models import Article, UserArticleState
FILTER_CHOICES = [
("unread", "Unread"),
("all", "All"),
("starred", "Starred"),
("saved", "Saved"),
]
VALID_FILTERS = {key for key, _ in FILTER_CHOICES}
PAGE_SIZE = 30
def _base_queryset(user):
"""Articles from the user's subscriptions, with rule-hidden articles
excluded regardless of which filter tab is active."""
return (
Article.objects.filter(feed__subscriptions__user=user)
.exclude(user_states__user=user, user_states__is_hidden=True)
.select_related("feed")
)
def _with_user_state_annotations(queryset, user):
queryset = queryset.prefetch_related(
Prefetch(
"user_states",
queryset=UserArticleState.objects.filter(user=user),
to_attr="my_states",
)
)
articles = list(queryset)
for article in articles:
state = article.my_states[0] if article.my_states else None
article.is_read = state.is_read if state else False
article.is_starred = state.is_starred if state else False
article.is_saved_for_later = state.is_saved_for_later if state else False
return articles
def _get_or_create_state(user, article):
state, _ = UserArticleState.objects.get_or_create(user=user, article=article)
return state
@login_required
def stream(request):
return render(request, "reader/stream.html")
active_filter = request.GET.get("filter", "unread")
if active_filter not in VALID_FILTERS:
active_filter = "unread"
queryset = _base_queryset(request.user)
if active_filter == "unread":
queryset = queryset.exclude(user_states__user=request.user, user_states__is_read=True)
elif active_filter == "starred":
queryset = queryset.filter(user_states__user=request.user, user_states__is_starred=True)
elif active_filter == "saved":
queryset = queryset.filter(
user_states__user=request.user, user_states__is_saved_for_later=True
)
try:
page = max(int(request.GET.get("page", 1)), 1)
except ValueError:
page = 1
start, end = (page - 1) * PAGE_SIZE, page * PAGE_SIZE
total_count = queryset.count()
articles = _with_user_state_annotations(queryset[start:end], request.user)
context = {
"articles": articles,
"active_filter": active_filter,
"filters": FILTER_CHOICES,
"page": page,
"has_next_page": end < total_count,
"has_previous_page": page > 1,
}
return render(request, "reader/stream.html", context)
@login_required
def article_detail(request, article_id):
article = get_object_or_404(_base_queryset(request.user), pk=article_id)
state = _get_or_create_state(request.user, article)
if not state.is_read:
state.is_read = True
state.read_at = timezone.now()
state.save(update_fields=["is_read", "read_at"])
article.is_read = state.is_read
article.is_starred = state.is_starred
article.is_saved_for_later = state.is_saved_for_later
return render(request, "reader/article_detail.html", {"article": article})
@login_required
@require_POST
def toggle_read(request, article_id):
return _toggle(request, article_id, "is_read", "read_at")
@login_required
@require_POST
def toggle_star(request, article_id):
return _toggle(request, article_id, "is_starred", "starred_at")
@login_required
@require_POST
def toggle_saved(request, article_id):
return _toggle(request, article_id, "is_saved_for_later", None)
def _toggle(request, article_id, flag_field, timestamp_field):
try:
article = _base_queryset(request.user).get(pk=article_id)
except Article.DoesNotExist:
raise Http404
state = _get_or_create_state(request.user, article)
new_value = not getattr(state, flag_field)
setattr(state, flag_field, new_value)
update_fields = [flag_field]
if timestamp_field:
setattr(state, timestamp_field, timezone.now() if new_value else None)
update_fields.append(timestamp_field)
state.save(update_fields=update_fields)
article.is_read = state.is_read
article.is_starred = state.is_starred
article.is_saved_for_later = state.is_saved_for_later
return render(request, "reader/_article_actions.html", {"article": article})