Private
Public Access
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:
1 parent
0e132e9ab9
commit
753ae9efc7
15 files changed
+267
-82
No files matched your search
+61
-21
@@ -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})
|
||||
Reference in new issue
Block a user