]> 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 6e5d6e64abc87c79b7f96a4efb89770354f80de6..b8d9f5cad3ce18c49189200856c6b2f5d764c7e7 100644 (file)
@@ -24,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" />')
 
@@ -82,6 +82,17 @@ def get_score_badge(user):
 # 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()
 
@@ -98,15 +109,18 @@ def get_accept_rate(user):
     # 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,
+            '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') % user.username
+        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:
-        accept_rate_number_title = _('%s has no accepted answers') % smart_unicode(user.username)
+        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>
@@ -114,7 +128,7 @@ def get_accept_rate(user):
     """ % {
         'accept_rate_label' : _('accept rate'),
         'accept_rate_title' : _('Rate of the user\'s accepted answers'),
-        'accept_rate' : int(accept_rate),
+        'accept_rate' : 100 if freeze else int(accept_rate),
         'accept_rate_number_title' : u'%s' % accept_rate_number_title,
     }
 
@@ -136,7 +150,6 @@ def diff_date(date, limen=2):
 
     now = datetime.datetime.now()
     diff = now - date
-    years = diff.years
     days = diff.days
     hours = int(diff.seconds/3600)
     minutes = int(diff.seconds/60)
@@ -171,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()
@@ -278,6 +309,7 @@ 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
@@ -288,7 +320,6 @@ class DeclareNode(template.Node):
                     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')