Make Starred and Saved-for-later pre-configured system tags

UserArticleState.is_starred/is_saved_for_later/starred_at are gone —
starring and saving now add/remove an ArticleTag against a per-user
"Starred" / "Saved for later" Tag, created lazily on first use and marked
Tag.is_system=True. is_read/is_hidden stay on UserArticleState since
they're genuine reading state, not categorization, which is what made
these two candidates for unifying into the general tag mechanism.

System tags are excluded from the Manage Tags page and from delete_tag
(get_object_or_404 now requires is_system=False, so attempting to delete
one 404s) and from the subscription-tag autocomplete, since they
categorize articles, not feeds. The star/save icons are still exactly
where they were — this only changes what powers them.

apps.rules.engine's star action now does get_system_tag + ArticleTag.
get_or_create instead of setting a boolean field. A data migration
converts every existing is_starred=True/is_saved_for_later=True row into
its ArticleTag equivalent before the fields are dropped — verified by
seeding the pre-removal schema directly (including a row with both flags
set) and confirming the migration produces the right tags.

apps.articles.queries.with_user_state gained one more query (tag lookup
for the current page's articles) to stay in the same "fixed number of
queries regardless of article count" shape it already had.

Verified end to end against a running dev server: toggling star/saved,
the starred/saved filters, the sidebar's Favourites count, and that the
Starred tag doesn't appear on the Manage Tags page.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
ksmithandClaude Sonnet 5 committed 2026-10-30 21:36:44 -06:00
1 parent 0e132e9ab9
commit 753ae9efc7
15 files changed
+267 -82

No files matched your search

+18 -8
View File
@@ -2,7 +2,7 @@ import pytest
from django.contrib.auth import get_user_model
from django.urls import reverse
from apps.articles.models import Article, UserArticleState
from apps.articles.models import STARRED_TAG_NAME, Article, ArticleTag, UserArticleState
from apps.feeds.models import Feed, Subscription
User = get_user_model()
@@ -91,14 +91,20 @@ def test_toggle_star_flips_state_and_persists(client, alice, alices_article):
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
assert ArticleTag.objects.filter(
user=alice,
article=alices_article,
tag__is_system=True,
tag__name=STARRED_TAG_NAME,
).exists()
client.post(url)
state.refresh_from_db()
assert state.is_starred is False
assert state.starred_at is None
assert not ArticleTag.objects.filter(
user=alice,
article=alices_article,
tag__is_system=True,
tag__name=STARRED_TAG_NAME,
).exists()
@pytest.mark.django_db
@@ -118,7 +124,11 @@ def test_user_cannot_toggle_state_on_another_users_article(client, alice, bob, a
@pytest.mark.django_db
def test_stream_remembers_last_selected_filter(client, alice, alices_article):
UserArticleState.objects.create(user=alice, article=alices_article, is_starred=True)
from apps.articles.models import get_system_tag
ArticleTag.objects.create(
user=alice, article=alices_article, tag=get_system_tag(alice, STARRED_TAG_NAME)
)
client.login(username="alice", password="pw-alice-123")
response = client.get(reverse("reader:stream"), {"filter": "starred"})
+61 -21
View File
@@ -8,7 +8,15 @@ 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.models import (
SAVED_TAG_NAME,
STARRED_TAG_NAME,
Article,
ArticleTag,
Tag,
UserArticleState,
get_system_tag,
)
from apps.articles.queries import with_user_state
from apps.feeds.models import Feed
@@ -37,6 +45,20 @@ def _get_or_create_state(user, article):
return state
def _article_tag_flags(user, article):
"""is_starred/is_saved_for_later for a single article — the toggle
views' equivalent of with_user_state's bulk version."""
tag_names = set(
ArticleTag.objects.filter(
user=user,
article=article,
tag__is_system=True,
tag__name__in=[STARRED_TAG_NAME, SAVED_TAG_NAME],
).values_list("tag__name", flat=True)
)
return STARRED_TAG_NAME in tag_names, SAVED_TAG_NAME in tag_names
def _apply_scope(request, queryset):
"""Narrows an already-base-scoped Article queryset by the optional
?tag=/?feed= params the sidebar links use. feed_id alone is enough to
@@ -111,10 +133,18 @@ def stream(request):
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)
queryset = queryset.filter(
article_tags__user=request.user,
article_tags__tag__user=request.user,
article_tags__tag__name=STARRED_TAG_NAME,
article_tags__tag__is_system=True,
)
elif active_filter == "saved":
queryset = queryset.filter(
user_states__user=request.user, user_states__is_saved_for_later=True
article_tags__user=request.user,
article_tags__tag__user=request.user,
article_tags__tag__name=SAVED_TAG_NAME,
article_tags__tag__is_system=True,
)
queryset, active_tag, active_feed = _apply_scope(request, queryset)
@@ -188,8 +218,7 @@ def _load_article_and_mark_read(user, article_id):
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
article.is_starred, article.is_saved_for_later = _article_tag_flags(user, article)
return article
@@ -212,38 +241,49 @@ def article_inline(request, article_id):
@login_required
@require_POST
def toggle_read(request, article_id):
return _toggle(request, article_id, "is_read", "read_at")
article = _owned_article(request.user, article_id)
state = _get_or_create_state(request.user, article)
state.is_read = not state.is_read
state.read_at = timezone.now() if state.is_read else None
state.save(update_fields=["is_read", "read_at"])
article.is_read = state.is_read
article.is_starred, article.is_saved_for_later = _article_tag_flags(request.user, article)
return render(request, "reader/_article_actions.html", {"article": article})
@login_required
@require_POST
def toggle_star(request, article_id):
return _toggle(request, article_id, "is_starred", "starred_at")
return _toggle_system_tag(request, article_id, STARRED_TAG_NAME)
@login_required
@require_POST
def toggle_saved(request, article_id):
return _toggle(request, article_id, "is_saved_for_later", None)
return _toggle_system_tag(request, article_id, SAVED_TAG_NAME)
def _toggle(request, article_id, flag_field, timestamp_field):
def _owned_article(user, article_id):
try:
article = _base_queryset(request.user).get(pk=article_id)
return _base_queryset(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
def _toggle_system_tag(request, article_id, tag_name):
article = _owned_article(request.user, article_id)
tag = get_system_tag(request.user, tag_name)
existing = ArticleTag.objects.filter(user=request.user, article=article, tag=tag)
if existing.exists():
existing.delete()
else:
ArticleTag.objects.create(user=request.user, article=article, tag=tag)
state = UserArticleState.objects.filter(user=request.user, article=article).first()
article.is_read = state.is_read if state else False
article.is_starred, article.is_saved_for_later = _article_tag_flags(request.user, article)
return render(request, "reader/_article_actions.html", {"article": article})