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:
ksmithandClaude Sonnet 5 committed 2026-10-30 16:26:30 -06:00
1 parent 69e3520e3b
commit 579696efc0
12 files changed
+386 -5

No files matched your search

+18
View File
@@ -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)
+118
View File
@@ -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
+25
View File
@@ -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
View File
@@ -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
View File
@@ -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,
+1
View File
@@ -5,5 +5,6 @@ urlpatterns = [
path("admin/", admin.site.urls),
path("", include("apps.accounts.urls")),
path("", include("apps.feeds.urls")),
path("", include("apps.articles.urls")),
path("", include("apps.reader.urls")),
]
+71
View File
@@ -139,6 +139,77 @@ input, button {
text-align: center;
}
.tag-tabs {
display: flex;
flex-wrap: wrap;
gap: 0.4rem;
margin-bottom: 1rem;
}
.tag-pill {
--tag-color: #6b7280;
display: inline-flex;
align-items: center;
gap: 0.3rem;
padding: 0.2rem 0.6rem;
border-radius: 999px;
border: 1px solid var(--tag-color);
color: var(--tag-color);
text-decoration: none;
font-size: 0.85rem;
}
.tag-pill.active {
background: var(--tag-color);
color: #111;
}
.tag-remove {
background: none;
border: none;
color: inherit;
cursor: pointer;
padding: 0;
font-size: 1rem;
line-height: 1;
}
.article-tags {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 0.4rem;
margin: 1rem 0;
}
.add-tag-form {
display: inline-flex;
gap: 0.3rem;
}
.add-tag-form input {
padding: 0.25rem 0.5rem;
font-size: 0.85rem;
width: 8rem;
}
.tag-manage-list {
list-style: none;
padding: 0;
}
.tag-manage-list li {
display: flex;
align-items: center;
gap: 0.75rem;
padding: 0.5rem 0;
border-bottom: 1px solid #eee;
}
.inline-form {
display: inline;
}
.article-content {
line-height: 1.6;
margin-top: 1rem;
+26
View File
@@ -0,0 +1,26 @@
<div id="article-tags-{{ article.pk }}" class="article-tags">
{% for tag in tags %}
<span class="tag-pill" {% if tag.color %}style="--tag-color: {{ tag.color }};"{% endif %}>
{{ tag.name }}
<button
hx-post="{% url 'articles:remove_tag_from_article' article.pk tag.pk %}"
hx-target="#article-tags-{{ article.pk }}"
hx-swap="outerHTML"
class="tag-remove" title="Remove tag">&times;</button>
</span>
{% endfor %}
<form
hx-post="{% url 'articles:add_tag_to_article' article.pk %}"
hx-target="#article-tags-{{ article.pk }}"
hx-swap="outerHTML"
class="add-tag-form">
<input type="text" name="tag_name" placeholder="Add tag" list="user-tags-{{ article.pk }}" maxlength="100">
<button type="submit" class="icon-btn">Add</button>
</form>
<datalist id="user-tags-{{ article.pk }}">
{% for tag in all_user_tags %}
<option value="{{ tag.name }}">
{% endfor %}
</datalist>
</div>
+28
View File
@@ -0,0 +1,28 @@
{% extends "base.html" %}
{% block title %}Tags — Evection{% endblock %}
{% block content %}
<p><a href="{% url 'reader:stream' %}">&larr; Back to stream</a></p>
<h1>Tags</h1>
<ul class="tag-manage-list">
{% for tag in tags %}
<li>
<a href="{% url 'reader:stream' %}?tag={{ tag.pk }}" class="tag-pill"
{% if tag.color %}style="--tag-color: {{ tag.color }};"{% endif %}>{{ tag.name }}</a>
<form method="post" action="{% url 'articles:delete_tag' tag.pk %}" class="inline-form">
{% csrf_token %}
<button type="submit" class="icon-btn">Delete</button>
</form>
</li>
{% empty %}
<li class="empty-state">No tags yet.</li>
{% endfor %}
</ul>
<h2>New tag</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Create</button>
</form>
{% endblock %}
+2 -2
View File
@@ -8,9 +8,9 @@
<nav class="pagination">
{% if has_previous_page %}
<a href="?filter={{ active_filter }}&page={{ page|add:"-1" }}">&larr; Newer</a>
<a href="?filter={{ active_filter }}{% if active_tag %}&tag={{ active_tag.pk }}{% endif %}&page={{ page|add:"-1" }}">&larr; Newer</a>
{% endif %}
{% if has_next_page %}
<a href="?filter={{ active_filter }}&page={{ page|add:"1" }}">Older &rarr;</a>
<a href="?filter={{ active_filter }}{% if active_tag %}&tag={{ active_tag.pk }}{% endif %}&page={{ page|add:"1" }}">Older &rarr;</a>
{% endif %}
</nav>
+6
View File
@@ -13,6 +13,12 @@
{% include "reader/_article_actions.html" %}
<div
id="article-tags-{{ article.pk }}"
hx-get="{% url 'articles:article_tags_partial' article.pk %}"
hx-trigger="load"
hx-swap="outerHTML"></div>
<div class="article-content">
{{ article.content_html|safe }}
</div>
+11 -1
View File
@@ -5,15 +5,25 @@
<h1>Evection</h1>
<nav class="top-nav">
<a href="{% url 'feeds:add_feed' %}">Add feed</a>
<a href="{% url 'articles:tag_list' %}">Tags</a>
<a href="{% url 'accounts:logout' %}">Log out</a>
</nav>
</header>
<nav class="filter-tabs">
{% for key, label in filters %}
<a href="?filter={{ key }}" class="{% if active_filter == key %}active{% endif %}">{{ label }}</a>
<a href="?filter={{ key }}" class="{% if active_filter == key and not active_tag %}active{% endif %}">{{ label }}</a>
{% endfor %}
</nav>
{% if user_tags %}
<nav class="tag-tabs">
{% for tag in user_tags %}
<a href="?filter={{ active_filter }}&tag={{ tag.pk }}" class="tag-pill{% if active_tag.pk == tag.pk %} active{% endif %}"
{% if tag.color %}style="--tag-color: {{ tag.color }};"{% endif %}>{{ tag.name }}</a>
{% endfor %}
</nav>
{% endif %}
{% include "reader/_article_list.html" %}
{% endblock %}