Private
Public Access
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>
175 lines
6.6 KiB
Python
175 lines
6.6 KiB
Python
from io import BytesIO
|
|
|
|
from django.contrib import messages
|
|
from django.contrib.auth.decorators import login_required
|
|
from django.http import HttpResponse
|
|
from django.shortcuts import get_object_or_404, redirect, render
|
|
from django.views.decorators.http import require_POST
|
|
|
|
from apps.articles.forms import AddTagForm
|
|
from apps.articles.models import SubscriptionTag, Tag
|
|
|
|
from .forms import AddFeedForm, EditSubscriptionForm, OPMLImportForm
|
|
from .models import Feed, Subscription
|
|
from .opml import export_opml, import_opml, parse_opml
|
|
from .services.fetcher import fetch_and_ingest
|
|
from .tasks import import_opml_task
|
|
|
|
OPML_ASYNC_THRESHOLD = 50
|
|
|
|
|
|
@login_required
|
|
def add_feed(request):
|
|
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)
|
|
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)
|
|
feed.save()
|
|
|
|
if result.status == Feed.STATUS_ERROR:
|
|
messages.warning(
|
|
request,
|
|
f"Subscribed to {feed_url}, but the initial fetch failed: {result.error_message}",
|
|
)
|
|
else:
|
|
article_count = len(result.new_article_ids)
|
|
messages.success(
|
|
request,
|
|
f"Subscribed to '{subscription.display_title}' — "
|
|
f"{article_count} article(s) fetched.",
|
|
)
|
|
return redirect("reader:stream")
|
|
|
|
return render(request, "feeds/add_feed.html", {"form": form})
|
|
|
|
|
|
@login_required
|
|
def subscription_list(request):
|
|
subscriptions = list(
|
|
Subscription.objects.filter(user=request.user)
|
|
.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})
|
|
|
|
|
|
@login_required
|
|
def edit_subscription(request, subscription_id):
|
|
subscription = get_object_or_404(Subscription, user=request.user, pk=subscription_id)
|
|
form = EditSubscriptionForm(request.POST or None, instance=subscription, user=request.user)
|
|
if request.method == "POST" and form.is_valid():
|
|
form.save()
|
|
messages.success(request, f"Updated '{subscription.display_title}'.")
|
|
return redirect("feeds:subscription_list")
|
|
|
|
return render(
|
|
request, "feeds/edit_subscription.html", {"form": form, "subscription": subscription}
|
|
)
|
|
|
|
|
|
@login_required
|
|
def subscription_tags_partial(request, subscription_id):
|
|
subscription = get_object_or_404(Subscription, user=request.user, pk=subscription_id)
|
|
tags = Tag.objects.filter(
|
|
subscription_tags__subscription=subscription, subscription_tags__user=request.user
|
|
)
|
|
all_user_tags = Tag.objects.filter(user=request.user).exclude(pk__in=[t.pk for t in tags])
|
|
return render(
|
|
request,
|
|
"feeds/_subscription_tags.html",
|
|
{"subscription": subscription, "tags": tags, "all_user_tags": all_user_tags},
|
|
)
|
|
|
|
|
|
@login_required
|
|
@require_POST
|
|
def add_tag_to_subscription(request, subscription_id):
|
|
subscription = get_object_or_404(Subscription, user=request.user, pk=subscription_id)
|
|
form = AddTagForm(request.POST)
|
|
if form.is_valid():
|
|
tag_name = form.cleaned_data["tag_name"].strip()
|
|
if tag_name:
|
|
tag, _ = Tag.objects.get_or_create(user=request.user, name=tag_name)
|
|
SubscriptionTag.objects.get_or_create(
|
|
user=request.user, subscription=subscription, tag=tag
|
|
)
|
|
return subscription_tags_partial(request, subscription_id)
|
|
|
|
|
|
@login_required
|
|
@require_POST
|
|
def remove_tag_from_subscription(request, subscription_id, tag_id):
|
|
subscription = get_object_or_404(Subscription, user=request.user, pk=subscription_id)
|
|
SubscriptionTag.objects.filter(
|
|
user=request.user, subscription=subscription, tag_id=tag_id
|
|
).delete()
|
|
return subscription_tags_partial(request, subscription_id)
|
|
|
|
|
|
@login_required
|
|
@require_POST
|
|
def unsubscribe(request, subscription_id):
|
|
subscription = get_object_or_404(Subscription, user=request.user, pk=subscription_id)
|
|
title = subscription.display_title
|
|
subscription.delete()
|
|
messages.success(request, f"Unsubscribed from '{title}'.")
|
|
return redirect("feeds:subscription_list")
|
|
|
|
|
|
@login_required
|
|
def import_opml_view(request):
|
|
form = OPMLImportForm(request.POST or None, request.FILES or None)
|
|
if request.method == "POST" and form.is_valid():
|
|
content = form.cleaned_data["opml_file"].read()
|
|
try:
|
|
entry_count = len(parse_opml(BytesIO(content)))
|
|
except Exception:
|
|
messages.error(request, "That file doesn't look like a valid OPML export.")
|
|
return redirect("feeds:import_opml")
|
|
|
|
if entry_count > OPML_ASYNC_THRESHOLD:
|
|
import_opml_task.delay(request.user.id, content.decode("utf-8"))
|
|
messages.success(request, f"Importing {entry_count} feeds in the background.")
|
|
else:
|
|
result = import_opml(request.user, BytesIO(content))
|
|
messages.success(
|
|
request,
|
|
f"Imported {result['created']} feed(s) "
|
|
f"({result['already_subscribed']} already subscribed).",
|
|
)
|
|
return redirect("feeds:subscription_list")
|
|
|
|
return render(request, "feeds/import_opml.html", {"form": form})
|
|
|
|
|
|
@login_required
|
|
def export_opml_view(request):
|
|
content = export_opml(request.user)
|
|
response = HttpResponse(content, content_type="text/x-opml")
|
|
response["Content-Disposition"] = 'attachment; filename="evection-subscriptions.opml"'
|
|
return response
|