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
+10
-4
@@ -12,7 +12,14 @@ import re
|
||||
|
||||
from django.utils import timezone
|
||||
|
||||
from apps.articles.models import Article, ArticleTag, Tag, UserArticleState
|
||||
from apps.articles.models import (
|
||||
STARRED_TAG_NAME,
|
||||
Article,
|
||||
ArticleTag,
|
||||
Tag,
|
||||
UserArticleState,
|
||||
get_system_tag,
|
||||
)
|
||||
|
||||
from .models import FilterRule
|
||||
|
||||
@@ -75,9 +82,8 @@ def apply_rule_action(rule: FilterRule, user, article: Article, state: UserArtic
|
||||
return True
|
||||
|
||||
if rule.action == FilterRule.ACTION_STAR:
|
||||
state.is_starred = True
|
||||
state.starred_at = state.starred_at or now
|
||||
state.save(update_fields=["is_starred", "starred_at"])
|
||||
tag = get_system_tag(user, STARRED_TAG_NAME)
|
||||
ArticleTag.objects.get_or_create(user=user, article=article, tag=tag)
|
||||
return False
|
||||
|
||||
if rule.action == FilterRule.ACTION_MARK_READ:
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
import pytest
|
||||
from django.contrib.auth import get_user_model
|
||||
|
||||
from apps.articles.models import Article, ArticleTag, Tag, UserArticleState
|
||||
from apps.articles.models import (
|
||||
STARRED_TAG_NAME,
|
||||
Article,
|
||||
ArticleTag,
|
||||
Tag,
|
||||
UserArticleState,
|
||||
)
|
||||
from apps.feeds.models import Feed
|
||||
|
||||
from .engine import evaluate_rules_for_article, rule_matches
|
||||
@@ -10,6 +16,12 @@ from .models import FilterRule
|
||||
User = get_user_model()
|
||||
|
||||
|
||||
def is_starred(user, article):
|
||||
return ArticleTag.objects.filter(
|
||||
user=user, article=article, tag__is_system=True, tag__name=STARRED_TAG_NAME
|
||||
).exists()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def user(db):
|
||||
return User.objects.create_user("alice", password="pw-alice-123")
|
||||
@@ -98,9 +110,8 @@ class TestApplyRuleAction:
|
||||
rules = [make_rule(user, action=FilterRule.ACTION_STAR)]
|
||||
evaluate_rules_for_article(user, article, rules)
|
||||
|
||||
assert is_starred(user, article) is True
|
||||
state = UserArticleState.objects.get(user=user, article=article)
|
||||
assert state.is_starred is True
|
||||
assert state.starred_at is not None
|
||||
assert state.is_read is False
|
||||
|
||||
def test_mark_read_action(self, user, article):
|
||||
@@ -143,8 +154,7 @@ class TestRulePriorityAndShortCircuit:
|
||||
|
||||
evaluate_rules_for_article(user, article, [tag_rule, star_rule])
|
||||
|
||||
state = UserArticleState.objects.get(user=user, article=article)
|
||||
assert state.is_starred is True
|
||||
assert is_starred(user, article) is True
|
||||
assert Tag.objects.filter(user=user, name="early-tag").exists()
|
||||
|
||||
def test_hide_short_circuits_later_rules(self, user, article):
|
||||
@@ -156,7 +166,7 @@ class TestRulePriorityAndShortCircuit:
|
||||
state = UserArticleState.objects.get(user=user, article=article)
|
||||
assert state.is_hidden is True
|
||||
# The star rule never ran because hide short-circuited first.
|
||||
assert state.is_starred is False
|
||||
assert is_starred(user, article) is False
|
||||
|
||||
def test_non_hide_actions_stack_across_multiple_rules(self, user, article):
|
||||
star_rule = make_rule(user, priority=1, action=FilterRule.ACTION_STAR)
|
||||
@@ -164,6 +174,6 @@ class TestRulePriorityAndShortCircuit:
|
||||
|
||||
evaluate_rules_for_article(user, article, [star_rule, read_rule])
|
||||
|
||||
assert is_starred(user, article) is True
|
||||
state = UserArticleState.objects.get(user=user, article=article)
|
||||
assert state.is_starred is True
|
||||
assert state.is_read is True
|
||||
@@ -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
|
||||
|
||||
from .models import FilterRule
|
||||
@@ -11,6 +11,12 @@ from .tasks import reapply_rules_for_user_backlog
|
||||
User = get_user_model()
|
||||
|
||||
|
||||
def is_starred(user, article):
|
||||
return ArticleTag.objects.filter(
|
||||
user=user, article=article, tag__is_system=True, tag__name=STARRED_TAG_NAME
|
||||
).exists()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def alice(db):
|
||||
return User.objects.create_user("alice", password="pw-alice-123")
|
||||
@@ -110,7 +116,7 @@ def test_reapply_rule_view_applies_to_unread_backlog(client, alice, feed):
|
||||
|
||||
response = client.post(reverse("rules:reapply_rule", args=[rule.pk]))
|
||||
assert response.status_code == 302
|
||||
assert UserArticleState.objects.get(user=alice, article=article).is_starred is True
|
||||
assert is_starred(alice, article) is True
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
@@ -129,8 +135,7 @@ def test_reapply_skips_already_read_articles(alice, feed):
|
||||
count = reapply_rules_for_user_backlog(rule.pk)
|
||||
|
||||
assert count == 0
|
||||
state = UserArticleState.objects.get(user=alice, article=article)
|
||||
assert state.is_starred is False
|
||||
assert is_starred(alice, article) is False
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
@@ -153,5 +158,5 @@ def test_reapply_respects_feed_scope(alice):
|
||||
|
||||
reapply_rules_for_user_backlog(rule.pk)
|
||||
|
||||
assert UserArticleState.objects.get(user=alice, article=article_a).is_starred is True
|
||||
assert is_starred(alice, article_a) is True
|
||||
assert not UserArticleState.objects.filter(user=alice, article=article_b).exists()
|
||||
Reference in new issue
Block a user