Private
Public Access
Adding a feed and the admin retry queued fetch_feed directly without claiming the feed, so the periodic poller (which sees a never-fetched or just-reset feed as due) could claim and fetch it too: duplicate fetches, and one task overwriting another's claim/ETag/next-fetch state. The retry path also queued the backoff reset and the fetch as two independent tasks that could run in either order. claim_feed/enqueue_fetch in apps.feeds.tasks are now the only way a fetch gets dispatched — the poller, add-feed and retry all use them, so a feed can only have one fetch in flight. If queueing fails the claim is released (the poller recovers it), and add-feed still saves the subscription during a broker outage. The retry resets backoff synchronously before claiming, and says so when a fetch is already running. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
181 lines
7.0 KiB
Python
181 lines
7.0 KiB
Python
import logging
|
|
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, extract_opml_bytes, import_opml, parse_opml
|
|
from .tasks import enqueue_fetch, import_opml_task
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
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
|
|
)
|
|
|
|
# Enqueue the initial fetch instead of running it inline (T3) — a
|
|
# slow or large feed would otherwise tie up a Gunicorn worker for
|
|
# the duration of the request; fetch_feed handles the save/error
|
|
# bookkeeping the same way the periodic poll's dispatch does.
|
|
try:
|
|
enqueue_fetch(feed.pk)
|
|
except Exception:
|
|
# The subscription itself is saved; with the broker down the
|
|
# periodic poller picks the (never-fetched) feed up once it's
|
|
# back, so this isn't worth failing the request over.
|
|
logger.exception("Could not queue the initial fetch for feed %s", feed.pk)
|
|
messages.success(
|
|
request,
|
|
f"Subscribed to {feed_url}. Fetching its articles now — "
|
|
"refresh in a few seconds if you don't see them yet.",
|
|
)
|
|
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
|
|
)
|
|
# Starred/Saved for later categorize articles, not feeds — don't
|
|
# suggest them here (see Tag.is_system).
|
|
all_user_tags = Tag.objects.filter(user=request.user, is_system=False).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():
|
|
uploaded = form.cleaned_data["opml_file"].read()
|
|
try:
|
|
content = extract_opml_bytes(uploaded)
|
|
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
|