Skip to content

Commit 53ee121

Browse files
committed
Add top tags page
1 parent b609dca commit 53ee121

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

blog/tests.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,18 @@ def test_og_description_escapes_quotes_tag_page(self):
362362
'<meta property="og:description" content="1 posts tagged ‘test’. Tag with &quot;quotes&quot;"',
363363
html=False,
364364
)
365+
366+
def test_top_tags_page(self):
367+
for i in range(1, 12):
368+
tag = Tag.objects.create(tag=f"tag{i}")
369+
for j in range(i):
370+
entry = EntryFactory(title=f"Entry{i}-{j}")
371+
entry.tags.add(tag)
372+
response = self.client.get("/top-tags/")
373+
assert response.status_code == 200
374+
tags_info = response.context["tags_info"]
375+
self.assertEqual(len(tags_info), 10)
376+
self.assertEqual(tags_info[0]["tag"].tag, "tag11")
377+
self.assertFalse(any(info["tag"].tag == "tag1" for info in tags_info))
378+
latest = Tag.objects.get(tag="tag11").entry_set.order_by("-created")[0].title
379+
self.assertContains(response, latest)

blog/views.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,43 @@ def tag_index(request):
436436
return render(request, "tags.html")
437437

438438

439+
def top_tags(request):
440+
"""Display recent headlines for the 10 most popular tags."""
441+
tags = (
442+
Tag.objects.annotate(
443+
entry_count=models.Count(
444+
"entry", filter=models.Q(entry__is_draft=False), distinct=True
445+
),
446+
blogmark_count=models.Count(
447+
"blogmark", filter=models.Q(blogmark__is_draft=False), distinct=True
448+
),
449+
quotation_count=models.Count(
450+
"quotation", filter=models.Q(quotation__is_draft=False), distinct=True
451+
),
452+
note_count=models.Count(
453+
"note", filter=models.Q(note__is_draft=False), distinct=True
454+
),
455+
)
456+
.annotate(
457+
total=models.F("entry_count")
458+
+ models.F("blogmark_count")
459+
+ models.F("quotation_count")
460+
+ models.F("note_count")
461+
)
462+
.order_by("-total")[:10]
463+
)
464+
tags_info = [
465+
{
466+
"tag": tag,
467+
"total": tag.total,
468+
"recent_entries": tag.entry_set.filter(is_draft=False)
469+
.order_by("-created")[:5],
470+
}
471+
for tag in tags
472+
]
473+
return render(request, "top_tags.html", {"tags_info": tags_info})
474+
475+
439476
# This query gets the IDs of things that match all of the tags
440477
INTERSECTION_SQL = """
441478
SELECT %(content_table)s.id

config/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ def versions(request):
147147
re_path(r"^favicon\.ico$", favicon_ico),
148148
re_path(r"^search/$", search_views.search),
149149
re_path(r"^about/$", blog_views.about),
150+
path("top-tags/", blog_views.top_tags),
150151
re_path(r"^tags/$", blog_views.tag_index),
151152
re_path(r"^tags/(.*?)/$", blog_views.archive_tag),
152153
re_path(r"^tags/(.*?).atom$", blog_views.archive_tag_atom),

templates/top_tags.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{% extends "smallhead.html" %}
2+
{% block title %}Top tags{% endblock %}
3+
{% block primary %}
4+
<h2 class="archive-h2">Top tags</h2>
5+
{% for info in tags_info %}
6+
<h3><a href="{{ info.tag.get_absolute_url }}">{{ info.tag.tag }}</a> ({{ info.total }})</h3>
7+
<ul>
8+
{% for entry in info.recent_entries %}
9+
<li><a href="{{ entry.get_absolute_url }}">{{ entry.title }}</a></li>
10+
{% empty %}
11+
<li>No entries yet</li>
12+
{% endfor %}
13+
</ul>
14+
{% endfor %}
15+
{% endblock %}

0 commit comments

Comments
 (0)