Private
Public Access
Rebuild the reader UI to match FreshRSS's two-column layout
The previous UI was plain unstyled lists. Rebuilt around what FreshRSS actually looks like (per reference screenshots): a persistent left sidebar (folders/feeds with unread counts, favourites, tags) and a single main column with a date-grouped article list, where clicking a title expands the full article inline in place rather than navigating to a separate page. - apps/core/context_processors.py: feeds the sidebar's folder/feed tree, tags, and starred count to every authenticated page in one shared query set, so base.html can render it without each view wiring it up. - apps/reader/views.py: date-grouped stream (itertools.groupby over the already-ordered queryset), ?feed=/?folder= scoping alongside the existing ?tag=, a bulk mark-all-read endpoint (bulk_create/update, not one UserArticleState per article), and article_inline — the htmx endpoint behind the inline expand, sharing its mark-read logic with the full article_detail view via _load_article_and_mark_read. - templates/reader/_article_inline.html returns the article content plus an out-of-band swap of _article_actions.html, so the row's read/star icons update immediately when an article is expanded and marked read. - static/js/stream.js: the accordion toggle (one article open at a time), calling htmx.ajax() directly rather than hx-* attributes so a second click on an already-open row can collapse it without re-fetching. - static/css/base.css: full FreshRSS-like dark theme (with a light variant under prefers-color-scheme) replacing the previous unstyled pass — sidebar, topbar search, row/icon states, inline-expand styling. Verified end to end against a running dev server: folder/feed/tag scoping, mark-all-read, the inline-expand mark-read side effect and its icon update, and — against config.settings.prod specifically — that no inline <script> crept back in anywhere (would silently break under the CSP added in the previous commit) and the CSP header is still present on every page. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4c20558525
commit
cfa64b5485
18 files changed
+860
-159
No files matched your search
@@ -1,4 +1,6 @@
|
||||
from .models import UserArticleState
|
||||
from django.db.models import Count
|
||||
|
||||
from .models import Article, UserArticleState
|
||||
|
||||
|
||||
def with_user_state(articles, user):
|
||||
@@ -26,3 +28,17 @@ def with_user_state(articles, user):
|
||||
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 unread_counts_by_feed(user):
|
||||
"""{feed_id: unread_count} for every feed the user subscribes to, in one
|
||||
query — used by the sidebar, so it must stay cheap regardless of how
|
||||
many feeds/articles exist."""
|
||||
rows = (
|
||||
Article.objects.filter(feed__subscriptions__user=user)
|
||||
.exclude(user_states__user=user, user_states__is_read=True)
|
||||
.exclude(user_states__user=user, user_states__is_hidden=True)
|
||||
.values("feed_id")
|
||||
.annotate(unread=Count("id"))
|
||||
)
|
||||
return {row["feed_id"]: row["unread"] for row in rows}
|
||||
@@ -0,0 +1,41 @@
|
||||
from apps.articles.models import Tag, UserArticleState
|
||||
from apps.articles.queries import unread_counts_by_feed
|
||||
from apps.feeds.models import Folder, Subscription
|
||||
|
||||
|
||||
def sidebar(request):
|
||||
"""Folder/feed tree, tags, and starred count for the persistent sidebar
|
||||
rendered by base.html on every authenticated page. Kept out of
|
||||
individual views so every page gets the same sidebar for free.
|
||||
"""
|
||||
user = getattr(request, "user", None)
|
||||
if user is None or not user.is_authenticated:
|
||||
return {}
|
||||
|
||||
unread_counts = unread_counts_by_feed(user)
|
||||
subscriptions = (
|
||||
Subscription.objects.filter(user=user)
|
||||
.select_related("feed", "folder")
|
||||
.order_by("order", "id")
|
||||
)
|
||||
|
||||
folders = list(Folder.objects.filter(user=user).order_by("order", "name"))
|
||||
by_folder = {folder.pk: [] for folder in folders}
|
||||
unfoldered = []
|
||||
for subscription in subscriptions:
|
||||
subscription.unread_count = unread_counts.get(subscription.feed_id, 0)
|
||||
if subscription.folder_id and subscription.folder_id in by_folder:
|
||||
by_folder[subscription.folder_id].append(subscription)
|
||||
else:
|
||||
unfoldered.append(subscription)
|
||||
|
||||
return {
|
||||
"sidebar_folders": [
|
||||
{"folder": folder, "subscriptions": by_folder[folder.pk]} for folder in folders
|
||||
],
|
||||
"sidebar_unfoldered": unfoldered,
|
||||
"sidebar_tags": Tag.objects.filter(user=user),
|
||||
"sidebar_starred_count": UserArticleState.objects.filter(
|
||||
user=user, is_starred=True
|
||||
).count(),
|
||||
}
|
||||
+4
-1
@@ -35,7 +35,10 @@ def test_dashboard_lists_erroring_feeds(client, django_user_model):
|
||||
assert response.status_code == 200
|
||||
assert b"Broken Feed" in response.content
|
||||
assert b"Connection timed out" in response.content
|
||||
assert b"OK Feed" not in response.content
|
||||
# "OK Feed" legitimately appears elsewhere on the page (the sidebar
|
||||
# lists every subscription), so check the dashboard's own error table
|
||||
# scope via context rather than raw page content.
|
||||
assert list(response.context["erroring_feeds"]) == [broken_feed]
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
|
||||
@@ -6,7 +6,13 @@ app_name = "reader"
|
||||
|
||||
urlpatterns = [
|
||||
path("", views.stream, name="stream"),
|
||||
path("mark-all-read/", views.mark_all_read, name="mark_all_read"),
|
||||
path("articles/<int:article_id>/", views.article_detail, name="article_detail"),
|
||||
path(
|
||||
"articles/<int:article_id>/inline/",
|
||||
views.article_inline,
|
||||
name="article_inline",
|
||||
),
|
||||
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"),
|
||||
|
||||
+125
-13
@@ -1,11 +1,15 @@
|
||||
from itertools import groupby
|
||||
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import Http404
|
||||
from django.shortcuts import get_object_or_404, render
|
||||
from django.shortcuts import get_object_or_404, redirect, render
|
||||
from django.urls import reverse
|
||||
from django.utils import timezone
|
||||
from django.views.decorators.http import require_POST
|
||||
|
||||
from apps.articles.models import Article, Tag, UserArticleState
|
||||
from apps.articles.queries import with_user_state
|
||||
from apps.feeds.models import Feed, Folder
|
||||
|
||||
FILTER_CHOICES = [
|
||||
("unread", "Unread"),
|
||||
@@ -32,6 +36,69 @@ def _get_or_create_state(user, article):
|
||||
return state
|
||||
|
||||
|
||||
def _apply_scope(request, queryset):
|
||||
"""Narrows an already-base-scoped Article queryset by the optional
|
||||
?tag=/?feed=/?folder= params the sidebar links use. feed_id alone is
|
||||
enough to scope by feed (the base queryset already restricts to the
|
||||
user's own subscriptions); folder_id must be combined with user in the
|
||||
same .filter() call — Feed is shared across users, so a separate call
|
||||
would match any user's subscription with that folder_id, not just this
|
||||
user's, leaking other users' folder contents in.
|
||||
"""
|
||||
active_tag = None
|
||||
tag_id = request.GET.get("tag")
|
||||
if tag_id:
|
||||
active_tag = Tag.objects.filter(user=request.user, pk=tag_id).first()
|
||||
if active_tag:
|
||||
queryset = queryset.filter(
|
||||
article_tags__user=request.user, article_tags__tag=active_tag
|
||||
)
|
||||
|
||||
active_feed = None
|
||||
feed_id = request.GET.get("feed")
|
||||
if feed_id:
|
||||
active_feed = Feed.objects.filter(pk=feed_id, subscriptions__user=request.user).first()
|
||||
if active_feed:
|
||||
queryset = queryset.filter(feed_id=active_feed.pk)
|
||||
|
||||
active_folder = None
|
||||
folder_id = request.GET.get("folder")
|
||||
if folder_id:
|
||||
active_folder = Folder.objects.filter(user=request.user, pk=folder_id).first()
|
||||
if active_folder:
|
||||
queryset = queryset.filter(
|
||||
feed__subscriptions__user=request.user,
|
||||
feed__subscriptions__folder_id=active_folder.pk,
|
||||
)
|
||||
|
||||
return queryset, active_tag, active_feed, active_folder
|
||||
|
||||
|
||||
def _date_label(day, today):
|
||||
if day is None:
|
||||
return "Undated"
|
||||
diff = (today - day).days
|
||||
if diff == 0:
|
||||
return f"Received today — {day.strftime('%-d %B %Y')}"
|
||||
if diff == 1:
|
||||
return f"Received yesterday — {day.strftime('%-d %B %Y')}"
|
||||
return f"Received {day.strftime('%-d %B %Y')}"
|
||||
|
||||
|
||||
def _group_by_date(articles):
|
||||
"""Articles are already ordered newest-first (Article.Meta.ordering),
|
||||
so same-day articles are contiguous and groupby doesn't need a sort."""
|
||||
today = timezone.localdate()
|
||||
|
||||
def day_of(article):
|
||||
return timezone.localtime(article.published_at).date() if article.published_at else None
|
||||
|
||||
return [
|
||||
{"label": _date_label(day, today), "articles": list(items)}
|
||||
for day, items in groupby(articles, key=day_of)
|
||||
]
|
||||
|
||||
|
||||
@login_required
|
||||
def stream(request):
|
||||
active_filter = request.GET.get("filter", "unread")
|
||||
@@ -48,14 +115,7 @@ def stream(request):
|
||||
user_states__user=request.user, user_states__is_saved_for_later=True
|
||||
)
|
||||
|
||||
active_tag = None
|
||||
tag_id = request.GET.get("tag")
|
||||
if tag_id:
|
||||
active_tag = Tag.objects.filter(user=request.user, pk=tag_id).first()
|
||||
if active_tag:
|
||||
queryset = queryset.filter(
|
||||
article_tags__user=request.user, article_tags__tag=active_tag
|
||||
)
|
||||
queryset, active_tag, active_feed, active_folder = _apply_scope(request, queryset)
|
||||
|
||||
try:
|
||||
page = max(int(request.GET.get("page", 1)), 1)
|
||||
@@ -67,10 +127,12 @@ def stream(request):
|
||||
articles = with_user_state(queryset[start:end], request.user)
|
||||
|
||||
context = {
|
||||
"articles": articles,
|
||||
"date_groups": _group_by_date(articles),
|
||||
"active_filter": active_filter,
|
||||
"filters": FILTER_CHOICES,
|
||||
"active_tag": active_tag,
|
||||
"active_feed": active_feed,
|
||||
"active_folder": active_folder,
|
||||
"user_tags": Tag.objects.filter(user=request.user),
|
||||
"page": page,
|
||||
"has_next_page": end < total_count,
|
||||
@@ -80,10 +142,45 @@ def stream(request):
|
||||
|
||||
|
||||
@login_required
|
||||
def article_detail(request, article_id):
|
||||
article = get_object_or_404(_base_queryset(request.user), pk=article_id)
|
||||
@require_POST
|
||||
def mark_all_read(request):
|
||||
"""Marks every unread article currently matching the stream's filter
|
||||
scope (tag included, "unread"/"all" filter excluded since it's a no-op
|
||||
choice here) as read — bulk, not one UserArticleState per article."""
|
||||
queryset = _base_queryset(request.user)
|
||||
queryset, _active_tag, _active_feed, _active_folder = _apply_scope(request, queryset)
|
||||
|
||||
state = _get_or_create_state(request.user, article)
|
||||
unread_articles = queryset.exclude(user_states__user=request.user, user_states__is_read=True)
|
||||
unread_ids = list(unread_articles.values_list("id", flat=True))
|
||||
|
||||
if unread_ids:
|
||||
now = timezone.now()
|
||||
existing_state_article_ids = set(
|
||||
UserArticleState.objects.filter(
|
||||
user=request.user, article_id__in=unread_ids
|
||||
).values_list("article_id", flat=True)
|
||||
)
|
||||
UserArticleState.objects.filter(
|
||||
user=request.user, article_id__in=existing_state_article_ids
|
||||
).update(is_read=True, read_at=now)
|
||||
|
||||
new_article_ids = [aid for aid in unread_ids if aid not in existing_state_article_ids]
|
||||
UserArticleState.objects.bulk_create(
|
||||
[
|
||||
UserArticleState(user=request.user, article_id=aid, is_read=True, read_at=now)
|
||||
for aid in new_article_ids
|
||||
]
|
||||
)
|
||||
|
||||
stream_url = reverse("reader:stream")
|
||||
query = request.GET.urlencode()
|
||||
return redirect(f"{stream_url}?{query}" if query else stream_url)
|
||||
|
||||
|
||||
def _load_article_and_mark_read(user, article_id):
|
||||
article = get_object_or_404(_base_queryset(user), pk=article_id)
|
||||
|
||||
state = _get_or_create_state(user, article)
|
||||
if not state.is_read:
|
||||
state.is_read = True
|
||||
state.read_at = timezone.now()
|
||||
@@ -92,10 +189,25 @@ def article_detail(request, article_id):
|
||||
article.is_read = state.is_read
|
||||
article.is_starred = state.is_starred
|
||||
article.is_saved_for_later = state.is_saved_for_later
|
||||
return article
|
||||
|
||||
|
||||
@login_required
|
||||
def article_detail(request, article_id):
|
||||
article = _load_article_and_mark_read(request.user, article_id)
|
||||
return render(request, "reader/article_detail.html", {"article": article})
|
||||
|
||||
|
||||
@login_required
|
||||
def article_inline(request, article_id):
|
||||
"""Fetched via htmx when a row in the stream is expanded in place —
|
||||
mirrors article_detail's mark-read side effect, but renders just the
|
||||
content fragment plus an out-of-band update of the row's own read/star
|
||||
icons (see templates/reader/_article_inline.html)."""
|
||||
article = _load_article_and_mark_read(request.user, article_id)
|
||||
return render(request, "reader/_article_inline.html", {"article": article})
|
||||
|
||||
|
||||
@login_required
|
||||
@require_POST
|
||||
def toggle_read(request, article_id):
|
||||
|
||||
@@ -54,4 +54,4 @@ def test_search_results_carry_user_state(client, alice, feed):
|
||||
response = client.get(reverse("search:search"), {"q": "Django"})
|
||||
content = response.content.decode()
|
||||
|
||||
assert 'class="icon-btn active"' in content
|
||||
assert 'class="icon-btn icon-btn-star active"' in content
|
||||
Reference in new issue
Block a user