Private
Public Access
Implements tag management (create/delete) and per-article tagging with a freeform "add tag" input that creates the tag on first use instead of requiring a separate create-then-assign flow. Tags render as pills in the stream header; clicking one filters the stream to articles carrying that tag, combinable with the existing read/starred/saved filters. The "Saved" filter already added in the reading-UI milestone functions as the reading list, so this closes out the reading-list/tags milestone without a separate dedicated view. Verified live in-browser: creating a tag from an article, the pill appearing in the stream nav, filtering by it, and deleting it from the tag-management page, all via htmx without full page reloads. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
69 lines
2.3 KiB
Python
69 lines
2.3 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 AddTagToArticleForm, 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")
|
|
|
|
tags = Tag.objects.filter(user=request.user)
|
|
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)
|
|
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 = AddTagToArticleForm(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)
|