Remove folders; feeds are organized by tags now

Folders are gone — organization is tags-only, via the SubscriptionTag
model added last commit. A migration converts every existing Folder into
a same-named Tag applied to its feeds, and every folder-scoped FilterRule
into an equivalent tag-scoped one, before the Folder table is dropped, so
upgrading an existing instance loses nothing (verified by seeding the
pre-removal schema directly and running the migration chain against it).

Unlike folders, a feed can carry more than one tag, so:
- apps.articles.tasks.apply_filter_rules_for_feed and apps.rules.tasks's
  backlog re-apply now scope by "does this feed carry tag X" rather than
  "does this feed belong to folder X".
- apps.feeds.opml: a feed's tags export as a comma-separated "category"
  attribute (the standard OPML way to represent multiple categories)
  instead of nesting under one folder outline. Import reads both that
  attribute and nested category outlines, turning each enclosing level
  into its own tag rather than collapsing to the nearest one.
- The sidebar groups feeds by tag instead of folder, so a feed with two
  tags now appears in both groups; untagged feeds fall into "Untagged".

AddFeedForm gained a comma-separated tag_names field for parity with the
old add-time folder picker. EditSubscriptionForm dropped its folder
field entirely — tagging a subscription happens through the tag editor
already on that page.

Verified end to end: the migration chain from empty, the data migration
against a seeded pre-removal schema (folder/subscription/rule all convert
correctly), and the running app's sidebar grouping, OPML export category
attributes, and tag editor.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
ksmithandClaude Sonnet 5 committed 2026-10-30 21:30:06 -06:00
1 parent 3b58fdcb7e
commit 0e132e9ab9
24 files changed
+417 -307

No files matched your search

