]> git.openstreetmap.org Git - osqa.git/blobdiff - forum/templatetags/extra_tags.py
adding functionality to freeze accept rate to 100% for specific users
[osqa.git] / forum / templatetags / extra_tags.py
index 0a3c42109233e9cbc80135fe1af14b95170b5fac..b8d9f5cad3ce18c49189200856c6b2f5d764c7e7 100644 (file)
@@ -7,8 +7,9 @@ import re
 import logging
 import random
 from django import template
-from django.utils.encoding import smart_unicode
+from django.utils.encoding import smart_unicode, force_unicode, smart_str
 from django.utils.safestring import mark_safe
+from django.utils import dateformat
 from forum.models import Question, Answer, QuestionRevision, AnswerRevision, NodeRevision
 from django.utils.translation import ugettext as _
 from django.utils.translation import ungettext
@@ -23,7 +24,7 @@ from django.core.urlresolvers import reverse
 register = template.Library()
 
 GRAVATAR_TEMPLATE = ('<img class="gravatar" width="%(size)s" height="%(size)s" '
-'src="http://www.gravatar.com/avatar/%(gravatar_hash)s'
+'src="https://secure.gravatar.com/avatar/%(gravatar_hash)s'
 '?s=%(size)s&amp;d=%(default)s&amp;r=%(rating)s" '
 'alt="%(username)s\'s gravatar image" />')
 
@@ -78,6 +79,60 @@ def get_score_badge(user):
     'reputationword' : _('reputation points'),
     })
 
+# Usage: {% get_accept_rate node.author %}
+@register.simple_tag
+def get_accept_rate(user):
+    # If the Show Accept Rate feature is not activated this tag should return a blank string
+    if not settings.SHOW_USER_ACCEPT_RATE:
+        return ""
+
+    # Freeze accept rate for users
+    freeze_accept_rate_for_users_users = settings.FREEZE_ACCEPT_RATE_FOR.value
+    if user.username in list(freeze_accept_rate_for_users_users):
+        freeze = True
+    else:
+        freeze = False
+
+    # 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.
+    try:
+        accept_rate = (float(accepted_answers_count) / float(total_answers_count) * 100)
+    except ZeroDivisionError:
+        accept_rate = 0
+
+    # 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' :  smart_unicode(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') % smart_unicode(user.username)
+    # This are the only options. Otherwise there are no accepted answers at all.
+    else:
+        if freeze:
+            accept_rate_number_title = ""
+        else:
+            accept_rate_number_title = _('%s has no accepted answers') % smart_unicode(user.username)
+
+    html_output = """
+    <span title="%(accept_rate_title)s" class="accept_rate">%(accept_rate_label)s:</span>
+    <span title="%(accept_rate_number_title)s">%(accept_rate)d&#37;</span>
+    """ % {
+        'accept_rate_label' : _('accept rate'),
+        'accept_rate_title' : _('Rate of the user\'s accepted answers'),
+        'accept_rate' : 100 if freeze else 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):
@@ -99,11 +154,11 @@ def diff_date(date, limen=2):
     hours = int(diff.seconds/3600)
     minutes = int(diff.seconds/60)
 
-    if days > 2:
-        if date.year == now.year:
-            return date.strftime(_("%b %d at %H:%M").encode())
-        else:
-            return date.strftime(_("%b %d '%y at %H:%M").encode())
+    if date.year != now.year:
+        return dateformat.format(date, 'd M \'y, H:i')
+    elif days > 2:
+        return dateformat.format(date, 'd M, H:i')
+
     elif days == 2:
         return _('2 days ago')
     elif days == 1:
@@ -129,6 +184,24 @@ def media(url):
         url = url_prefix + url
         return url
 
+@register.simple_tag
+def get_tag_font_size(tag):
+    occurrences_of_current_tag = tag.used_count
+
+    # Occurrences count settings
+    min_occurs = int(settings.TAGS_CLOUD_MIN_OCCURS)
+    max_occurs = int(settings.TAGS_CLOUD_MAX_OCCURS)
+
+    # Font size settings
+    min_font_size = int(settings.TAGS_CLOUD_MIN_FONT_SIZE)
+    max_font_size = int(settings.TAGS_CLOUD_MAX_FONT_SIZE)
+
+    # Calculate the font size of the tag according to the occurrences count
+    weight = (math.log(occurrences_of_current_tag)-math.log(min_occurs))/(math.log(max_occurs)-math.log(min_occurs))
+    font_size_of_current_tag = min_font_size + int(math.floor((max_font_size-min_font_size)*weight))
+
+    return font_size_of_current_tag
+
 class ItemSeparatorNode(template.Node):
     def __init__(self, separator):
         sep = separator.strip()
@@ -236,13 +309,17 @@ class DeclareNode(template.Node):
                 d['os'] = os
                 d['html'] = html
                 d['reverse'] = reverse
+                d['settings'] = settings
+                d['smart_str'] = smart_str
+                d['smart_unicode'] = smart_unicode
+                d['force_unicode'] = force_unicode
                 for c in clist:
                     d.update(c)
                 try:
-                    context[m.group(1).strip()] = eval(m.group(3).strip(), d)
+                    command = m.group(3).strip()
+                    context[m.group(1).strip()] = eval(command, d)
                 except Exception, e:
                     logging.error("Error in declare tag, when evaluating: %s" % m.group(3).strip())
-                    raise
         return ''
 
 @register.tag(name='declare')