From d1ee0b6e5e4314518fb5b2dadd236212d431849c Mon Sep 17 00:00:00 2001 From: stephenmcd Date: Thu, 24 Feb 2011 11:12:23 +1100 Subject: [PATCH] Added post counts to archive and author listings for blog posts. --- .../templates/blog/includes/filter_panel.html | 16 +++++++++++----- mezzanine/blog/templatetags/blog_tags.py | 17 ++++++++++------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/mezzanine/blog/templates/blog/includes/filter_panel.html b/mezzanine/blog/templates/blog/includes/filter_panel.html index f1a53be282..6d1a2d00e0 100644 --- a/mezzanine/blog/templates/blog/includes/filter_panel.html +++ b/mezzanine/blog/templates/blog/includes/filter_panel.html @@ -15,9 +15,12 @@

{% trans "Recent Posts" %}

{% if months %}

{% trans "Archive" %}

{% for month in months %} -{% ifchanged month.year %}{% if not forloop.first %}{% endif %}

{{ month.year }}

{% endif %} +

{{ month.date.year }}

{% endif %} @@ -47,10 +50,13 @@

{% trans "Tags" %}

{% blog_authors as authors %} {% if authors %}

{% trans "Authors" %}

+ {% endif %}

{% trans "Feeds" %}

diff --git a/mezzanine/blog/templatetags/blog_tags.py b/mezzanine/blog/templatetags/blog_tags.py index 63ddf79b5b..9200be1f11 100644 --- a/mezzanine/blog/templatetags/blog_tags.py +++ b/mezzanine/blog/templatetags/blog_tags.py @@ -60,12 +60,15 @@ def blog_months(*args): """ Put a list of dates for blog posts into the template context. """ - months = [] - for month in BlogPost.objects.published().dates("publish_date", "month", - order="DESC"): - if month not in months: - months.append(month) - return months + dates = BlogPost.objects.published().values_list("publish_date", flat=True) + date_dicts = [{"date": datetime(d.year, d.month, 1)} for d in dates] + month_dicts = [] + for date_dict in date_dicts: + if date_dict not in month_dicts: + month_dicts.append(date_dict) + for i, date_dict in enumerate(month_dicts): + month_dicts[i]["post_count"] = date_dicts.count(date_dict) + return month_dicts @register.as_tag @@ -108,7 +111,7 @@ def blog_authors(*args): Put a list of authors (users) for blog posts into the template context. """ blog_posts = BlogPost.objects.published() - return list(User.objects.filter(blogposts__in=blog_posts).distinct()) + return list(User.objects.filter(blogposts__in=blog_posts).annotate(post_count=Count("blogposts"))) @register.as_tag