Private
Public Access
Add OPML import/export and a subscriptions management page
Implements OPML parsing with defusedxml (untrusted upload, XXE risk) and export with the stdlib's plain ElementTree (trusted, app-generated output). Nesting deeper than one level flattens into its nearest enclosing folder, matching the app's flat-folder model. Import reuses existing Feed/Subscription rows via get_or_create, so re-importing the same file is idempotent and cross-user Feed dedup is preserved. Large imports (>50 entries) dispatch to a Celery task instead of blocking the request; newly imported feeds aren't fetched immediately since they have no last_fetched_at, so the next poll_due_feeds beat cycle picks them up naturally. Adds a Subscriptions page (replacing the standalone "Add feed" link in nav) listing current subscriptions with unsubscribe, plus Import/ Export OPML actions. Verified live: imported a 2-feed/1-folder OPML file against the running dev server (via an authenticated session, since this browser tool has no file-picker automation), confirmed the resulting subscriptions and folder in the UI, exported and inspected the resulting OPML structure, and unsubscribed from one feed. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8653dbeaf9
commit
ea214dd9ce
11 files changed
+533
-3
No files matched your search
@@ -0,0 +1,135 @@
|
||||
"""OPML import/export.
|
||||
|
||||
Import parses with defusedxml since an uploaded .opml file is untrusted
|
||||
user input (XXE risk with a plain XML parser). Export uses the stdlib's
|
||||
plain ElementTree since the output is entirely app-generated — no
|
||||
untrusted input, no XXE concern there.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import xml.etree.ElementTree as ET
|
||||
from datetime import datetime
|
||||
from datetime import timezone as dt_timezone
|
||||
|
||||
import defusedxml.ElementTree as DefusedET
|
||||
|
||||
from .models import Feed, Folder, Subscription
|
||||
|
||||
|
||||
def parse_opml(file_obj) -> list[dict]:
|
||||
"""Returns a flat list of {feed_url, title, site_url, folder_name}.
|
||||
|
||||
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.
|
||||
"""
|
||||
tree = DefusedET.parse(file_obj)
|
||||
root = tree.getroot()
|
||||
body = root.find("body")
|
||||
entries: list[dict] = []
|
||||
if body is None:
|
||||
return entries
|
||||
|
||||
def walk(container, folder_name):
|
||||
for outline in container.findall("outline"):
|
||||
xml_url = outline.get("xmlUrl")
|
||||
if xml_url:
|
||||
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,
|
||||
}
|
||||
)
|
||||
else:
|
||||
nested_name = outline.get("title") or outline.get("text") or folder_name
|
||||
walk(outline, nested_name)
|
||||
|
||||
walk(body, None)
|
||||
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."""
|
||||
entries = parse_opml(file_obj)
|
||||
folder_cache: dict[str, Folder] = {}
|
||||
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={
|
||||
"title": entry["title"][:500],
|
||||
"site_url": entry["site_url"][:1000],
|
||||
},
|
||||
)
|
||||
_, created = Subscription.objects.get_or_create(
|
||||
user=user,
|
||||
feed=feed,
|
||||
defaults={"folder": folder, "custom_title": ""},
|
||||
)
|
||||
if created:
|
||||
created_count += 1
|
||||
else:
|
||||
already_subscribed_count += 1
|
||||
|
||||
return {
|
||||
"total": len(entries),
|
||||
"created": created_count,
|
||||
"already_subscribed": already_subscribed_count,
|
||||
}
|
||||
|
||||
|
||||
def export_opml(user) -> bytes:
|
||||
root = ET.Element("opml", version="2.0")
|
||||
head = ET.SubElement(root, "head")
|
||||
ET.SubElement(head, "title").text = "Evection subscriptions"
|
||||
ET.SubElement(head, "dateCreated").text = datetime.now(dt_timezone.utc).strftime(
|
||||
"%a, %d %b %Y %H:%M:%S %z"
|
||||
)
|
||||
body = ET.SubElement(root, "body")
|
||||
|
||||
subscriptions = (
|
||||
Subscription.objects.filter(user=user)
|
||||
.select_related("feed", "folder")
|
||||
.order_by("folder__name", "order", "id")
|
||||
)
|
||||
|
||||
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 "",
|
||||
)
|
||||
|
||||
return ET.tostring(root, encoding="utf-8", xml_declaration=True)
|
||||
Reference in new issue
Block a user