@@ -0,0 +1,32 @@
from django.db import migrations
def migrate_folders_to_tags(apps, schema_editor):
Folder = apps.get_model("feeds", "Folder")
Tag = apps.get_model("articles", "Tag")
SubscriptionTag = apps.get_model("articles", "SubscriptionTag")
for folder in Folder.objects.all():
tag, _ = Tag.objects.get_or_create(user_id=folder.user_id, name=folder.name[:100])
for subscription in folder.subscriptions.all():
SubscriptionTag.objects.get_or_create(
user_id=folder.user_id, subscription=subscription, tag=tag
)
def noop_reverse(apps, schema_editor):
# Folders are gone by the time anyone would reverse this; nothing to
# restore them from.
pass
class Migration(migrations.Migration):
dependencies = [
("articles", "0002_subscriptiontag"),
("feeds", "0001_initial"),
]
operations = [
migrations.RunPython(migrate_folders_to_tags, noop_reverse),
]
+10 -5
View File
@@ -5,13 +5,13 @@ from apps.feeds.models import Subscription
from apps.rules.engine import evaluate_rules_for_article
from apps.rules.models import FilterRule
from .models import Article
from .models import Article, SubscriptionTag
@shared_task
def apply_filter_rules_for_feed(feed_id: int, article_ids: list[int]):
"""For every subscriber of ``feed_id``, evaluate their enabled
FilterRules (scoped to this feed/folder or unscoped) against the given
FilterRules (scoped to this feed/tag or unscoped) against the given
articles. Dispatched by the fetch pipeline after new/updated articles
are committed for a feed.
"""
@@ -29,10 +29,15 @@ def apply_filter_rules_for_feed(feed_id: int, article_ids: list[int]):
rules = FilterRule.objects.filter(user=subscription.user, is_enabled=True).filter(
Q(feed_id=feed_id) | Q(feed__isnull=True)
)
if subscription.folder_id:
rules = rules.filter(Q(folder_id=subscription.folder_id) | Q(folder__isnull=True))
subscription_tag_ids = list(
SubscriptionTag.objects.filter(
user=subscription.user, subscription=subscription
).values_list("tag_id", flat=True)
)
if subscription_tag_ids:
rules = rules.filter(Q(tag_id__in=subscription_tag_ids) | Q(tag__isnull=True))
else:
rules = rules.filter(folder__isnull=True)
rules = rules.filter(tag__isnull=True)
rules = list(rules.order_by("priority", "id"))
if not rules:
continue
+38 -14
View File
@@ -1,10 +1,10 @@
import pytest
from django.contrib.auth import get_user_model
from apps.feeds.models import Feed, Folder, Subscription
from apps.feeds.models import Feed, Subscription
from apps.rules.models import FilterRule
from .models import Article, UserArticleState
from .models import Article, SubscriptionTag, Tag, UserArticleState
from .tasks import apply_filter_rules_for_feed
User = get_user_model()
@@ -72,31 +72,55 @@ def test_feed_scoped_rule_does_not_apply_to_other_feeds(alice):
@pytest.mark.django_db
def test_folder_scoped_rule_only_applies_within_that_folder(alice, feed):
def test_tag_scoped_rule_only_applies_to_feeds_carrying_that_tag(alice, feed):
other_feed = Feed.objects.create(feed_url="https://other.example.com/feed.xml")
folder = Folder.objects.create(user=alice, name="Tech")
Subscription.objects.create(user=alice, feed=feed, folder=folder)
Subscription.objects.create(user=alice, feed=other_feed, folder=None)
tagged_sub = Subscription.objects.create(user=alice, feed=feed)
Subscription.objects.create(user=alice, feed=other_feed)
tag = Tag.objects.create(user=alice, name="Tech")
SubscriptionTag.objects.create(user=alice, subscription=tagged_sub, tag=tag)
FilterRule.objects.create(
user=alice,
folder=folder,
tag=tag,
match_field=FilterRule.FIELD_TITLE,
match_type=FilterRule.MATCH_CONTAINS,
match_value="Python",
action=FilterRule.ACTION_STAR,
)
in_folder_article = Article.objects.create(feed=feed, guid="guid-1", title="Python in folder")
outside_folder_article = Article.objects.create(
feed=other_feed, guid="guid-2", title="Python outside folder"
tagged_article = Article.objects.create(feed=feed, guid="guid-1", title="Python in tagged feed")
untagged_article = Article.objects.create(
feed=other_feed, guid="guid-2", title="Python in untagged feed"
)
apply_filter_rules_for_feed(feed.id, [in_folder_article.pk])
apply_filter_rules_for_feed(other_feed.id, [outside_folder_article.pk])
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=in_folder_article).is_starred is True
assert not UserArticleState.objects.filter(user=alice, article=outside_folder_article).exists()
assert UserArticleState.objects.get(user=alice, article=tagged_article).is_starred is True
assert not UserArticleState.objects.filter(user=alice, article=untagged_article).exists()
@pytest.mark.django_db
def test_rule_scoped_to_one_tag_still_applies_to_a_feed_with_multiple_tags(alice, feed):
subscription = Subscription.objects.create(user=alice, feed=feed)
tech_tag = Tag.objects.create(user=alice, name="Tech")
news_tag = Tag.objects.create(user=alice, name="News")
SubscriptionTag.objects.create(user=alice, subscription=subscription, tag=tech_tag)
SubscriptionTag.objects.create(user=alice, subscription=subscription, tag=news_tag)
FilterRule.objects.create(
user=alice,
tag=news_tag,
match_field=FilterRule.FIELD_TITLE,
match_type=FilterRule.MATCH_CONTAINS,
match_value="Python",
action=FilterRule.ACTION_STAR,
)
article = Article.objects.create(feed=feed, guid="guid-1", title="Python news")
apply_filter_rules_for_feed(feed.id, [article.pk])
assert UserArticleState.objects.get(user=alice, article=article).is_starred is True
@pytest.mark.django_db
+32 -22
View File
@@ -1,40 +1,50 @@
from apps.articles.models import Tag, UserArticleState
from apps.articles.models import SubscriptionTag, Tag, UserArticleState
from apps.articles.queries import unread_counts_by_feed
from apps.feeds.models import Folder, Subscription
from apps.feeds.models import Subscription
def sidebar(request):
"""Folder/feed tree, tags, 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.
"""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)
subscriptions = (
Subscription.objects.filter(user=user)
.select_related("feed", "folder")
.order_by("order", "id")
)
tags = list(Tag.objects.filter(user=user))
folders = list(Folder.objects.filter(user=user).order_by("order", "name"))
by_folder = {folder.pk: [] for folder in folders}
unfoldered = []
for subscription in subscriptions:
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)
if subscription.folder_id and subscription.folder_id in by_folder:
by_folder[subscription.folder_id].append(subscription)
else:
unfoldered.append(subscription)
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_folders": [
{"folder": folder, "subscriptions": by_folder[folder.pk]} for folder in folders
"sidebar_tag_groups": [
{"tag": tag, "subscriptions": feeds_by_tag.get(tag.pk, [])} for tag in tags
],
"sidebar_unfoldered": unfoldered,
"sidebar_tags": Tag.objects.filter(user=user),
"sidebar_untagged": untagged,
"sidebar_starred_count": UserArticleState.objects.filter(
user=user, is_starred=True
).count(),
+3 -9
View File
@@ -1,12 +1,6 @@
from django.contrib import admin
from .models import Feed, Folder, Subscription
@admin.register(Folder)
class FolderAdmin(admin.ModelAdmin):
list_display = ["name", "user", "order"]
list_filter = ["user"]
from .models import Feed, Subscription
@admin.register(Feed)
@@ -25,5 +19,5 @@ class FeedAdmin(admin.ModelAdmin):
@admin.register(Subscription)
class SubscriptionAdmin(admin.ModelAdmin):
list_display = ["user", "feed", "folder", "custom_title"]
list_filter = ["user", "folder"]
list_display = ["user", "feed", "custom_title"]
list_filter = ["user"]
+8 -27
View File
@@ -1,46 +1,27 @@
from django import forms
from .models import Folder, Subscription
from .models import Subscription
class AddFeedForm(forms.Form):
feed_url = forms.URLField(label="Feed URL", max_length=1000, assume_scheme="https")
folder = forms.ModelChoiceField(queryset=Folder.objects.none(), required=False)
def __init__(self, *args, user=None, **kwargs):
super().__init__(*args, **kwargs)
if user is not None:
self.fields["folder"].queryset = Folder.objects.filter(user=user)
tag_names = forms.CharField(
label="Tags",
max_length=500,
required=False,
help_text="Comma-separated. Leave blank to tag it later.",
)
class EditSubscriptionForm(forms.ModelForm):
new_folder_name = forms.CharField(
label="Or create a new folder",
max_length=255,
required=False,
help_text="Leave blank to use the folder selected above.",
)
class Meta:
model = Subscription
fields = ["custom_title", "folder"]
fields = ["custom_title"]
labels = {"custom_title": "Title (leave blank to use the feed's own title)"}
def __init__(self, *args, user=None, **kwargs):
super().__init__(*args, **kwargs)
self._user = user
self.fields["folder"].queryset = Folder.objects.filter(user=user)
self.fields["folder"].required = False
def save(self, commit=True):
subscription = super().save(commit=False)
new_folder_name = self.cleaned_data.get("new_folder_name", "").strip()
if new_folder_name:
folder, _created = Folder.objects.get_or_create(user=self._user, name=new_folder_name)
subscription.folder = folder
if commit:
subscription.save()
return subscription
class OPMLImportForm(forms.Form):
@@ -0,0 +1,23 @@
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
("feeds", "0001_initial"),
# Both of these convert any Folder/FilterRule.folder data into its
# tag equivalent before the Folder table disappears out from under
# them.
("articles", "0003_migrate_folders_to_tags"),
("rules", "0002_folder_scope_to_tag_scope"),
]
operations = [
migrations.RemoveField(
model_name="subscription",
name="folder",
),
migrations.DeleteModel(
name="Folder",
),
]
-18
View File
@@ -2,21 +2,6 @@ from django.conf import settings
from django.db import models
class Folder(models.Model):
user = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="folders"
)
name = models.CharField(max_length=255)
order = models.PositiveIntegerField(default=0)
class Meta:
unique_together = [("user", "name")]
ordering = ["order", "name"]
def __str__(self):
return self.name
class Feed(models.Model):
"""A single polled RSS/Atom source, shared across every subscriber.
@@ -68,9 +53,6 @@ class Subscription(models.Model):
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="subscriptions"
)
feed = models.ForeignKey(Feed, on_delete=models.CASCADE, related_name="subscriptions")
folder = models.ForeignKey(
Folder, null=True, blank=True, on_delete=models.SET_NULL, related_name="subscriptions"
)
custom_title = models.CharField(max_length=500, blank=True)
order = models.PositiveIntegerField(default=0)
created_at = models.DateTimeField(auto_now_add=True)
+58 -49
View File
@@ -14,15 +14,20 @@ from datetime import timezone as dt_timezone
import defusedxml.ElementTree as DefusedET
from .models import Feed, Folder, Subscription
from apps.articles.models import SubscriptionTag, Tag
from .models import Feed, Subscription
def parse_opml(file_obj) -> list[dict]:
"""Returns a flat list of {feed_url, title, site_url, folder_name}.
"""Returns a flat list of {feed_url, title, site_url, tag_names}.
Folders are kept to a single flat level, matching the app's folder
model — nesting deeper than one level collapses into its nearest
enclosing folder rather than being represented as a path.
A feed's tags come from two places, combined: every enclosing
category/folder outline's name (so a feed nested two levels deep picks
up both as separate tags — tags aren't limited to one level the way
the old folder scheme was), and a comma-separated "category" attribute
on the feed's own outline, the convention export_opml below writes and
many other readers use for a flat multi-category representation.
"""
tree = DefusedET.parse(file_obj)
root = tree.getroot()
@@ -31,44 +36,45 @@ def parse_opml(file_obj) -> list[dict]:
if body is None:
return entries
def walk(container, folder_name):
def walk(container, ancestor_names):
for outline in container.findall("outline"):
xml_url = outline.get("xmlUrl")
if xml_url:
tag_names = list(ancestor_names)
category_attr = outline.get("category")
if category_attr:
for name in category_attr.split(","):
name = name.strip()
if name and name not in tag_names:
tag_names.append(name)
entries.append(
{
"feed_url": xml_url,
"title": outline.get("title") or outline.get("text") or "",
"site_url": outline.get("htmlUrl") or "",
"folder_name": folder_name,
"tag_names": tag_names,
}
)
else:
nested_name = outline.get("title") or outline.get("text") or folder_name
walk(outline, nested_name)
nested_name = outline.get("title") or outline.get("text")
next_ancestors = ancestor_names + [nested_name] if nested_name else ancestor_names
walk(outline, next_ancestors)
walk(body, None)
walk(body, [])
return entries
def import_opml(user, file_obj) -> dict:
"""Creates/reuses Feeds and Subscriptions for every entry. Does not
fetch any feed content — newly created Feeds have no last_fetched_at,
so the next poll_due_feeds beat cycle picks them up naturally."""
"""Creates/reuses Feeds and Subscriptions for every entry, tagging each
subscription per its entry's tag_names. Does not fetch any feed
content — newly created Feeds have no last_fetched_at, so the next
poll_due_feeds beat cycle picks them up naturally."""
entries = parse_opml(file_obj)
folder_cache: dict[str, Folder] = {}
tag_cache: dict[str, Tag] = {}
created_count = 0
already_subscribed_count = 0
for entry in entries:
folder = None
folder_name = entry["folder_name"]
if folder_name:
folder = folder_cache.get(folder_name)
if folder is None:
folder, _ = Folder.objects.get_or_create(user=user, name=folder_name[:255])
folder_cache[folder_name] = folder
feed, _ = Feed.objects.get_or_create(
feed_url=entry["feed_url"][:1000],
defaults={
@@ -76,16 +82,23 @@ def import_opml(user, file_obj) -> dict:
"site_url": entry["site_url"][:1000],
},
)
_, created = Subscription.objects.get_or_create(
subscription, created = Subscription.objects.get_or_create(
user=user,
feed=feed,
defaults={"folder": folder, "custom_title": ""},
defaults={"custom_title": ""},
)
if created:
created_count += 1
else:
already_subscribed_count += 1
for tag_name in entry["tag_names"]:
tag = tag_cache.get(tag_name)
if tag is None:
tag, _ = Tag.objects.get_or_create(user=user, name=tag_name[:100])
tag_cache[tag_name] = tag
SubscriptionTag.objects.get_or_create(user=user, subscription=subscription, tag=tag)
return {
"total": len(entries),
"created": created_count,
@@ -94,6 +107,10 @@ def import_opml(user, file_obj) -> dict:
def export_opml(user) -> bytes:
"""A flat list of outlines (no folder nesting) with each feed's tags
joined into a comma-separated "category" attribute — the standard OPML
way to represent multi-category membership without picking one tag to
nest under, which a feed with several tags wouldn't fit."""
root = ET.Element("opml", version="2.0")
head = ET.SubElement(root, "head")
ET.SubElement(head, "title").text = "Evection subscriptions"
@@ -103,33 +120,25 @@ def export_opml(user) -> bytes:
body = ET.SubElement(root, "body")
subscriptions = (
Subscription.objects.filter(user=user)
.select_related("feed", "folder")
.order_by("folder__name", "order", "id")
Subscription.objects.filter(user=user).select_related("feed").order_by("order", "id")
)
tag_names_by_subscription: dict[int, list[str]] = {}
for row in (
SubscriptionTag.objects.filter(user=user).select_related("tag").order_by("tag__name")
):
tag_names_by_subscription.setdefault(row.subscription_id, []).append(row.tag.name)
folder_elements: dict[str, ET.Element] = {}
for subscription in subscriptions:
parent = body
if subscription.folder:
parent = folder_elements.get(subscription.folder.name)
if parent is None:
parent = ET.SubElement(
body,
"outline",
title=subscription.folder.name,
text=subscription.folder.name,
)
folder_elements[subscription.folder.name] = parent
ET.SubElement(
parent,
"outline",
type="rss",
text=subscription.display_title,
title=subscription.display_title,
xmlUrl=subscription.feed.feed_url,
htmlUrl=subscription.feed.site_url or "",
)
attrs = {
"type": "rss",
"text": subscription.display_title,
"title": subscription.display_title,
"xmlUrl": subscription.feed.feed_url,
"htmlUrl": subscription.feed.site_url or "",
}
tag_names = tag_names_by_subscription.get(subscription.pk)
if tag_names:
attrs["category"] = ",".join(tag_names)
ET.SubElement(body, "outline", attrs)
return ET.tostring(root, encoding="utf-8", xml_declaration=True)
+66 -27
View File
@@ -3,7 +3,9 @@ from io import BytesIO
import pytest
from django.contrib.auth import get_user_model
from .models import Feed, Folder, Subscription
from apps.articles.models import SubscriptionTag, Tag
from .models import Feed, Subscription
from .opml import export_opml, import_opml, parse_opml
User = get_user_model()
@@ -16,8 +18,8 @@ SIMPLE_OPML = b"""<?xml version="1.0" encoding="UTF-8"?>
<outline type="rss" text="Example Blog" title="Example Blog"
xmlUrl="https://example.com/feed.xml" htmlUrl="https://example.com"/>
</outline>
<outline type="rss" text="Unfoldered Feed" title="Unfoldered Feed"
xmlUrl="https://unfoldered.example.com/feed.xml"/>
<outline type="rss" text="Untagged Feed" title="Untagged Feed"
xmlUrl="https://untagged.example.com/feed.xml"/>
</body>
</opml>
"""
@@ -35,6 +37,15 @@ NESTED_OPML = b"""<?xml version="1.0" encoding="UTF-8"?>
</opml>
"""
CATEGORY_ATTR_OPML = b"""<?xml version="1.0" encoding="UTF-8"?>
<opml version="2.0">
<body>
<outline type="rss" text="Multi-tag feed" category="Tech,Backend"
xmlUrl="https://multi.example.com/feed.xml"/>
</body>
</opml>
"""
MALFORMED_XML = b"<opml><body><outline"
@@ -44,25 +55,32 @@ def alice(db):
class TestParseOPML:
def test_parses_foldered_and_unfoldered_entries(self):
def test_parses_tagged_and_untagged_entries(self):
entries = parse_opml(BytesIO(SIMPLE_OPML))
assert len(entries) == 2
foldered = next(e for e in entries if e["feed_url"] == "https://example.com/feed.xml")
assert foldered["folder_name"] == "Tech"
assert foldered["title"] == "Example Blog"
tagged = next(e for e in entries if e["feed_url"] == "https://example.com/feed.xml")
assert tagged["tag_names"] == ["Tech"]
assert tagged["title"] == "Example Blog"
unfoldered = next(
e for e in entries if e["feed_url"] == "https://unfoldered.example.com/feed.xml"
untagged = next(
e for e in entries if e["feed_url"] == "https://untagged.example.com/feed.xml"
)
assert unfoldered["folder_name"] is None
assert untagged["tag_names"] == []
def test_flattens_nesting_deeper_than_one_level(self):
def test_nesting_produces_a_tag_per_level(self):
entries = parse_opml(BytesIO(NESTED_OPML))
assert len(entries) == 1
# Deepest enclosing folder name wins rather than a path.
assert entries[0]["folder_name"] == "Child"
# Unlike the old single-folder scheme, each enclosing level becomes
# its own tag rather than collapsing to the nearest one.
assert entries[0]["tag_names"] == ["Parent", "Child"]
def test_category_attribute_produces_multiple_tags(self):
entries = parse_opml(BytesIO(CATEGORY_ATTR_OPML))
assert len(entries) == 1
assert entries[0]["tag_names"] == ["Tech", "Backend"]
def test_malformed_xml_raises(self):
with pytest.raises(Exception):
@@ -71,18 +89,23 @@ class TestParseOPML:
@pytest.mark.django_db
class TestImportOPML:
def test_creates_feeds_folders_and_subscriptions(self, alice):
def test_creates_feeds_tags_and_subscriptions(self, alice):
result = import_opml(alice, BytesIO(SIMPLE_OPML))
assert result == {"total": 2, "created": 2, "already_subscribed": 0}
assert Folder.objects.filter(user=alice, name="Tech").exists()
assert Subscription.objects.filter(
assert Tag.objects.filter(user=alice, name="Tech").exists()
tagged_sub = Subscription.objects.get(
user=alice, feed__feed_url="https://example.com/feed.xml"
).exists()
unfoldered_sub = Subscription.objects.get(
user=alice, feed__feed_url="https://unfoldered.example.com/feed.xml"
)
assert unfoldered_sub.folder is None
assert SubscriptionTag.objects.filter(
user=alice, subscription=tagged_sub, tag__name="Tech"
).exists()
untagged_sub = Subscription.objects.get(
user=alice, feed__feed_url="https://untagged.example.com/feed.xml"
)
assert not SubscriptionTag.objects.filter(user=alice, subscription=untagged_sub).exists()
def test_reimporting_the_same_file_does_not_duplicate(self, alice):
import_opml(alice, BytesIO(SIMPLE_OPML))
@@ -90,7 +113,7 @@ class TestImportOPML:
assert result == {"total": 2, "created": 0, "already_subscribed": 2}
assert Subscription.objects.filter(user=alice).count() == 2
assert Folder.objects.filter(user=alice, name="Tech").count() == 1
assert Tag.objects.filter(user=alice, name="Tech").count() == 1
def test_reuses_existing_feed_across_users(self, alice):
bob = User.objects.create_user("bob", password="pw-bob-123")
@@ -109,18 +132,34 @@ class TestExportOPML:
import_opml(alice, BytesIO(SIMPLE_OPML))
exported = export_opml(alice)
assert b'category="Tech"' in exported
bob = User.objects.create_user("bob", password="pw-bob-123")
result = import_opml(bob, BytesIO(exported))
assert result["created"] == 2
assert Folder.objects.filter(user=bob, name="Tech").exists()
assert Subscription.objects.filter(
user=bob, feed__feed_url="https://example.com/feed.xml", folder__name="Tech"
).exists()
assert Subscription.objects.filter(
user=bob, feed__feed_url="https://unfoldered.example.com/feed.xml", folder__isnull=True
tagged_sub = Subscription.objects.get(
user=bob, feed__feed_url="https://example.com/feed.xml"
)
assert SubscriptionTag.objects.filter(
user=bob, subscription=tagged_sub, tag__name="Tech"
).exists()
untagged_sub = Subscription.objects.get(
user=bob, feed__feed_url="https://untagged.example.com/feed.xml"
)
assert not SubscriptionTag.objects.filter(user=bob, subscription=untagged_sub).exists()
def test_export_represents_multiple_tags_as_comma_separated_category(self, alice):
feed = Feed.objects.create(feed_url="https://multi.example.com/feed.xml")
subscription = Subscription.objects.create(user=alice, feed=feed)
for name in ["Tech", "Backend"]:
tag, _ = Tag.objects.get_or_create(user=alice, name=name)
SubscriptionTag.objects.create(user=alice, subscription=subscription, tag=tag)
exported = export_opml(alice)
# Alphabetical (export_opml orders by tag name), not insertion order.
assert b'category="Backend,Tech"' in exported
def test_export_with_no_subscriptions(self, alice):
exported = export_opml(alice)
+3 -36
View File
@@ -3,7 +3,7 @@ from django.contrib.auth import get_user_model
from django.core.files.uploadedfile import SimpleUploadedFile
from django.urls import reverse
from .models import Feed, Folder, Subscription
from .models import Feed, Subscription
from .test_opml import SIMPLE_OPML
User = get_user_model()
@@ -39,50 +39,17 @@ def test_subscription_list_shows_only_own_subscriptions(client, alice, bob, alic
@pytest.mark.django_db
def test_edit_subscription_updates_title_and_folder(client, alice, alices_subscription):
def test_edit_subscription_updates_title(client, alice, alices_subscription):
client.login(username="alice", password="pw-alice-123")
folder = Folder.objects.create(user=alice, name="Tech")
response = client.post(
reverse("feeds:edit_subscription", args=[alices_subscription.pk]),
{"custom_title": "My Example Feed", "folder": folder.pk},
{"custom_title": "My Example Feed"},
)
assert response.status_code == 302
alices_subscription.refresh_from_db()
assert alices_subscription.custom_title == "My Example Feed"
assert alices_subscription.folder == folder
@pytest.mark.django_db
def test_edit_subscription_can_create_a_new_folder(client, alice, alices_subscription):
client.login(username="alice", password="pw-alice-123")
response = client.post(
reverse("feeds:edit_subscription", args=[alices_subscription.pk]),
{"custom_title": "", "folder": "", "new_folder_name": "Brand New Folder"},
)
assert response.status_code == 302
alices_subscription.refresh_from_db()
assert alices_subscription.folder.name == "Brand New Folder"
assert Folder.objects.filter(user=alice, name="Brand New Folder").count() == 1
@pytest.mark.django_db
def test_edit_subscription_can_clear_folder(client, alice, alices_subscription):
client.login(username="alice", password="pw-alice-123")
alices_subscription.folder = Folder.objects.create(user=alice, name="Tech")
alices_subscription.save()
response = client.post(
reverse("feeds:edit_subscription", args=[alices_subscription.pk]),
{"custom_title": "", "folder": ""},
)
assert response.status_code == 302
alices_subscription.refresh_from_db()
assert alices_subscription.folder is None
@pytest.mark.django_db
+28 -14
View File
@@ -3,7 +3,7 @@ import responses
from django.contrib.auth import get_user_model
from django.urls import reverse
from .models import Feed, Folder, Subscription
from .models import Feed, Subscription
User = get_user_model()
@@ -44,19 +44,6 @@ def test_a_user_cannot_subscribe_to_the_same_feed_twice(two_users):
Subscription.objects.create(user=alice, feed=feed)
@pytest.mark.django_db
def test_folder_names_are_scoped_per_user(two_users):
alice, bob = two_users
Folder.objects.create(user=alice, name="Tech")
# Same folder name for a different user must be allowed.
Folder.objects.create(user=bob, name="Tech")
assert Folder.objects.filter(name="Tech").count() == 2
with pytest.raises(Exception):
Folder.objects.create(user=alice, name="Tech")
GOOD_FEED_XML = """<?xml version="1.0"?>
<rss version="2.0"><channel>
<title>Example Blog</title>
@@ -86,6 +73,33 @@ def test_add_feed_view_requires_login(client):
assert response.status_code == 302
@pytest.mark.django_db
@responses.activate
def test_add_feed_view_creates_tags_from_comma_separated_tag_names(client, two_users):
from apps.articles.models import SubscriptionTag, Tag
alice, _ = two_users
client.login(username="alice", password="pw-alice-123")
responses.add(responses.GET, "https://example.com/feed.xml", body=GOOD_FEED_XML, status=200)
response = client.post(
reverse("feeds:add_feed"),
{"feed_url": "https://example.com/feed.xml", "tag_names": "Tech, backend"},
)
assert response.status_code == 302
subscription = Subscription.objects.get(
user=alice, feed__feed_url="https://example.com/feed.xml"
)
tag_names = set(
Tag.objects.filter(
subscription_tags__subscription=subscription, subscription_tags__user=alice
).values_list("name", flat=True)
)
assert tag_names == {"Tech", "backend"}
assert SubscriptionTag.objects.filter(user=alice, subscription=subscription).count() == 2
@pytest.mark.django_db
@responses.activate
def test_add_feed_view_rejects_duplicate_subscription(client, two_users):
+20 -9
View File
@@ -20,20 +20,25 @@ OPML_ASYNC_THRESHOLD = 50
@login_required
def add_feed(request):
form = AddFeedForm(request.POST or None, user=request.user)
form = AddFeedForm(request.POST or None)
if request.method == "POST" and form.is_valid():
feed_url = form.cleaned_data["feed_url"]
feed, _created = Feed.objects.get_or_create(feed_url=feed_url)
subscription, created = Subscription.objects.get_or_create(
user=request.user,
feed=feed,
defaults={"folder": form.cleaned_data["folder"]},
)
subscription, created = Subscription.objects.get_or_create(user=request.user, feed=feed)
if not created:
messages.info(request, "You're already subscribed to that feed.")
return redirect("reader:stream")
tag_names = [
name.strip() for name in form.cleaned_data["tag_names"].split(",") if name.strip()
]
for tag_name in tag_names:
tag, _ = Tag.objects.get_or_create(user=request.user, name=tag_name)
SubscriptionTag.objects.get_or_create(
user=request.user, subscription=subscription, tag=tag
)
# Fetch synchronously so the user sees results immediately; the
# periodic Celery poll takes over from here for subsequent fetches.
result = fetch_and_ingest(feed)
@@ -58,11 +63,17 @@ def add_feed(request):
@login_required
def subscription_list(request):
subscriptions = (
subscriptions = list(
Subscription.objects.filter(user=request.user)
.select_related("feed", "folder")
.order_by("folder__name", "order", "id")
.select_related("feed")
.order_by("order", "id")
)
tags_by_subscription = {}
for row in SubscriptionTag.objects.filter(user=request.user).select_related("tag"):
tags_by_subscription.setdefault(row.subscription_id, []).append(row.tag)
for subscription in subscriptions:
subscription.tags = tags_by_subscription.get(subscription.pk, [])
return render(request, "feeds/subscriptions.html", {"subscriptions": subscriptions})
+7 -22
View File
@@ -10,7 +10,7 @@ from django.views.decorators.http import require_POST
from apps.articles.models import Article, Tag, UserArticleState
from apps.articles.queries import with_user_state
from apps.feeds.models import Feed, Folder
from apps.feeds.models import Feed
FILTER_CHOICES = [
("unread", "Unread"),
@@ -39,13 +39,9 @@ def _get_or_create_state(user, article):
def _apply_scope(request, queryset):
"""Narrows an already-base-scoped Article queryset by the optional
?tag=/?feed=/?folder= params the sidebar links use. feed_id alone is
enough to scope by feed (the base queryset already restricts to the
user's own subscriptions); folder_id must be combined with user in the
same .filter() call — Feed is shared across users, so a separate call
would match any user's subscription with that folder_id, not just this
user's, leaking other users' folder contents in.
"""
?tag=/?feed= params the sidebar links use. feed_id alone is enough to
scope by feed (the base queryset already restricts to the user's own
subscriptions)."""
active_tag = None
tag_id = request.GET.get("tag")
if tag_id:
@@ -71,17 +67,7 @@ def _apply_scope(request, queryset):
if active_feed:
queryset = queryset.filter(feed_id=active_feed.pk)
active_folder = None
folder_id = request.GET.get("folder")
if folder_id:
active_folder = Folder.objects.filter(user=request.user, pk=folder_id).first()
if active_folder:
queryset = queryset.filter(
feed__subscriptions__user=request.user,
feed__subscriptions__folder_id=active_folder.pk,
)
return queryset, active_tag, active_feed, active_folder
return queryset, active_tag, active_feed
def _date_label(day, today):
@@ -131,7 +117,7 @@ def stream(request):
user_states__user=request.user, user_states__is_saved_for_later=True
)
queryset, active_tag, active_feed, active_folder = _apply_scope(request, queryset)
queryset, active_tag, active_feed = _apply_scope(request, queryset)
try:
page = max(int(request.GET.get("page", 1)), 1)
@@ -148,7 +134,6 @@ def stream(request):
"filters": FILTER_CHOICES,
"active_tag": active_tag,
"active_feed": active_feed,
"active_folder": active_folder,
"user_tags": Tag.objects.filter(user=request.user),
"page": page,
"has_next_page": end < total_count,
@@ -164,7 +149,7 @@ def mark_all_read(request):
scope (tag included, "unread"/"all" filter excluded since it's a no-op
choice here) as read — bulk, not one UserArticleState per article."""
queryset = _base_queryset(request.user)
queryset, _active_tag, _active_feed, _active_folder = _apply_scope(request, queryset)
queryset, _active_tag, _active_feed = _apply_scope(request, queryset)
unread_articles = queryset.exclude(user_states__user=request.user, user_states__is_read=True)
unread_ids = list(unread_articles.values_list("id", flat=True))
+6 -4
View File
@@ -1,6 +1,7 @@
from django import forms
from apps.feeds.models import Feed, Folder
from apps.articles.models import Tag
from apps.feeds.models import Feed
from .models import FilterRule
@@ -11,7 +12,7 @@ class FilterRuleForm(forms.ModelForm):
fields = [
"name",
"feed",
"folder",
"tag",
"match_field",
"match_type",
"match_value",
@@ -21,15 +22,16 @@ class FilterRuleForm(forms.ModelForm):
"priority",
"is_enabled",
]
labels = {"tag": "Scope to tag"}
def __init__(self, *args, user=None, **kwargs):
super().__init__(*args, **kwargs)
self._user = user
if user is not None:
self.fields["feed"].queryset = Feed.objects.filter(subscriptions__user=user).distinct()
self.fields["folder"].queryset = Folder.objects.filter(user=user)
self.fields["tag"].queryset = Tag.objects.filter(user=user)
self.fields["feed"].required = False
self.fields["folder"].required = False
self.fields["tag"].required = False
def clean(self):
cleaned_data = super().clean()
@@ -0,0 +1,43 @@
import django.db.models.deletion
from django.db import migrations, models
def migrate_folder_scope_to_tag_scope(apps, schema_editor):
FilterRule = apps.get_model("rules", "FilterRule")
Tag = apps.get_model("articles", "Tag")
for rule in FilterRule.objects.exclude(folder__isnull=True).select_related("folder"):
tag = Tag.objects.filter(user_id=rule.user_id, name=rule.folder.name[:100]).first()
if tag is not None:
rule.tag = tag
rule.save(update_fields=["tag"])
def noop_reverse(apps, schema_editor):
pass
class Migration(migrations.Migration):
dependencies = [
("rules", "0001_initial"),
("articles", "0003_migrate_folders_to_tags"),
]
operations = [
migrations.AddField(
model_name="filterrule",
name="tag",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.CASCADE,
to="articles.tag",
),
),
migrations.RunPython(migrate_folder_scope_to_tag_scope, noop_reverse),
migrations.RemoveField(
model_name="filterrule",
name="folder",
),
]
+6 -4
View File
@@ -1,7 +1,8 @@
from django.conf import settings
from django.db import models
from apps.feeds.models import Feed, Folder
from apps.articles.models import Tag
from apps.feeds.models import Feed
class FilterRule(models.Model):
@@ -53,10 +54,11 @@ class FilterRule(models.Model):
)
name = models.CharField(max_length=255, blank=True)
# Scope: null means "every subscription", matching the plan's decision
# to keep folders flat and scoping simple.
# Scope: null means "every subscription". A tag-scoped rule applies to
# any feed carrying that tag (via SubscriptionTag) — a feed can carry
# more than one tag, unlike the single-folder scoping this replaced.
feed = models.ForeignKey(Feed, null=True, blank=True, on_delete=models.CASCADE)
folder = models.ForeignKey(Folder, null=True, blank=True, on_delete=models.CASCADE)
tag = models.ForeignKey(Tag, null=True, blank=True, on_delete=models.CASCADE)
match_field = models.CharField(max_length=20, choices=MATCH_FIELD_CHOICES)
match_type = models.CharField(max_length=20, choices=MATCH_TYPE_CHOICES)
+5 -5
View File
@@ -10,7 +10,7 @@ from .models import FilterRule
def reapply_rules_for_user_backlog(rule_id: int) -> int:
"""Re-evaluates a user's full enabled ruleset (not just the triggering
rule, so priority/short-circuit ordering stays correct) against their
unread backlog, scoped by the triggering rule's own feed/folder scope.
unread backlog, scoped by the triggering rule's own feed/tag scope.
Exists as a plain function so it can be called synchronously for
immediate feedback (e.g. from a "Re-apply" view) or wrapped in a task
@@ -25,11 +25,11 @@ def reapply_rules_for_user_backlog(rule_id: int) -> int:
if rule.feed_id:
feed_ids = [rule.feed_id]
elif rule.folder_id:
elif rule.tag_id:
feed_ids = list(
Subscription.objects.filter(user=user, folder_id=rule.folder_id).values_list(
"feed_id", flat=True
)
Subscription.objects.filter(
user=user, subscription_tags__user=user, subscription_tags__tag_id=rule.tag_id
).values_list("feed_id", flat=True)
)
else:
feed_ids = list(Subscription.objects.filter(user=user).values_list("feed_id", flat=True))
+1 -1
View File
@@ -10,7 +10,7 @@ from .tasks import reapply_rules_for_user_backlog
@login_required
def rule_list(request):
rules = FilterRule.objects.filter(user=request.user).select_related("feed", "folder")
rules = FilterRule.objects.filter(user=request.user).select_related("feed", "tag")
return render(request, "rules/rule_list.html", {"rules": rules})
+2 -2
View File
@@ -197,12 +197,12 @@ form p {
letter-spacing: 0.04em;
}
.sidebar-folder-link {
.sidebar-tag-link {
color: inherit;
text-decoration: none;
}
.sidebar-folder-link.active {
.sidebar-tag-link.active {
color: var(--accent);
}
+22 -34
View File
@@ -1,4 +1,4 @@
<nav class="sidebar" aria-label="Feeds and folders">
<nav class="sidebar" aria-label="Feeds and tags">
<div class="sidebar-brand">
<a href="{% url 'reader:stream' %}">Evection</a>
</div>
@@ -8,7 +8,7 @@
<ul class="sidebar-top-nav">
<li>
<a href="{% url 'reader:stream' %}?filter=all"
class="{% if not request.GET.tag and not request.GET.feed and not request.GET.folder and request.GET.filter == 'all' %}active{% endif %}">
class="{% if not request.GET.tag and not request.GET.feed and request.GET.filter == 'all' %}active{% endif %}">
All articles
</a>
</li>
@@ -26,46 +26,34 @@
</li>
</ul>
{% if sidebar_tags %}
<details class="sidebar-section" open>
<summary>Tags</summary>
<ul class="sidebar-item-list">
{% for tag in sidebar_tags %}
<li>
<a href="{% url 'reader:stream' %}?tag={{ tag.pk }}"
class="{% if request.GET.tag == tag.pk|stringformat:'s' %}active{% endif %}">{{ tag.name }}</a>
</li>
{% endfor %}
</ul>
</details>
{% endif %}
{% for entry in sidebar_folders %}
{% for entry in sidebar_tag_groups %}
<details class="sidebar-section" open>
<summary>
<a href="{% url 'reader:stream' %}?folder={{ entry.folder.pk }}"
class="sidebar-folder-link{% if request.GET.folder == entry.folder.pk|stringformat:'s' %} active{% endif %}">{{ entry.folder.name }}</a>
<a href="{% url 'reader:stream' %}?tag={{ entry.tag.pk }}"
class="sidebar-tag-link{% if request.GET.tag == entry.tag.pk|stringformat:'s' %} active{% endif %}">{{ entry.tag.name }}</a>
</summary>
<ul class="sidebar-item-list">
{% for subscription in entry.subscriptions %}
<li>
<a href="{% url 'reader:stream' %}?feed={{ subscription.feed_id }}"
class="sidebar-feed-link{% if request.GET.feed == subscription.feed_id|stringformat:'s' %} active{% endif %}">
{% if subscription.feed.favicon_url %}<img src="{{ subscription.feed.favicon_url }}" alt="" class="feed-favicon">{% endif %}
<span>{{ subscription.display_title }}</span>
</a>
{% if subscription.unread_count %}<span class="unread-count">{{ subscription.unread_count }}</span>{% endif %}
</li>
{% endfor %}
</ul>
{% if entry.subscriptions %}
<ul class="sidebar-item-list">
{% for subscription in entry.subscriptions %}
<li>
<a href="{% url 'reader:stream' %}?feed={{ subscription.feed_id }}"
class="sidebar-feed-link{% if request.GET.feed == subscription.feed_id|stringformat:'s' %} active{% endif %}">
{% if subscription.feed.favicon_url %}<img src="{{ subscription.feed.favicon_url }}" alt="" class="feed-favicon">{% endif %}
<span>{{ subscription.display_title }}</span>
</a>
{% if subscription.unread_count %}<span class="unread-count">{{ subscription.unread_count }}</span>{% endif %}
</li>
{% endfor %}
</ul>
{% endif %}
</details>
{% endfor %}
{% if sidebar_unfoldered %}
{% if sidebar_untagged %}
<details class="sidebar-section" open>
<summary>Uncategorized</summary>
<summary>Untagged</summary>
<ul class="sidebar-item-list">
{% for subscription in sidebar_unfoldered %}
{% for subscription in sidebar_untagged %}
<li>
<a href="{% url 'reader:stream' %}?feed={{ subscription.feed_id }}"
class="sidebar-feed-link{% if request.GET.feed == subscription.feed_id|stringformat:'s' %} active{% endif %}">
+1 -1
View File
@@ -15,7 +15,7 @@
<li class="subscription-row">
<div>
<strong>{{ subscription.display_title }}</strong>
{% if subscription.folder %}<span class="rule-summary">{{ subscription.folder.name }}</span>{% endif %}
{% for tag in subscription.tags %}<span class="tag-pill" {% if tag.color %}style="--tag-color: {{ tag.color }};"{% endif %}>{{ tag.name }}</span>{% endfor %}
</div>
<div class="rule-actions">
<a href="{% url 'feeds:edit_subscription' subscription.pk %}" class="icon-btn">Edit</a>
+2 -3
View File
@@ -4,20 +4,19 @@
<div class="stream-toolbar">
<h1 class="stream-scope-title">
{% if active_feed %}{{ active_feed.title|default:active_feed.feed_url }}
{% elif active_folder %}{{ active_folder.name }}
{% elif active_tag %}{{ active_tag.name }}
{% else %}All articles{% endif %}
</h1>
<nav class="filter-tabs">
{% for key, label in filters %}
<a href="?filter={{ key }}{% if active_tag %}&tag={{ active_tag.pk }}{% endif %}{% if active_feed %}&feed={{ active_feed.pk }}{% endif %}{% if active_folder %}&folder={{ active_folder.pk }}{% endif %}"
<a href="?filter={{ key }}{% if active_tag %}&tag={{ active_tag.pk }}{% endif %}{% if active_feed %}&feed={{ active_feed.pk }}{% endif %}"
class="{% if active_filter == key %}active{% endif %}">{{ label }}</a>
{% endfor %}
</nav>
<form method="post"
action="{% url 'reader:mark_all_read' %}?filter={{ active_filter }}{% if active_tag %}&tag={{ active_tag.pk }}{% endif %}{% if active_feed %}&feed={{ active_feed.pk }}{% endif %}{% if active_folder %}&folder={{ active_folder.pk }}{% endif %}"
action="{% url 'reader:mark_all_read' %}?filter={{ active_filter }}{% if active_tag %}&tag={{ active_tag.pk }}{% endif %}{% if active_feed %}&feed={{ active_feed.pk }}{% endif %}"
class="inline-form">
{% csrf_token %}
<button type="submit">Mark all as read</button>
+1 -1
View File
@@ -14,7 +14,7 @@
{{ rule.get_match_field_display }} {{ rule.get_match_type_display|lower }}
"{{ rule.match_value }}"
{% if rule.feed %} in {{ rule.feed.title }}{% endif %}
{% if rule.folder %} in folder {{ rule.folder.name }}{% endif %}
{% if rule.tag %} tagged {{ rule.tag.name }}{% endif %}
&rarr; {{ rule.get_action_display }}{% if rule.action == "tag" %} "{{ rule.action_value }}"{% endif %}
{% if not rule.is_enabled %} (disabled){% endif %}
</div>