Private
Public Access
UserArticleState.is_starred/is_saved_for_later/starred_at are gone — starring and saving now add/remove an ArticleTag against a per-user "Starred" / "Saved for later" Tag, created lazily on first use and marked Tag.is_system=True. is_read/is_hidden stay on UserArticleState since they're genuine reading state, not categorization, which is what made these two candidates for unifying into the general tag mechanism. System tags are excluded from the Manage Tags page and from delete_tag (get_object_or_404 now requires is_system=False, so attempting to delete one 404s) and from the subscription-tag autocomplete, since they categorize articles, not feeds. The star/save icons are still exactly where they were — this only changes what powers them. apps.rules.engine's star action now does get_system_tag + ArticleTag. get_or_create instead of setting a boolean field. A data migration converts every existing is_starred=True/is_saved_for_later=True row into its ArticleTag equivalent before the fields are dropped — verified by seeding the pre-removal schema directly (including a row with both flags set) and confirming the migration produces the right tags. apps.articles.queries.with_user_state gained one more query (tag lookup for the current page's articles) to stay in the same "fixed number of queries regardless of article count" shape it already had. Verified end to end against a running dev server: toggling star/saved, the starred/saved filters, the sidebar's Favourites count, and that the Starred tag doesn't appear on the Manage Tags page. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
179 lines
6.7 KiB
Python
179 lines
6.7 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
|
|
)
|
|
# 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():
|
|
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
|