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>
71 lines
2.4 KiB
Python
71 lines
2.4 KiB
Python
from django.contrib.auth.decorators import login_required
|
|
from django.http import Http404
|
|
from django.shortcuts import get_object_or_404, redirect, render
|
|
from django.views.decorators.http import require_POST
|
|
|
|
from .forms import AddTagForm, TagForm
|
|
from .models import Article, ArticleTag, Tag
|
|
|
|
|
|
def _owned_article(user, article_id):
|
|
try:
|
|
return Article.objects.get(feed__subscriptions__user=user, pk=article_id)
|
|
except Article.DoesNotExist:
|
|
raise Http404
|
|
|
|
|
|
@login_required
|
|
def tag_list(request):
|
|
form = TagForm(request.POST or None)
|
|
if request.method == "POST" and form.is_valid():
|
|
tag = form.save(commit=False)
|
|
tag.user = request.user
|
|
tag.save()
|
|
return redirect("articles:tag_list")
|
|
|
|
# Starred/Saved for later are pre-configured tags (see Tag.is_system) —
|
|
# managed only via the star/save icons, not this page.
|
|
tags = Tag.objects.filter(user=request.user, is_system=False)
|
|
return render(request, "articles/tags.html", {"tags": tags, "form": form})
|
|
|
|
|
|
@login_required
|
|
@require_POST
|
|
def delete_tag(request, tag_id):
|
|
tag = get_object_or_404(Tag, user=request.user, pk=tag_id, is_system=False)
|
|
tag.delete()
|
|
return redirect("articles:tag_list")
|
|
|
|
|
|
@login_required
|
|
def article_tags_partial(request, article_id):
|
|
article = _owned_article(request.user, article_id)
|
|
tags = Tag.objects.filter(article_tags__article=article, article_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,
|
|
"articles/_article_tags.html",
|
|
{"article": article, "tags": tags, "all_user_tags": all_user_tags},
|
|
)
|
|
|
|
|
|
@login_required
|
|
@require_POST
|
|
def add_tag_to_article(request, article_id):
|
|
article = _owned_article(request.user, article_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)
|
|
ArticleTag.objects.get_or_create(user=request.user, article=article, tag=tag)
|
|
return article_tags_partial(request, article_id)
|
|
|
|
|
|
@login_required
|
|
@require_POST
|
|
def remove_tag_from_article(request, article_id, tag_id):
|
|
article = _owned_article(request.user, article_id)
|
|
ArticleTag.objects.filter(user=request.user, article=article, tag_id=tag_id).delete()
|
|
return article_tags_partial(request, article_id)
|