Fix tag cloud generating code

It now works even if there are no tags.
This commit is contained in:
Gergely Polonkai 2014-06-13 08:23:50 +02:00
parent 4f8db2dc97
commit 8820dd89ba

View File

@ -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}