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
+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)
|
||||
Reference in new issue
Block a user