Files
Evection/apps/core/context_processors.py
T
ksmithandClaude Sonnet 5 753ae9efc7 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>
2026-10-30 21:36:44 -06:00

54 lines
2.2 KiB
Python

from apps.articles.models import STARRED_TAG_NAME, ArticleTag, SubscriptionTag, Tag
from apps.articles.queries import unread_counts_by_feed
from apps.feeds.models import Subscription
def sidebar(request):
"""Tag/feed tree 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.
Feeds are grouped by tag (a feed can appear under more than one, since
SubscriptionTag is many-to-many — unlike the single-folder scheme this
replaced); feeds with no tags at all fall into "untagged".
"""
user = getattr(request, "user", None)
if user is None or not user.is_authenticated:
return {}
unread_counts = unread_counts_by_feed(user)
# System tags (Starred/Saved for later) categorize articles, not feeds
# — they get their own pinned nav items above, not a feed-tag group.
tags = list(Tag.objects.filter(user=user, is_system=False))
tag_rows = (
SubscriptionTag.objects.filter(user=user)
.select_related("subscription__feed", "tag")
.order_by("tag__name", "subscription__order", "subscription__id")
)
feeds_by_tag = {tag.pk: [] for tag in tags}
tagged_subscription_ids = set()
for row in tag_rows:
subscription = row.subscription
subscription.unread_count = unread_counts.get(subscription.feed_id, 0)
feeds_by_tag.setdefault(row.tag_id, []).append(subscription)
tagged_subscription_ids.add(subscription.pk)
untagged = []
for subscription in (
Subscription.objects.filter(user=user).select_related("feed").order_by("order", "id")
):
if subscription.pk not in tagged_subscription_ids:
subscription.unread_count = unread_counts.get(subscription.feed_id, 0)
untagged.append(subscription)
return {
"sidebar_tag_groups": [
{"tag": tag, "subscriptions": feeds_by_tag.get(tag.pk, [])} for tag in tags
],
"sidebar_untagged": untagged,
"sidebar_starred_count": ArticleTag.objects.filter(
user=user, tag__user=user, tag__name=STARRED_TAG_NAME, tag__is_system=True
).count(),
}