From 2f529c625c787d18010fcf0be8f3d6a67df7a295 Mon Sep 17 00:00:00 2001 From: NoahY Date: Wed, 27 Jul 2011 00:54:20 +0530 Subject: tag cloud cleaning --- askbot/templatetags/extra_tags.py | 32 +++++++++++++++++++++++--------- askbot/views/readers.py | 39 ++------------------------------------- 2 files changed, 25 insertions(+), 46 deletions(-) diff --git a/askbot/templatetags/extra_tags.py b/askbot/templatetags/extra_tags.py index 95916b65..0fc5547d 100644 --- a/askbot/templatetags/extra_tags.py +++ b/askbot/templatetags/extra_tags.py @@ -45,9 +45,25 @@ def gravatar(user, size): 'alt_text': _('%(username)s gravatar image') % {'username': username}, 'username': username, }) + +@register.simple_tag +def get_tag_font_size(tags): + max_tag = 0 + for tag in tags: + if tag.used_count > max_tag: + max_tag = tag.used_count + + min_tag = max_tag + for tag in tags: + if tag.used_count < min_tag: + min_tag = tag.used_count + + font_size = {} + for tag in tags: + font_size[tag.name] = tag_font_size(max_tag,min_tag,tag.used_count) + + return font_size -MAX_FONTSIZE = 18 -MIN_FONTSIZE = 12 @register.simple_tag def tag_font_size(max_size, min_size, current_size): """ @@ -55,16 +71,14 @@ def tag_font_size(max_size, min_size, current_size): Algorithm from http://blogs.dekoh.com/dev/2007/10/29/choosing-a-good- font-size-variation-algorithm-for-your-tag-cloud/ """ + MAX_FONTSIZE = 7 + MIN_FONTSIZE = 1 + #avoid invalid calculation if current_size == 0: current_size = 1 - try: - weight = (math.log10(current_size) - math.log10(min_size)) / \ - (math.log10(max_size) - math.log10(min_size)) - except Exception: - weight = 0 - return MIN_FONTSIZE + round((MAX_FONTSIZE - MIN_FONTSIZE) * weight) - + weight = (math.log10(current_size) - math.log10(min_size)) / (math.log10(max_size) - math.log10(min_size)) + return int(MIN_FONTSIZE + round((MAX_FONTSIZE - MIN_FONTSIZE) * weight)) #todo: this function may need to be removed to simplify the paginator functionality LEADING_PAGE_RANGE_DISPLAYED = TRAILING_PAGE_RANGE_DISPLAYED = 5 diff --git a/askbot/views/readers.py b/askbot/views/readers.py index 141d826c..b42d6ef7 100644 --- a/askbot/views/readers.py +++ b/askbot/views/readers.py @@ -6,7 +6,6 @@ By main textual content is meant - text of Questions, Answers and Comments. The "read-only" requirement here is not 100% strict, as for example "question" view does allow adding new comments via Ajax form post. """ -import math import datetime import logging import urllib @@ -92,7 +91,7 @@ def questions(request): search_state = search_state, ) - font_size = get_tag_font_size(related_tags) + font_size = extra_tags.get_tag_font_size(related_tags) paginator = Paginator(qs, search_state.page_size) @@ -335,14 +334,12 @@ def tags(request):#view showing a listing of available tags - plain list tags = models.Tag.objects.all().filter(deleted=False).exclude(used_count=0).order_by("name") - font_size = get_tag_font_size(tags) + font_size = extra_tags.get_tag_font_size(tags) data = { 'active_tab': 'tags', 'page_class': 'tags-page', 'tags' : tags, - 'max' : max_tag, - 'min' : min_tag, 'font_size' : font_size, 'stag' : stag, 'tab_id' : sortby, @@ -351,38 +348,6 @@ def tags(request):#view showing a listing of available tags - plain list return render_into_skin('tags.html', data, request) -def get_tag_font_size(tags): - max_tag = 0 - for tag in tags: - if tag.used_count > max_tag: - max_tag = tag.used_count - - min_tag = max_tag - for tag in tags: - if tag.used_count < min_tag: - min_tag = tag.used_count - - font_size = {} - for tag in tags: - font_size[tag.name] = tag_font_size(max_tag,min_tag,tag.used_count) - - return font_size - -def tag_font_size(max_size, min_size, current_size): - """ - do a logarithmic mapping calcuation for a proper size for tagging cloud - Algorithm from http://blogs.dekoh.com/dev/2007/10/29/choosing-a-good- - font-size-variation-algorithm-for-your-tag-cloud/ - """ - MAX_FONTSIZE = 7 - MIN_FONTSIZE = 1 - - #avoid invalid calculation - if current_size == 0: - current_size = 1 - weight = (math.log10(current_size) - math.log10(min_size)) / (math.log10(max_size) - math.log10(min_size)) - return int(MIN_FONTSIZE + round((MAX_FONTSIZE - MIN_FONTSIZE) * weight)) - @csrf.csrf_protect def question(request, id):#refactor - long subroutine. display question body, answers and comments """view that displays body of the question and -- cgit v1.2.3-1-g7c22