9 from django import template
10 from django.utils.encoding import smart_unicode, force_unicode, smart_str
11 from django.utils.safestring import mark_safe
12 from django.utils import dateformat
13 from forum.models import Question, Answer, QuestionRevision, AnswerRevision, NodeRevision
14 from django.utils.translation import ugettext as _
15 from django.utils.translation import ungettext
16 from forum import settings
17 from django.template.defaulttags import url as default_url
18 from forum import skins
19 from forum.utils import html
20 from extra_filters import decorated_int
21 from django.core.urlresolvers import reverse
23 register = template.Library()
25 GRAVATAR_TEMPLATE = ('<img class="gravatar" width="%(size)s" height="%(size)s" '
26 'src="https://secure.gravatar.com/avatar/%(gravatar_hash)s'
27 '?s=%(size)s&d=%(default)s&r=%(rating)s" '
28 'alt="%(username)s\'s gravatar image" />')
31 def gravatar(user, size):
33 gravatar = user['gravatar']
34 username = user['username']
35 except (TypeError, AttributeError, KeyError):
36 gravatar = user.gravatar
37 username = user.username
38 return mark_safe(GRAVATAR_TEMPLATE % {
40 'gravatar_hash': gravatar,
41 'default': settings.GRAVATAR_DEFAULT_IMAGE,
42 'rating': settings.GRAVATAR_ALLOWED_RATING,
43 'username': template.defaultfilters.urlencode(username),
48 def get_score_badge(user):
49 return _get_score_badge(user)
51 def _get_score_badge(user):
52 if user.is_suspended():
53 return _("(suspended)")
55 repstr = decorated_int(user.reputation, "")
57 BADGE_TEMPLATE = '<span class="score" title="%(reputation)s %(reputationword)s">%(repstr)s</span>'
59 BADGE_TEMPLATE = '%s%s' % (BADGE_TEMPLATE, '<span title="%(gold)s %(badgesword)s">'
60 '<span class="badge1">●</span>'
61 '<span class="badgecount">%(gold)s</span>'
64 BADGE_TEMPLATE = '%s%s' % (BADGE_TEMPLATE, '<span title="%(silver)s %(badgesword)s">'
65 '<span class="silver">●</span>'
66 '<span class="badgecount">%(silver)s</span>'
69 BADGE_TEMPLATE = '%s%s' % (BADGE_TEMPLATE, '<span title="%(bronze)s %(badgesword)s">'
70 '<span class="bronze">●</span>'
71 '<span class="badgecount">%(bronze)s</span>'
73 BADGE_TEMPLATE = smart_unicode(BADGE_TEMPLATE, encoding='utf-8', strings_only=False, errors='strict')
74 return mark_safe(BADGE_TEMPLATE % {
75 'reputation' : user.reputation,
78 'silver' : user.silver,
79 'bronze' : user.bronze,
80 'badgesword' : _('badges'),
81 'reputationword' : _('reputation points'),
84 # Usage: {% get_accept_rate node.author %}
86 def get_accept_rate(user):
87 # If the Show Accept Rate feature is not activated this tag should return a blank string
88 if not settings.SHOW_USER_ACCEPT_RATE:
91 # Freeze accept rate for users
92 freeze_accept_rate_for_users_users = settings.FREEZE_ACCEPT_RATE_FOR.value
93 if user.username in list(freeze_accept_rate_for_users_users):
98 # We get the number of all user's answers.
99 total_answers_count = Answer.objects.filter(author=user).count()
101 # We get the number of the user's accepted answers.
102 accepted_answers_count = Answer.objects.filter(author=user, state_string__contains="(accepted)").count()
104 # In order to represent the accept rate in percentages we divide the number of the accepted answers to the
105 # total answers count and make a hundred multiplication.
107 accept_rate = (float(accepted_answers_count) / float(total_answers_count) * 100)
108 except ZeroDivisionError:
111 # If the user has more than one accepted answers the rate title will be in plural.
112 if accepted_answers_count > 1:
113 accept_rate_number_title = _('%(user)s has %(count)d accepted answers') % {
114 'user' : smart_unicode(user.username),
115 'count' : int(accepted_answers_count)
117 # If the user has one accepted answer we'll be using singular.
118 elif accepted_answers_count == 1:
119 accept_rate_number_title = _('%s has one accepted answer') % smart_unicode(user.username)
120 # This are the only options. Otherwise there are no accepted answers at all.
123 accept_rate_number_title = ""
125 accept_rate_number_title = _('%s has no accepted answers') % smart_unicode(user.username)
128 <span title="%(accept_rate_title)s" class="accept_rate">%(accept_rate_label)s:</span>
129 <span title="%(accept_rate_number_title)s">%(accept_rate)d%</span>
131 'accept_rate_label' : _('accept rate'),
132 'accept_rate_title' : _('Rate of the user\'s accepted answers'),
133 'accept_rate' : 100 if freeze else int(accept_rate),
134 'accept_rate_number_title' : u'%s' % accept_rate_number_title,
137 return mark_safe(html_output)
140 def get_age(birthday):
141 current_time = datetime.datetime(*time.localtime()[0:6])
143 month = birthday.month
145 diff = current_time - datetime.datetime(year, month, day, 0, 0, 0)
146 return diff.days / 365
149 def diff_date(date, limen=2):
153 now = datetime.datetime.now()
156 hours = int(diff.seconds/3600)
157 minutes = int(diff.seconds/60)
159 if date.year != now.year:
160 return dateformat.format(date, 'd M \'y, H:i')
162 return dateformat.format(date, 'd M, H:i')
165 return _('2 days ago')
167 return _('yesterday')
169 return ungettext('%(hr)d ' + _("hour ago"), '%(hr)d ' + _("hours ago"), hours) % {'hr':hours}
170 elif diff.seconds >= 60:
171 return ungettext('%(min)d ' + _("min ago"), '%(min)d ' + _("mins ago"), minutes) % {'min':minutes}
173 return ungettext('%(sec)d ' + _("sec ago"), '%(sec)d ' + _("secs ago"), diff.seconds) % {'sec':diff.seconds}
177 url = skins.find_media_source(url)
179 # Create the URL prefix.
180 url_prefix = settings.FORCE_SCRIPT_NAME + '/m/'
182 # Make sure any duplicate forward slashes are replaced with a single
184 url_prefix = re.sub("/+", "/", url_prefix)
186 url = url_prefix + url
190 def get_tag_font_size(tag):
191 occurrences_of_current_tag = tag.used_count
193 # Occurrences count settings
194 min_occurs = int(settings.TAGS_CLOUD_MIN_OCCURS)
195 max_occurs = int(settings.TAGS_CLOUD_MAX_OCCURS)
198 min_font_size = int(settings.TAGS_CLOUD_MIN_FONT_SIZE)
199 max_font_size = int(settings.TAGS_CLOUD_MAX_FONT_SIZE)
201 # Calculate the font size of the tag according to the occurrences count
202 weight = (math.log(occurrences_of_current_tag)-math.log(min_occurs))/(math.log(max_occurs)-math.log(min_occurs))
203 font_size_of_current_tag = min_font_size + int(math.floor((max_font_size-min_font_size)*weight))
205 return font_size_of_current_tag
207 class ItemSeparatorNode(template.Node):
208 def __init__(self, separator):
209 sep = separator.strip()
210 if sep[0] == sep[-1] and sep[0] in ('\'', '"'):
213 raise template.TemplateSyntaxError('separator in joinitems tag must be quoted')
216 def render(self, context):
219 class BlockMediaUrlNode(template.Node):
220 def __init__(self, nodelist):
221 self.items = nodelist
223 def render(self, context):
224 prefix = settings.APP_URL + 'm/'
228 for item in self.items:
229 url += item.render(context)
231 url = skins.find_media_source(url)
234 return out.replace(' ', '')
236 @register.tag(name='blockmedia')
237 def blockmedia(parser, token):
239 tagname = token.split_contents()
241 raise template.TemplateSyntaxError("blockmedia tag does not use arguments")
244 nodelist.append(parser.parse(('endblockmedia')))
245 next = parser.next_token()
246 if next.contents == 'endblockmedia':
248 return BlockMediaUrlNode(nodelist)
253 domain = settings.APP_BASE_URL
254 #protocol = getattr(settings, "PROTOCOL", "http")
256 return "%s%s" % (domain, path)
259 class SimpleVarNode(template.Node):
260 def __init__(self, name, value):
262 self.value = template.Variable(value)
264 def render(self, context):
265 context[self.name] = self.value.resolve(context)
268 class BlockVarNode(template.Node):
269 def __init__(self, name, block):
273 def render(self, context):
274 source = self.block.render(context)
275 context[self.name] = source.strip()
279 @register.tag(name='var')
280 def do_var(parser, token):
281 tokens = token.split_contents()[1:]
283 if not len(tokens) or not re.match('^\w+$', tokens[0]):
284 raise template.TemplateSyntaxError("Expected variable name")
287 nodelist = parser.parse(('endvar',))
288 parser.delete_first_token()
289 return BlockVarNode(tokens[0], nodelist)
290 elif len(tokens) == 3:
291 return SimpleVarNode(tokens[0], tokens[2])
293 raise template.TemplateSyntaxError("Invalid number of arguments")
295 class DeclareNode(template.Node):
296 dec_re = re.compile('^\s*(\w+)\s*(:?=)\s*(.*)$')
298 def __init__(self, block):
301 def render(self, context):
302 source = self.block.render(context)
304 for line in source.splitlines():
305 m = self.dec_re.search(line)
307 clist = list(context)
313 d['reverse'] = reverse
314 d['settings'] = settings
315 d['smart_str'] = smart_str
316 d['smart_unicode'] = smart_unicode
317 d['force_unicode'] = force_unicode
321 command = m.group(3).strip()
322 context[m.group(1).strip()] = eval(command, d)
324 logging.error("Error in declare tag, when evaluating: %s" % m.group(3).strip())
327 @register.tag(name='declare')
328 def do_declare(parser, token):
329 nodelist = parser.parse(('enddeclare',))
330 parser.delete_first_token()
331 return DeclareNode(nodelist)
333 # Usage: {% random 1 999 %}
334 # Generates random number in the template
335 class RandomNumberNode(template.Node):
336 # We get the limiting numbers
337 def __init__(self, int_from, int_to):
338 self.int_from = int(int_from)
339 self.int_to = int(int_to)
341 # We generate the random number using the standard python interface
342 def render(self, context):
343 return str(random.randint(self.int_from, self.int_to))
345 @register.tag(name="random")
346 def random_number(parser, token):
347 # Try to get the limiting numbers from the token
349 tag_name, int_from, int_to = token.split_contents()
351 # If we had no success -- raise an exception
352 raise template.TemplateSyntaxError, "%r tag requires exactly two arguments" % token.contents.split()[0]
354 # Call the random Node
355 return RandomNumberNode(int_from, int_to)