Skip to content

Commit

Permalink
Hide drafts from autocomplete tag counts
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Sep 18, 2024
1 parent 4435b34 commit 350ac43
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
17 changes: 14 additions & 3 deletions blog/tag_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Value,
IntegerField,
F,
Q,
Subquery,
OuterRef,
Count,
Expand All @@ -21,21 +22,31 @@ def tags_autocomplete(request):
if query:
entry_count = (
Tag.objects.filter(id=OuterRef("pk"))
.annotate(count=Count("entry", distinct=True))
.annotate(
count=Count("entry", filter=Q(entry__is_draft=False), distinct=True)
)
.values("count")
)

# Subquery for counting blogmarks
blogmark_count = (
Tag.objects.filter(id=OuterRef("pk"))
.annotate(count=Count("blogmark", distinct=True))
.annotate(
count=Count(
"blogmark", filter=Q(blogmark__is_draft=False), distinct=True
)
)
.values("count")
)

# Subquery for counting quotations
quotation_count = (
Tag.objects.filter(id=OuterRef("pk"))
.annotate(count=Count("quotation", distinct=True))
.annotate(
count=Count(
"quotation", filter=Q(quotation__is_draft=False), distinct=True
)
)
.values("count")
)

Expand Down
33 changes: 33 additions & 0 deletions blog/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
QuotationFactory,
)
from blog.models import Tag, PreviousTagName
import json


class BlogTests(TransactionTestCase):
Expand Down Expand Up @@ -168,6 +169,22 @@ def test_draft_items_not_displayed(self):
live_entry.get_absolute_url(),
)

counts = json.loads(self.client.get("/tags-autocomplete/?q=testing").content)
assert counts == {
"tags": [
{
"id": testing.pk,
"tag": "testing",
"description": "",
"total_entry": 1,
"total_blogmark": 1,
"total_quotation": 1,
"is_exact_match": 1,
"count": 3,
}
]
}

for path in paths:
response = self.client.get(path)
self.assertNotContains(response, "draftentry")
Expand All @@ -188,6 +205,22 @@ def test_draft_items_not_displayed(self):
self.assertNotContains(response3, robots_fragment)
self.assertNotContains(response3, draft_warning_fragment)

counts2 = json.loads(self.client.get("/tags-autocomplete/?q=testing").content)
assert counts2 == {
"tags": [
{
"id": testing.pk,
"tag": "testing",
"description": "",
"total_entry": 2,
"total_blogmark": 2,
"total_quotation": 2,
"is_exact_match": 1,
"count": 6,
}
]
}

for path in paths:
response4 = self.client.get(path)
self.assertContains(response4, "draftentry")
Expand Down

0 comments on commit 350ac43

Please sign in to comment.