Skip to content

Commit

Permalink
Annotate the number of entries in category_tree templatetag
Browse files Browse the repository at this point in the history
  • Loading branch information
Fantomas42 committed Sep 30, 2016
1 parent 7d7ddcf commit 2dff2aa
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
5 changes: 3 additions & 2 deletions zinnia/templates/zinnia/tags/categories_tree.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
<ul class="{% if not categories %}no-{% endif %}categories-tree category-root">
{% recursetree categories %}
<li{% if node.slug == context_category.slug %} class="current">{% else %}>{% endif %}
<a href="{{ node.get_absolute_url }}" title="{% blocktrans with category=node %}Show all entries in {{ category }}{% endblocktrans %}">{{ node }}</a>
{% blocktrans count entry_count=node.entries_published.count %}{{ entry_count }} entry{% plural %}{{ entry_count }} entries{% endblocktrans %}
<a href="{{ node.get_absolute_url }}"
title="{% blocktrans with category=node %}Show all entries in {{ category }}{% endblocktrans %}">{{ node }}</a>
{% blocktrans count entry_count=node.count_entries %}{{ entry_count }} entry{% plural %}{{ entry_count }} entries{% endblocktrans %}
{% if not node.is_leaf_node %}
<ul class="category-children category-{{ node.slug }}">
{{ children }}
Expand Down
3 changes: 2 additions & 1 deletion zinnia/templatetags/zinnia.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ def get_categories_tree(context, template='zinnia/tags/categories_tree.html'):
Return the categories as a tree.
"""
return {'template': template,
'categories': Category.objects.all(),
'categories': Category.objects.all().annotate(
count_entries=Count('entries')),
'context_category': context.get('category')}


Expand Down
3 changes: 3 additions & 0 deletions zinnia/tests/test_templatetags.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,14 @@ def test_get_categories_tree(self):

category = Category.objects.create(title='Category 1',
slug='category-1')
self.entry.categories.add(category)
self.publish_entry()
source_context = Context({'category': category})
with self.assertNumQueries(0):
context = get_categories_tree(
source_context, 'custom_template.html')
self.assertEqual(len(context['categories']), 1)
self.assertEqual(context['categories'][0].count_entries, 1)
self.assertEqual(context['template'], 'custom_template.html')
self.assertEqual(context['context_category'], category)

Expand Down

0 comments on commit 2dff2aa

Please sign in to comment.