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
-5
@@ -1,6 +1,6 @@
|
||||
from django.contrib import admin
|
||||
|
||||
from .models import Article, ArticleTag, Tag, UserArticleState
|
||||
from .models import Article, ArticleTag, SubscriptionTag, Tag, UserArticleState
|
||||
|
||||
|
||||
@admin.register(Article)
|
||||
@@ -12,16 +12,21 @@ class ArticleAdmin(admin.ModelAdmin):
|
||||
|
||||
@admin.register(UserArticleState)
|
||||
class UserArticleStateAdmin(admin.ModelAdmin):
|
||||
list_display = ["user", "article", "is_read", "is_starred", "is_saved_for_later", "is_hidden"]
|
||||
list_filter = ["is_read", "is_starred", "is_saved_for_later", "is_hidden"]
|
||||
list_display = ["user", "article", "is_read", "is_hidden"]
|
||||
list_filter = ["is_read", "is_hidden"]
|
||||
|
||||
|
||||
@admin.register(Tag)
|
||||
class TagAdmin(admin.ModelAdmin):
|
||||
list_display = ["name", "user", "color"]
|
||||
list_filter = ["user"]
|
||||
list_display = ["name", "user", "color", "is_system"]
|
||||
list_filter = ["user", "is_system"]
|
||||
|
||||
|
||||
@admin.register(ArticleTag)
|
||||
class ArticleTagAdmin(admin.ModelAdmin):
|
||||
list_display = ["user", "article", "tag"]
|
||||
|
||||
|
||||
@admin.register(SubscriptionTag)
|
||||
class SubscriptionTagAdmin(admin.ModelAdmin):
|
||||
list_display = ["user", "subscription", "tag"]
|
||||
@@ -0,0 +1,65 @@
|
||||
from django.db import migrations, models
|
||||
|
||||
STARRED_TAG_NAME = "Starred"
|
||||
SAVED_TAG_NAME = "Saved for later"
|
||||
|
||||
|
||||
def migrate_star_and_saved_to_tags(apps, schema_editor):
|
||||
UserArticleState = apps.get_model("articles", "UserArticleState")
|
||||
Tag = apps.get_model("articles", "Tag")
|
||||
ArticleTag = apps.get_model("articles", "ArticleTag")
|
||||
|
||||
tag_cache: dict[tuple[int, str], object] = {}
|
||||
|
||||
def system_tag(user_id, name):
|
||||
key = (user_id, name)
|
||||
if key not in tag_cache:
|
||||
tag, _ = Tag.objects.get_or_create(
|
||||
user_id=user_id, name=name, defaults={"is_system": True}
|
||||
)
|
||||
tag_cache[key] = tag
|
||||
return tag_cache[key]
|
||||
|
||||
for state in UserArticleState.objects.filter(is_starred=True):
|
||||
tag = system_tag(state.user_id, STARRED_TAG_NAME)
|
||||
ArticleTag.objects.get_or_create(user_id=state.user_id, article=state.article, tag=tag)
|
||||
|
||||
for state in UserArticleState.objects.filter(is_saved_for_later=True):
|
||||
tag = system_tag(state.user_id, SAVED_TAG_NAME)
|
||||
ArticleTag.objects.get_or_create(user_id=state.user_id, article=state.article, tag=tag)
|
||||
|
||||
|
||||
def noop_reverse(apps, schema_editor):
|
||||
pass
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("articles", "0003_migrate_folders_to_tags"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name="tag",
|
||||
name="is_system",
|
||||
field=models.BooleanField(default=False),
|
||||
),
|
||||
migrations.RunPython(migrate_star_and_saved_to_tags, noop_reverse),
|
||||
migrations.RemoveIndex(
|
||||
model_name="userarticlestate",
|
||||
name="articles_us_user_id_cb0761_idx",
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name="userarticlestate",
|
||||
name="is_starred",
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name="userarticlestate",
|
||||
name="is_saved_for_later",
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name="userarticlestate",
|
||||
name="starred_at",
|
||||
),
|
||||
]
|
||||
+20
-7
@@ -33,11 +33,15 @@ class Article(models.Model):
|
||||
|
||||
|
||||
class UserArticleState(models.Model):
|
||||
"""Per-user read/star/save/hide state for an Article.
|
||||
"""Per-user read/hide state for an Article.
|
||||
|
||||
Starred and saved-for-later are ArticleTags against pre-configured
|
||||
system tags instead of fields here (see Tag.is_system) — read/hidden
|
||||
stay here since they're genuine reading state, not categorization.
|
||||
|
||||
Rows are created lazily, only on first interaction — a row with no
|
||||
explicit True flags is equivalent to "no row exists" (unread, not
|
||||
starred). Never bulk-create one of these per subscriber on ingest.
|
||||
explicit True flags is equivalent to "no row exists" (unread). Never
|
||||
bulk-create one of these per subscriber on ingest.
|
||||
"""
|
||||
|
||||
user = models.ForeignKey(
|
||||
@@ -46,30 +50,34 @@ class UserArticleState(models.Model):
|
||||
article = models.ForeignKey(Article, on_delete=models.CASCADE, related_name="user_states")
|
||||
|
||||
is_read = models.BooleanField(default=False)
|
||||
is_starred = models.BooleanField(default=False)
|
||||
is_saved_for_later = models.BooleanField(default=False)
|
||||
is_hidden = models.BooleanField(default=False)
|
||||
|
||||
read_at = models.DateTimeField(null=True, blank=True)
|
||||
starred_at = models.DateTimeField(null=True, blank=True)
|
||||
|
||||
class Meta:
|
||||
unique_together = [("user", "article")]
|
||||
indexes = [
|
||||
models.Index(fields=["user", "is_read"]),
|
||||
models.Index(fields=["user", "is_starred"]),
|
||||
]
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.user}: {self.article}"
|
||||
|
||||
|
||||
# Pre-configured per-user tags backing the star/save-for-later icons —
|
||||
# created lazily on first use (see get_system_tag), same as any other tag,
|
||||
# just under a name the app itself controls rather than the user.
|
||||
STARRED_TAG_NAME = "Starred"
|
||||
SAVED_TAG_NAME = "Saved for later"
|
||||
|
||||
|
||||
class Tag(models.Model):
|
||||
user = models.ForeignKey(
|
||||
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="tags"
|
||||
)
|
||||
name = models.CharField(max_length=100)
|
||||
color = models.CharField(max_length=7, blank=True)
|
||||
is_system = models.BooleanField(default=False)
|
||||
|
||||
class Meta:
|
||||
unique_together = [("user", "name")]
|
||||
@@ -79,6 +87,11 @@ class Tag(models.Model):
|
||||
return self.name
|
||||
|
||||
|
||||
def get_system_tag(user, name):
|
||||
tag, _ = Tag.objects.get_or_create(user=user, name=name, defaults={"is_system": True})
|
||||
return tag
|
||||
|
||||
|
||||
class ArticleTag(models.Model):
|
||||
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
|
||||
article = models.ForeignKey(Article, on_delete=models.CASCADE, related_name="article_tags")
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
from django.db.models import Count
|
||||
|
||||
from .models import Article, UserArticleState
|
||||
from .models import SAVED_TAG_NAME, STARRED_TAG_NAME, Article, ArticleTag, UserArticleState
|
||||
|
||||
|
||||
def with_user_state(articles, user):
|
||||
"""Annotates an iterable of Article instances with is_read/is_starred/
|
||||
is_saved_for_later booleans for the given user, in two queries total
|
||||
regardless of how many articles there are (no N+1).
|
||||
regardless of how many articles there are (no N+1). Starred/saved come
|
||||
from ArticleTag against the pre-configured system tags, not a field on
|
||||
UserArticleState — see Tag.is_system.
|
||||
|
||||
Accepts either a queryset or a plain list (search backends return
|
||||
plain lists already ordered by relevance), materializing it either
|
||||
@@ -16,17 +18,28 @@ def with_user_state(articles, user):
|
||||
if not articles:
|
||||
return articles
|
||||
|
||||
article_ids = [a.pk for a in articles]
|
||||
states = {
|
||||
state.article_id: state
|
||||
for state in UserArticleState.objects.filter(
|
||||
user=user, article_id__in=[a.pk for a in articles]
|
||||
)
|
||||
for state in UserArticleState.objects.filter(user=user, article_id__in=article_ids)
|
||||
}
|
||||
|
||||
starred_ids = set()
|
||||
saved_ids = set()
|
||||
tag_rows = ArticleTag.objects.filter(
|
||||
user=user,
|
||||
article_id__in=article_ids,
|
||||
tag__is_system=True,
|
||||
tag__name__in=[STARRED_TAG_NAME, SAVED_TAG_NAME],
|
||||
).values_list("article_id", "tag__name")
|
||||
for article_id, tag_name in tag_rows:
|
||||
(starred_ids if tag_name == STARRED_TAG_NAME else saved_ids).add(article_id)
|
||||
|
||||
for article in articles:
|
||||
state = states.get(article.pk)
|
||||
article.is_read = state.is_read if state else False
|
||||
article.is_starred = state.is_starred if state else False
|
||||
article.is_saved_for_later = state.is_saved_for_later if state else False
|
||||
article.is_starred = article.pk in starred_ids
|
||||
article.is_saved_for_later = article.pk in saved_ids
|
||||
return articles
|
||||
|
||||
|
||||
|
||||
@@ -4,12 +4,18 @@ from django.contrib.auth import get_user_model
|
||||
from apps.feeds.models import Feed, Subscription
|
||||
from apps.rules.models import FilterRule
|
||||
|
||||
from .models import Article, SubscriptionTag, Tag, UserArticleState
|
||||
from .models import STARRED_TAG_NAME, Article, ArticleTag, SubscriptionTag, Tag, UserArticleState
|
||||
from .tasks import apply_filter_rules_for_feed
|
||||
|
||||
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 feed(db):
|
||||
return Feed.objects.create(feed_url="https://example.com/feed.xml", title="Example Feed")
|
||||
@@ -40,7 +46,7 @@ def test_dispatch_applies_rules_only_for_subscribers_with_matching_rules(feed, a
|
||||
|
||||
apply_filter_rules_for_feed(feed.id, [article.pk])
|
||||
|
||||
assert UserArticleState.objects.get(user=alice, article=article).is_starred is True
|
||||
assert is_starred(alice, article) is True
|
||||
# Bob has no rules, so he should get no state row at all.
|
||||
assert not UserArticleState.objects.filter(user=bob, article=article).exists()
|
||||
|
||||
@@ -67,7 +73,7 @@ def test_feed_scoped_rule_does_not_apply_to_other_feeds(alice):
|
||||
apply_filter_rules_for_feed(feed_a.id, [article_a.pk])
|
||||
apply_filter_rules_for_feed(feed_b.id, [article_b.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()
|
||||
|
||||
|
||||
@@ -96,7 +102,7 @@ def test_tag_scoped_rule_only_applies_to_feeds_carrying_that_tag(alice, feed):
|
||||
apply_filter_rules_for_feed(feed.id, [tagged_article.pk])
|
||||
apply_filter_rules_for_feed(other_feed.id, [untagged_article.pk])
|
||||
|
||||
assert UserArticleState.objects.get(user=alice, article=tagged_article).is_starred is True
|
||||
assert is_starred(alice, tagged_article) is True
|
||||
assert not UserArticleState.objects.filter(user=alice, article=untagged_article).exists()
|
||||
|
||||
|
||||
@@ -120,7 +126,7 @@ def test_rule_scoped_to_one_tag_still_applies_to_a_feed_with_multiple_tags(alice
|
||||
|
||||
apply_filter_rules_for_feed(feed.id, [article.pk])
|
||||
|
||||
assert UserArticleState.objects.get(user=alice, article=article).is_starred is True
|
||||
assert is_starred(alice, article) is True
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
|
||||
@@ -23,14 +23,16 @@ def tag_list(request):
|
||||
tag.save()
|
||||
return redirect("articles:tag_list")
|
||||
|
||||
tags = Tag.objects.filter(user=request.user)
|
||||
# Starred/Saved for later are pre-configured tags (see Tag.is_system) —
|
||||
# managed only via the star/save icons, not this page.
|
||||
tags = Tag.objects.filter(user=request.user, is_system=False)
|
||||
return render(request, "articles/tags.html", {"tags": tags, "form": form})
|
||||
|
||||
|
||||
@login_required
|
||||
@require_POST
|
||||
def delete_tag(request, tag_id):
|
||||
tag = get_object_or_404(Tag, user=request.user, pk=tag_id)
|
||||
tag = get_object_or_404(Tag, user=request.user, pk=tag_id, is_system=False)
|
||||
tag.delete()
|
||||
return redirect("articles:tag_list")
|
||||
|
||||
|
||||
Reference in new issue
Block a user