From 8820dd89ba6e965a5de8cdaa951784115435dc24 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Fri, 13 Jun 2014 08:23:50 +0200 Subject: [PATCH] Fix tag cloud generating code It now works even if there are no tags. --- gergelypolonkaiweb/helper.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/gergelypolonkaiweb/helper.py b/gergelypolonkaiweb/helper.py index 8fd3193..7a6b4d6 100644 --- a/gergelypolonkaiweb/helper.py +++ b/gergelypolonkaiweb/helper.py @@ -11,25 +11,24 @@ def randomheader(request): return {'header': header_file} def tagcloud(request): + tagcloud = [] tagcloudlist = Tag.objects.annotate(ct=Count('taggit_taggeditem_items')).order_by('-ct') if (len(tagcloudlist) > 0): tmax = tagcloudlist[0].ct tmin = 1 - if (tmax == tmin): - tmax += 1 + if (tmax == tmin): + tmax += 1 - tagcloud = [] + for cloudelement in tagcloudlist: + tagcount = cloudelement.ct - for cloudelement in tagcloudlist: - tagcount = cloudelement.ct + if (tagcount >= tmin): + size = int(floor((5.0 * (tagcount - tmin)) / (tmax - tmin))) + tagcloud.append({'name': cloudelement.name, 'slug': cloudelement.slug, 'size': size}) - if (tagcount >= tmin): - size = int(floor((5.0 * (tagcount - tmin)) / (tmax - tmin))) - tagcloud.append({'name': cloudelement.name, 'slug': cloudelement.slug, 'size': size}) - - tagcloud.sort(key=itemgetter('name')) + tagcloud.sort(key=itemgetter('name')) return {'tagcloud': tagcloud}