Private
Public Access
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:
1 parent
3b58fdcb7e
commit
0e132e9ab9
24 files changed
+417
-307
No files matched your search
+3
-9
@@ -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
@@ -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",
|
||||
),
|
||||
]
|
||||
@@ -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
@@ -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
@@ -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,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
@@ -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
@@ -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})
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user