Private
Public Access
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>
143 lines
4.5 KiB
Python
143 lines
4.5 KiB
Python
from django.contrib.auth.decorators import login_required
|
|
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):
|
|
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})
|