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
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&d=%(default)s&r=%(rating)s" '
'alt="%(username)s\'s gravatar image" />')
# 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()
# 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>
""" % {
'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,
}
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:
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()
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')