Private
Public Access
Add tags and tag-based stream filtering
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>
This commit is contained in:
1 parent
69e3520e3b
commit
579696efc0
12 files changed
+386
-5
No files matched your search
@@ -0,0 +1,18 @@
|
||||
from django import forms
|
||||
|
||||
from .models import Tag
|
||||
|
||||
|
||||
class TagForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = Tag
|
||||
fields = ["name", "color"]
|
||||
widgets = {"color": forms.TextInput(attrs={"type": "color"})}
|
||||
|
||||
|
||||
class AddTagToArticleForm(forms.Form):
|
||||
"""Freeform tag-name entry — assigning an unknown name creates that tag
|
||||
for the user, so there's one flow instead of "create tag" then
|
||||
"assign tag" as two separate steps."""
|
||||
|
||||
tag_name = forms.CharField(max_length=100)
|
||||
@@ -0,0 +1,118 @@
|
||||
import pytest
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.urls import reverse
|
||||
|
||||
from apps.feeds.models import Feed, Subscription
|
||||
|
||||
from .models import Article, ArticleTag, Tag
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def alice(db):
|
||||
return User.objects.create_user("alice", password="pw-alice-123")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def bob(db):
|
||||
return User.objects.create_user("bob", password="pw-bob-123")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def alices_article(alice):
|
||||
feed = Feed.objects.create(feed_url="https://example.com/feed.xml", title="Example")
|
||||
Subscription.objects.create(user=alice, feed=feed)
|
||||
return Article.objects.create(feed=feed, guid="guid-1", title="Hello world")
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_create_tag(client, alice):
|
||||
client.login(username="alice", password="pw-alice-123")
|
||||
response = client.post(reverse("articles:tag_list"), {"name": "golang", "color": "#ff0000"})
|
||||
assert response.status_code == 302
|
||||
assert Tag.objects.filter(user=alice, name="golang").exists()
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_delete_tag(client, alice):
|
||||
client.login(username="alice", password="pw-alice-123")
|
||||
tag = Tag.objects.create(user=alice, name="golang")
|
||||
|
||||
response = client.post(reverse("articles:delete_tag", args=[tag.pk]))
|
||||
assert response.status_code == 302
|
||||
assert not Tag.objects.filter(pk=tag.pk).exists()
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_user_cannot_delete_another_users_tag(client, alice, bob):
|
||||
client.login(username="bob", password="pw-bob-123")
|
||||
tag = Tag.objects.create(user=alice, name="golang")
|
||||
|
||||
response = client.post(reverse("articles:delete_tag", args=[tag.pk]))
|
||||
assert response.status_code == 404
|
||||
assert Tag.objects.filter(pk=tag.pk).exists()
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_add_tag_to_article_creates_tag_if_missing(client, alice, alices_article):
|
||||
client.login(username="alice", password="pw-alice-123")
|
||||
response = client.post(
|
||||
reverse("articles:add_tag_to_article", args=[alices_article.pk]), {"tag_name": "golang"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert Tag.objects.filter(user=alice, name="golang").exists()
|
||||
assert ArticleTag.objects.filter(
|
||||
user=alice, article=alices_article, tag__name="golang"
|
||||
).exists()
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_add_tag_to_article_reuses_existing_tag(client, alice, alices_article):
|
||||
tag = Tag.objects.create(user=alice, name="golang")
|
||||
client.login(username="alice", password="pw-alice-123")
|
||||
client.post(
|
||||
reverse("articles:add_tag_to_article", args=[alices_article.pk]), {"tag_name": "golang"}
|
||||
)
|
||||
assert Tag.objects.filter(user=alice, name="golang").count() == 1
|
||||
assert ArticleTag.objects.filter(user=alice, article=alices_article, tag=tag).count() == 1
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_remove_tag_from_article(client, alice, alices_article):
|
||||
tag = Tag.objects.create(user=alice, name="golang")
|
||||
ArticleTag.objects.create(user=alice, article=alices_article, tag=tag)
|
||||
client.login(username="alice", password="pw-alice-123")
|
||||
|
||||
response = client.post(
|
||||
reverse("articles:remove_tag_from_article", args=[alices_article.pk, tag.pk])
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert not ArticleTag.objects.filter(user=alice, article=alices_article, tag=tag).exists()
|
||||
# The tag itself survives removal from this one article.
|
||||
assert Tag.objects.filter(pk=tag.pk).exists()
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_user_cannot_tag_another_users_article(client, alice, bob, alices_article):
|
||||
client.login(username="bob", password="pw-bob-123")
|
||||
response = client.post(
|
||||
reverse("articles:add_tag_to_article", args=[alices_article.pk]), {"tag_name": "golang"}
|
||||
)
|
||||
assert response.status_code == 404
|
||||
assert not ArticleTag.objects.filter(article=alices_article).exists()
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_stream_tag_filter_only_shows_tagged_articles(client, alice, alices_article):
|
||||
feed = alices_article.feed
|
||||
other_article = Article.objects.create(feed=feed, guid="guid-2", title="Untagged article")
|
||||
tag = Tag.objects.create(user=alice, name="golang")
|
||||
ArticleTag.objects.create(user=alice, article=alices_article, tag=tag)
|
||||
|
||||
client.login(username="alice", password="pw-alice-123")
|
||||
response = client.get(reverse("reader:stream"), {"filter": "all", "tag": tag.pk})
|
||||
content = response.content.decode()
|
||||
|
||||
assert alices_article.title in content
|
||||
assert other_article.title not in content
|
||||
@@ -0,0 +1,25 @@
|
||||
from django.urls import path
|
||||
|
||||
from . import views
|
||||
|
||||
app_name = "articles"
|
||||
|
||||
urlpatterns = [
|
||||
path("tags/", views.tag_list, name="tag_list"),
|
||||
path("tags/<int:tag_id>/delete/", views.delete_tag, name="delete_tag"),
|
||||
path(
|
||||
"articles/<int:article_id>/tags/",
|
||||
views.article_tags_partial,
|
||||
name="article_tags_partial",
|
||||
),
|
||||
path(
|
||||
"articles/<int:article_id>/tags/add/",
|
||||
views.add_tag_to_article,
|
||||
name="add_tag_to_article",
|
||||
),
|
||||
path(
|
||||
"articles/<int:article_id>/tags/<int:tag_id>/remove/",
|
||||
views.remove_tag_from_article,
|
||||
name="remove_tag_from_article",
|
||||
),
|
||||
]
|
||||
+68
-1
@@ -1 +1,68 @@
|
||||
# Create your views here.
|
||||
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)
|
||||
+12
-1
@@ -5,7 +5,7 @@ from django.shortcuts import get_object_or_404, render
|
||||
from django.utils import timezone
|
||||
from django.views.decorators.http import require_POST
|
||||
|
||||
from apps.articles.models import Article, UserArticleState
|
||||
from apps.articles.models import Article, Tag, UserArticleState
|
||||
|
||||
FILTER_CHOICES = [
|
||||
("unread", "Unread"),
|
||||
@@ -65,6 +65,15 @@ def stream(request):
|
||||
user_states__user=request.user, user_states__is_saved_for_later=True
|
||||
)
|
||||
|
||||
active_tag = None
|
||||
tag_id = request.GET.get("tag")
|
||||
if tag_id:
|
||||
active_tag = Tag.objects.filter(user=request.user, pk=tag_id).first()
|
||||
if active_tag:
|
||||
queryset = queryset.filter(
|
||||
article_tags__user=request.user, article_tags__tag=active_tag
|
||||
)
|
||||
|
||||
try:
|
||||
page = max(int(request.GET.get("page", 1)), 1)
|
||||
except ValueError:
|
||||
@@ -78,6 +87,8 @@ def stream(request):
|
||||
"articles": articles,
|
||||
"active_filter": active_filter,
|
||||
"filters": FILTER_CHOICES,
|
||||
"active_tag": active_tag,
|
||||
"user_tags": Tag.objects.filter(user=request.user),
|
||||
"page": page,
|
||||
"has_next_page": end < total_count,
|
||||
"has_previous_page": page > 1,
|
||||
|
||||
Reference in new issue
Block a user