From: jordan Date: Sun, 10 Apr 2011 15:21:07 +0000 (+0000) Subject: #OSQA-315, implementing the accept rate feature that shows the rate of the accepted... X-Git-Tag: live~354 X-Git-Url: https://git.openstreetmap.org./osqa.git/commitdiff_plain/37cb83c0dafbfa9a656ea5823fe4abb0c47e7c96 #OSQA-315, implementing the accept rate feature that shows the rate of the accepted answers count for a certain user in percentages. git-svn-id: http://svn.osqa.net/svnroot/osqa/trunk@958 0cfe37f9-358a-4d5e-be75-b63607b5c754 --- diff --git a/forum/skins/default/media/style/style.css b/forum/skins/default/media/style/style.css index deab17b..652bd9f 100644 --- a/forum/skins/default/media/style/style.css +++ b/forum/skins/default/media/style/style.css @@ -1111,7 +1111,7 @@ ins { color: #CC9933; } -.score { +.score, .accept_rate { color: #333333; font-size: 110%; font-weight: bold; diff --git a/forum/skins/default/templates/node/contributors_info.html b/forum/skins/default/templates/node/contributors_info.html index 3ed461b..28bf49b 100644 --- a/forum/skins/default/templates/node/contributors_info.html +++ b/forum/skins/default/templates/node/contributors_info.html @@ -1,4 +1,4 @@ -{% load extra_tags %} +{% load extra_tags i18n %}

{{ node_verb }} @@ -6,7 +6,8 @@

{% gravatar node.author 32 %}

{{ node.author.decorated_name }}
- {% get_score_badge node.author %}

+ {% get_score_badge node.author %}
+ {% get_accept_rate node.author %}

{% if node.last_edited %}
diff --git a/forum/templatetags/extra_tags.py b/forum/templatetags/extra_tags.py index 0a3c421..e34e996 100644 --- a/forum/templatetags/extra_tags.py +++ b/forum/templatetags/extra_tags.py @@ -78,6 +78,43 @@ def get_score_badge(user): 'reputationword' : _('reputation points'), }) +# Usage: {% get_accept_rate node.author %} +@register.simple_tag +def get_accept_rate(user): + # We get the number of all user's answers. + total_answers_count = Answer.objects.filter(author=user).count() + + # We get the number of the user's accepted answers. + accepted_answers_count = Answer.objects.filter(author=user, state_string__contains="(accepted)").count() + + # In order to represent the accept rate in percentages we divide the number of the accepted answers to the + # total answers count and make a hundred multiplication. + accept_rate = (float(accepted_answers_count) / float(total_answers_count) * 100) + + # If the user has more than one accepted answers the rate title will be in plural. + if accepted_answers_count > 1: + accept_rate_number_title = _('%(user)s has %(count)d accepted answers') % { + 'user' : user.username, + 'count' : int(accepted_answers_count) + } + # If the user has one accepted answer we'll be using singular. + elif accepted_answers_count == 1: + accept_rate_number_title = _('%s has one accepted answer') % user.username + # This are the only options. Otherwise there are no accepted answers at all. + else: + accept_rate_number_title = _('%s has no accepted answers') % user.username + + html_output = """ + %(accept_rate_label)s: + %(accept_rate)d% + """ % { + 'accept_rate_label' : _('accept rate'), + 'accept_rate_title' : _('Rate of the user\'s accepted answers'), + 'accept_rate' : int(accept_rate), + 'accept_rate_number_title' : u'%s' % accept_rate_number_title, + } + + return mark_safe(html_output) @register.simple_tag def get_age(birthday):