feat(links): auto-create non-existent tags on link create, lowercase all

Select2 submits raw text as the value for new tags typed into the tag
box (tags: true + createTag in link_form.html). The previous
ModelMultipleChoiceField silently dropped those values because they
were not PKs of existing tags, so new tags were never persisted.

Add TagInputField, a tolerant ModelMultipleChoiceField subclass:
  - existing tag PKs resolve as before
  - raw text is treated as a new (or case-insensitive-existing) tag
  - names are stripped and lowercased before get_or_create so the tag
    namespace stays consistent and case-insensitive duplicates are
    avoided. The Tag.save() hook still derives the slug.

LinkForm now uses TagInputField for its tags field, so both
LinkCreateView and LinkUpdateView pick up the new behaviour. The
Select2 createTag callback in link_form.html now lowercases the term
so users see immediately what will be stored.

Add 7 regression tests in TestLinkTagAutoCreate covering: new tag
created + lowercased; multiple new tags; existing tag reused (no
duplicate); mixed PK + new-text submission; no tags submitted; empty
or whitespace-only submission ignored; whitespace stripped.
This commit is contained in:
2026-07-16 10:10:07 +10:00
parent e625ed13eb
commit bf3e2e80cf
3 changed files with 155 additions and 4 deletions
+98 -1
View File
@@ -5,7 +5,7 @@ Links are managed via Django template views, tested through the Django test clie
import pytest
from links.models import Link
from links.models import Link, Tag
@pytest.mark.django_db
@@ -137,3 +137,100 @@ class TestTemplateLinkFormValidation:
follow=True,
)
assert not Link.objects.filter(alias="bad-url").exists()
@pytest.mark.django_db
class TestLinkTagAutoCreate:
"""Select2 submits either existing tag PKs or raw text for new tags.
New tags must be created server-side and lowercased."""
def _create(self, client, tags):
return client.post(
"/create/",
{
"alias": "tagged-link",
"original_url": "https://example.com",
"link_type": Link.LinkType.LINK,
"description": "",
"text": "",
"tags": tags,
},
follow=True,
)
def test_new_tag_is_created_and_lowercased(self, client):
existing = Tag.objects.count()
response = self._create(client, ["Python"])
assert response.status_code == 200
assert Link.objects.filter(alias="tagged-link").exists()
link = Link.objects.get(alias="tagged-link")
created = Tag.objects.exclude(pk__in=[t.pk for t in link.tags.all()])
assert Tag.objects.count() == existing + 1
tag = Tag.objects.filter(name__iexact="python").first()
assert tag is not None
assert tag.name == "python"
assert tag.slug == "python"
assert tag in link.tags.all()
def test_multiple_new_tags_lowercased(self, client):
before = Tag.objects.count()
response = self._create(client, ["Python", "Django", "WEB"])
assert response.status_code == 200
link = Link.objects.get(alias="tagged-link")
names = sorted(t.name for t in link.tags.all())
assert names == ["django", "python", "web"]
assert Tag.objects.count() == before + 3
def test_existing_tag_reused_no_duplicate(self, client):
existing = Tag.objects.create(name="python", slug="python")
before = Tag.objects.count()
response = self._create(client, ["Python"])
assert response.status_code == 200
link = Link.objects.get(alias="tagged-link")
assert link.tags.count() == 1
assert link.tags.first().pk == existing.pk
# No duplicate tag created
assert Tag.objects.count() == before
assert Tag.objects.filter(name="python").count() == 1
def test_existing_tag_pk_and_new_tag_mixed(self, client):
existing = Tag.objects.create(name="existing", slug="existing")
before = Tag.objects.count()
response = self._create(client, [str(existing.pk), "NewTag"])
assert response.status_code == 200
link = Link.objects.get(alias="tagged-link")
names = sorted(t.name for t in link.tags.all())
assert names == ["existing", "newtag"]
assert Tag.objects.count() == before + 1
def test_no_tags_submitted_creates_no_tags(self, client):
before = Tag.objects.count()
response = self._create(client, [])
assert response.status_code == 200
link = Link.objects.get(alias="tagged-link")
assert link.tags.count() == 0
assert Tag.objects.count() == before
def test_empty_and_whitespace_values_ignored(self, client):
before = Tag.objects.count()
response = self._create(client, ["", " ", "real"])
assert response.status_code == 200
link = Link.objects.get(alias="tagged-link")
assert link.tags.count() == 1
assert link.tags.first().name == "real"
assert Tag.objects.count() == before + 1
def test_whitespace_in_tag_name_is_stripped(self, client):
before = Tag.objects.count()
# Select2's createTag trims, but be defensive server-side too
response = self._create(client, [" spaced out "])
assert response.status_code == 200
link = Link.objects.get(alias="tagged-link")
tag = link.tags.first()
assert tag.name == "spaced out"
assert tag.slug == "spaced-out"
assert Tag.objects.count() == before + 1