Finished tag cloud

This commit is contained in:
Gergely Polonkai 2013-10-01 22:56:20 +02:00
parent e04a7c6fda
commit 84fd0e38e3
3 changed files with 40 additions and 3 deletions

View File

@ -161,3 +161,14 @@ LOGGING = {
},
}
}
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.tz",
"django.contrib.messages.context_processors.messages",
'gergelypolonkai_django.taghelper.tagcloud',
)

View File

@ -0,0 +1,26 @@
from django.db.models import Count
from taggit.models import Tag
from math import floor
def tagcloud(request):
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
tagcloud = []
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})
tagcloud.sort(key=lambda k: k['name'])
return {'tagcloud': tagcloud}

View File

@ -48,10 +48,10 @@
</ul>
<br class="clear" />
</div>
{% if tagCloud|length > 0 %}
{% if tagcloud|length > 0 %}
<div id="tag-cloud">
{% for cloudItem in tagCloud %}
<a href="{% url 'blog_tag_list' cloudItem.name %}" class="size{{ cloudItem.size }}">{{ cloudItem.name }}</a>{% if not loop.last %} | {% endif %}
{% for cloudItem in tagcloud %}
<a href="{% url 'blog:taglist' cloudItem.slug %}" class="size{{ cloudItem.size }}">{{ cloudItem.name }}</a>{% if not loop.last %} | {% endif %}
{% endfor %}
</div>
{% endif %}