diff options
Diffstat (limited to 'forum/templatetags/extra_tags.py')
-rw-r--r-- | forum/templatetags/extra_tags.py | 162 |
1 files changed, 107 insertions, 55 deletions
diff --git a/forum/templatetags/extra_tags.py b/forum/templatetags/extra_tags.py index e82537fa..15f97ac8 100644 --- a/forum/templatetags/extra_tags.py +++ b/forum/templatetags/extra_tags.py @@ -9,6 +9,7 @@ from django.utils.encoding import smart_unicode from django.utils.safestring import mark_safe from forum.const import * from forum.models import Question, Answer, QuestionRevision, AnswerRevision +from forum.models import Badge from django.utils.translation import ugettext as _ from django.utils.translation import ungettext from django.conf import settings @@ -149,63 +150,114 @@ def post_contributor_info(post,contributor_type='original_author'): 'wiki_on':forum_settings.WIKI_ON, 'contributor_type':contributor_type } - + + +BADGE_TEMPLATE = '<span title="%(pluralized_badge_count)s">' \ + + '<span class="%(badge_css_class)s">%(badge_symbol)s</span>' \ + + '<span class="badgecount">%(badge_count)s</span>' \ + + '</span>' +BADGE_LEVELS = dict(Badge.TYPE_CHOICES) + +def render_badge_counter(badge_level = None, badge_count = None): + + pluralized_badge_count = ungettext( + '%(badge_count)d %(badge_level)s badge', + '%(badge_count)d %(badge_level)s badges', + badge_count + ) % { + 'badge_count': badge_count, + 'badge_level': BADGE_LEVELS[badge_level] + } + + output = BADGE_TEMPLATE % \ + { + 'pluralized_badge_count': pluralized_badge_count, + 'badge_css_class': Badge.CSS_CLASSES[badge_level], + 'badge_symbol': Badge.DISPLAY_SYMBOL, + 'badge_count': badge_count, + } + return output + + +REP_TEMPLATE = '<span class="reputation-score" ' \ + + 'title="%(reputation)s %(repword)s">%(reputation)s</span>' @register.simple_tag -def get_score_badge(user): - BADGE_TEMPLATE = '<span class="score" title="%(reputation)s %(reputationword)s">%(reputation)s</span>' - if user.gold > 0 : - BADGE_TEMPLATE = '%s%s' % (BADGE_TEMPLATE, '<span title="%(gold)s %(badgesword)s">' - '<span class="badge1">●</span>' - '<span class="badgecount">%(gold)s</span>' - '</span>') - if user.silver > 0: - BADGE_TEMPLATE = '%s%s' % (BADGE_TEMPLATE, '<span title="%(silver)s %(badgesword)s">' - '<span class="silver">●</span>' - '<span class="badgecount">%(silver)s</span>' - '</span>') - if user.bronze > 0: - BADGE_TEMPLATE = '%s%s' % (BADGE_TEMPLATE, '<span title="%(bronze)s %(badgesword)s">' - '<span class="bronze">●</span>' - '<span class="badgecount">%(bronze)s</span>' - '</span>') - BADGE_TEMPLATE = smart_unicode(BADGE_TEMPLATE, encoding='utf-8', strings_only=False, errors='strict') - return mark_safe(BADGE_TEMPLATE % { - 'reputation' : user.reputation, - 'gold' : user.gold, - 'silver' : user.silver, - 'bronze' : user.bronze, - 'badgesword' : _('badges'), - 'reputationword' : _('reputation points'), - }) - +def render_reputation_counter(rep): + return REP_TEMPLATE % { + 'repword': _('reputation points'), + 'reputation': rep + } + + @register.simple_tag -def get_score_badge_by_details(rep, gold, silver, bronze): - BADGE_TEMPLATE = '<span class="reputation-score" title="%(reputation)s %(repword)s">%(reputation)s</span>' - if gold > 0 : - BADGE_TEMPLATE = '%s%s' % (BADGE_TEMPLATE, '<span title="%(gold)s %(badgeword)s">' - '<span class="badge1">●</span>' - '<span class="badgecount">%(gold)s</span>' - '</span>') - if silver > 0: - BADGE_TEMPLATE = '%s%s' % (BADGE_TEMPLATE, '<span title="%(silver)s %(badgeword)s">' - '<span class="badge2">●</span>' - '<span class="badgecount">%(silver)s</span>' - '</span>') - if bronze > 0: - BADGE_TEMPLATE = '%s%s' % (BADGE_TEMPLATE, '<span title="%(bronze)s %(badgeword)s">' - '<span class="badge3">●</span>' - '<span class="badgecount">%(bronze)s</span>' - '</span>') - BADGE_TEMPLATE = smart_unicode(BADGE_TEMPLATE, encoding='utf-8', strings_only=False, errors='strict') - return mark_safe(BADGE_TEMPLATE % { - 'reputation' : rep, - 'gold' : gold, - 'silver' : silver, - 'bronze' : bronze, - 'repword' : _('reputation points'), - 'badgeword' : _('badges'), - }) - +def render_badge_counters(gold_count, silver_count, bronze_count): + output = '' + if gold_count > 0 : + output += render_badge_counter( + badge_level = Badge.GOLD, + badge_count = gold_count + ) + if silver_count > 0: + output += render_badge_counter( + badge_level = Badge.SILVER, + badge_count = silver_count + ) + if bronze_count > 0: + output += render_badge_counter( + badge_level = Badge.BRONZE, + badge_count = bronze_count + ) + return output + + +@register.simple_tag +def get_score_badge_by_details(rep, gold_count, silver_count, bronze_count): + output = render_reputation_counter(rep) + output += render_badge_counters(gold_count, silver_count, bronze_count) + return output + + +#this one is used for the header next to user profile and logout links +REP_TEMPLATE2 = '<a class="ab-nav-karma" href="%(karma_graph_url)s" ' \ + + ' title="%(karma_phrase)s">' \ + + '%(rep_word)s: %(reputation)s</a>' +BADGE_TEMPLATE2 = '<a class="ab-nav-badges" href="%(user_badges_url)s">' \ + + '%(badge_counters)s</a>' +@register.simple_tag +def get_long_score_and_badge_report(user): + profile_url = user.get_absolute_url() + karma_graph_url = profile_url + '?sort=reputation' + karma_phrase = _('your karma is %(reputation)s') \ + % { 'reputation': user.reputation } + + output = REP_TEMPLATE2 % { + 'repword': _('reputation points'), + 'karma_graph_url': karma_graph_url, + 'reputation': user.reputation, + 'karma_phrase': karma_phrase, + 'rep_word': _('reputation points'), + } + + badge_counters = render_badge_counters(user.gold, user.silver, user.bronze) + + if badge_counters != '': + output += ' ' + BADGE_TEMPLATE2 % { + 'user_badges_url': profile_url + '#badges', + 'badge_counters': _('badges: ') + badge_counters + } + return output + + +@register.simple_tag +def get_score_badge(user): + return get_score_badge_by_details( + user.reputation, + user.gold, + user.silver, + user.bronze + ) + + @register.simple_tag def get_user_vote_image(dic, key, arrow): if dic.has_key(key): |