From 84fd0e38e3c9f780040177fb7bef3e7c530d534f Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Tue, 1 Oct 2013 22:56:20 +0200 Subject: [PATCH] Finished tag cloud --- gergelypolonkai_django/settings.py | 11 ++++++++ gergelypolonkai_django/taghelper.py | 26 +++++++++++++++++++ .../templates/front_base.html | 6 ++--- 3 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 gergelypolonkai_django/taghelper.py diff --git a/gergelypolonkai_django/settings.py b/gergelypolonkai_django/settings.py index 3a088a0..3391f5b 100644 --- a/gergelypolonkai_django/settings.py +++ b/gergelypolonkai_django/settings.py @@ -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', +) diff --git a/gergelypolonkai_django/taghelper.py b/gergelypolonkai_django/taghelper.py new file mode 100644 index 0000000..b94a0f5 --- /dev/null +++ b/gergelypolonkai_django/taghelper.py @@ -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} diff --git a/gergelypolonkai_django/templates/front_base.html b/gergelypolonkai_django/templates/front_base.html index e389ce6..920f8c1 100644 --- a/gergelypolonkai_django/templates/front_base.html +++ b/gergelypolonkai_django/templates/front_base.html @@ -48,10 +48,10 @@
-{% if tagCloud|length > 0 %} +{% if tagcloud|length > 0 %}
-{% for cloudItem in tagCloud %} - {{ cloudItem.name }}{% if not loop.last %} | {% endif %} +{% for cloudItem in tagcloud %} + {{ cloudItem.name }}{% if not loop.last %} | {% endif %} {% endfor %}
{% endif %}