]> git.openstreetmap.org Git - osqa.git/blob - forum/templatetags/extra_tags.py
making the get_score_badge method decoratable, creating a private instance of it...
[osqa.git] / forum / templatetags / extra_tags.py
1 import time
2 import os
3 import posixpath
4 import datetime
5 import math
6 import re
7 import logging
8 import random
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 django.utils import simplejson
17 from forum import settings
18 from django.template.defaulttags import url as default_url
19 from forum import skins
20 from forum.utils import html
21 from extra_filters import decorated_int
22 from django.core.urlresolvers import reverse
23
24 register = template.Library()
25
26 GRAVATAR_TEMPLATE = ('<img class="gravatar" width="%(size)s" height="%(size)s" '
27 'src="https://secure.gravatar.com/avatar/%(gravatar_hash)s'
28 '?s=%(size)s&amp;d=%(default)s&amp;r=%(rating)s" '
29 'alt="%(username)s\'s gravatar image" />')
30
31 @register.simple_tag
32 def gravatar(user, size):
33     try:
34         gravatar = user['gravatar']
35         username = user['username']
36     except (TypeError, AttributeError, KeyError):
37         gravatar = user.gravatar
38         username = user.username
39     return mark_safe(GRAVATAR_TEMPLATE % {
40     'size': size,
41     'gravatar_hash': gravatar,
42     'default': settings.GRAVATAR_DEFAULT_IMAGE,
43     'rating': settings.GRAVATAR_ALLOWED_RATING,
44     'username': template.defaultfilters.urlencode(username),
45     })
46
47
48 @register.simple_tag
49 def get_score_badge(user):
50     return _get_score_badge(user)
51
52 def _get_score_badge(user):
53     if user.is_suspended():
54         return _("(suspended)")
55
56     repstr = decorated_int(user.reputation, "")
57
58     BADGE_TEMPLATE = '<span class="score" title="%(reputation)s %(reputationword)s">%(repstr)s</span>'
59     if user.gold > 0 :
60         BADGE_TEMPLATE = '%s%s' % (BADGE_TEMPLATE, '<span title="%(gold)s %(badgesword)s">'
61         '<span class="badge1">&#9679;</span>'
62         '<span class="badgecount">%(gold)s</span>'
63         '</span>')
64     if user.silver > 0:
65         BADGE_TEMPLATE = '%s%s' % (BADGE_TEMPLATE, '<span title="%(silver)s %(badgesword)s">'
66         '<span class="silver">&#9679;</span>'
67         '<span class="badgecount">%(silver)s</span>'
68         '</span>')
69     if user.bronze > 0:
70         BADGE_TEMPLATE = '%s%s' % (BADGE_TEMPLATE, '<span title="%(bronze)s %(badgesword)s">'
71         '<span class="bronze">&#9679;</span>'
72         '<span class="badgecount">%(bronze)s</span>'
73         '</span>')
74     BADGE_TEMPLATE = smart_unicode(BADGE_TEMPLATE, encoding='utf-8', strings_only=False, errors='strict')
75     return mark_safe(BADGE_TEMPLATE % {
76     'reputation' : user.reputation,
77     'repstr': repstr,
78     'gold' : user.gold,
79     'silver' : user.silver,
80     'bronze' : user.bronze,
81     'badgesword' : _('badges'),
82     'reputationword' : _('reputation points'),
83     })
84
85 # Usage: {% get_accept_rate node.author %}
86 @register.simple_tag
87 def get_accept_rate(user):
88     # If the Show Accept Rate feature is not activated this tag should return a blank string
89     if not settings.SHOW_USER_ACCEPT_RATE:
90         return ""
91
92     # Freeze accept rate for users
93     freeze_accept_rate_for_users_users = settings.FREEZE_ACCEPT_RATE_FOR.value
94     if user.username in list(freeze_accept_rate_for_users_users):
95         freeze = True
96     else:
97         freeze = False
98
99     # We get the number of all user's answers.
100     total_answers_count = Answer.objects.filter(author=user).count()
101
102     # We get the number of the user's accepted answers.
103     accepted_answers_count = Answer.objects.filter(author=user, state_string__contains="(accepted)").count()
104
105     # In order to represent the accept rate in percentages we divide the number of the accepted answers to the
106     # total answers count and make a hundred multiplication.
107     try:
108         accept_rate = (float(accepted_answers_count) / float(total_answers_count) * 100)
109     except ZeroDivisionError:
110         accept_rate = 0
111
112     # If the user has more than one accepted answers the rate title will be in plural.
113     if accepted_answers_count > 1:
114         accept_rate_number_title = _('%(user)s has %(count)d accepted answers') % {
115             'user' :  smart_unicode(user.username),
116             'count' : int(accepted_answers_count)
117         }
118     # If the user has one accepted answer we'll be using singular.
119     elif accepted_answers_count == 1:
120         accept_rate_number_title = _('%s has one accepted answer') % smart_unicode(user.username)
121     # This are the only options. Otherwise there are no accepted answers at all.
122     else:
123         if freeze:
124             accept_rate_number_title = ""
125         else:
126             accept_rate_number_title = _('%s has no accepted answers') % smart_unicode(user.username)
127
128     html_output = """
129     <span title="%(accept_rate_title)s" class="accept_rate">%(accept_rate_label)s:</span>
130     <span title="%(accept_rate_number_title)s">%(accept_rate)d&#37;</span>
131     """ % {
132         'accept_rate_label' : _('accept rate'),
133         'accept_rate_title' : _('Rate of the user\'s accepted answers'),
134         'accept_rate' : 100 if freeze else int(accept_rate),
135         'accept_rate_number_title' : u'%s' % accept_rate_number_title,
136     }
137
138     return mark_safe(html_output)
139
140 @register.simple_tag
141 def get_age(birthday):
142     current_time = datetime.datetime(*time.localtime()[0:6])
143     year = birthday.year
144     month = birthday.month
145     day = birthday.day
146     diff = current_time - datetime.datetime(year, month, day, 0, 0, 0)
147     return diff.days / 365
148
149 @register.simple_tag
150 def diff_date(date, limen=2):
151     if not date:
152         return _('unknown')
153
154     now = datetime.datetime.now()
155     diff = now - date
156     days = diff.days
157     hours = int(diff.seconds/3600)
158     minutes = int(diff.seconds/60)
159
160     if date.year != now.year:
161         return dateformat.format(date, 'd M \'y, H:i')
162     elif days > 2:
163         return dateformat.format(date, 'd M, H:i')
164
165     elif days == 2:
166         return _('2 days ago')
167     elif days == 1:
168         return _('yesterday')
169     elif minutes >= 60:
170         return ungettext('%(hr)d ' + _("hour ago"), '%(hr)d ' + _("hours ago"), hours) % {'hr':hours}
171     elif diff.seconds >= 60:
172         return ungettext('%(min)d ' + _("min ago"), '%(min)d ' + _("mins ago"), minutes) % {'min':minutes}
173     else:
174         return ungettext('%(sec)d ' + _("sec ago"), '%(sec)d ' + _("secs ago"), diff.seconds) % {'sec':diff.seconds}
175
176 @register.simple_tag
177 def media(url):
178     url = skins.find_media_source(url)
179     if url:
180         # Create the URL prefix.
181         url_prefix = settings.FORCE_SCRIPT_NAME + '/m/'
182
183         # Make sure any duplicate forward slashes are replaced with a single
184         # forward slash.
185         url_prefix = re.sub("/+", "/", url_prefix)
186
187         url = url_prefix + url
188         return url
189
190 @register.simple_tag
191 def get_tag_font_size(tag):
192     occurrences_of_current_tag = tag.used_count
193
194     # Occurrences count settings
195     min_occurs = int(settings.TAGS_CLOUD_MIN_OCCURS)
196     max_occurs = int(settings.TAGS_CLOUD_MAX_OCCURS)
197
198     # Font size settings
199     min_font_size = int(settings.TAGS_CLOUD_MIN_FONT_SIZE)
200     max_font_size = int(settings.TAGS_CLOUD_MAX_FONT_SIZE)
201
202     # Calculate the font size of the tag according to the occurrences count
203     weight = (math.log(occurrences_of_current_tag)-math.log(min_occurs))/(math.log(max_occurs)-math.log(min_occurs))
204     font_size_of_current_tag = min_font_size + int(math.floor((max_font_size-min_font_size)*weight))
205
206     return font_size_of_current_tag
207
208 class ItemSeparatorNode(template.Node):
209     def __init__(self, separator):
210         sep = separator.strip()
211         if sep[0] == sep[-1] and sep[0] in ('\'', '"'):
212             sep = sep[1:-1]
213         else:
214             raise template.TemplateSyntaxError('separator in joinitems tag must be quoted')
215         self.content = sep
216
217     def render(self, context):
218         return self.content
219
220 class BlockMediaUrlNode(template.Node):
221     def __init__(self, nodelist):
222         self.items = nodelist
223
224     def render(self, context):
225         prefix = settings.APP_URL + 'm/'
226         url = ''
227         if self.items:
228             url += '/'
229         for item in self.items:
230             url += item.render(context)
231
232         url = skins.find_media_source(url)
233         url = prefix + url
234         out = url
235         return out.replace(' ', '')
236
237 @register.tag(name='blockmedia')
238 def blockmedia(parser, token):
239     try:
240         tagname = token.split_contents()
241     except ValueError:
242         raise template.TemplateSyntaxError("blockmedia tag does not use arguments")
243     nodelist = []
244     while True:
245         nodelist.append(parser.parse(('endblockmedia')))
246         next = parser.next_token()
247         if next.contents == 'endblockmedia':
248             break
249     return BlockMediaUrlNode(nodelist)
250
251
252 @register.simple_tag
253 def fullmedia(url):
254     domain = settings.APP_BASE_URL
255     #protocol = getattr(settings, "PROTOCOL", "http")
256     path = media(url)
257     return "%s%s" % (domain, path)
258
259
260 class SimpleVarNode(template.Node):
261     def __init__(self, name, value):
262         self.name = name
263         self.value = template.Variable(value)
264
265     def render(self, context):
266         context[self.name] = self.value.resolve(context)
267         return ''
268
269 class BlockVarNode(template.Node):
270     def __init__(self, name, block):
271         self.name = name
272         self.block = block
273
274     def render(self, context):
275         source = self.block.render(context)
276         context[self.name] = source.strip()
277         return ''
278
279
280 @register.tag(name='var')
281 def do_var(parser, token):
282     tokens = token.split_contents()[1:]
283
284     if not len(tokens) or not re.match('^\w+$', tokens[0]):
285         raise template.TemplateSyntaxError("Expected variable name")
286
287     if len(tokens) == 1:
288         nodelist = parser.parse(('endvar',))
289         parser.delete_first_token()
290         return BlockVarNode(tokens[0], nodelist)
291     elif len(tokens) == 3:
292         return SimpleVarNode(tokens[0], tokens[2])
293
294     raise template.TemplateSyntaxError("Invalid number of arguments")
295
296 class DeclareNode(template.Node):
297     dec_re = re.compile('^\s*(\w+)\s*(:?=)\s*(.*)$')
298
299     def __init__(self, block):
300         self.block = block
301
302     def render(self, context):
303         source = self.block.render(context)
304
305         for line in source.splitlines():
306             m = self.dec_re.search(line)
307             if m:
308                 clist = list(context)
309                 clist.reverse()
310                 d = {}
311                 d['_'] = _
312                 d['os'] = os
313                 d['html'] = html
314                 d['reverse'] = reverse
315                 d['settings'] = settings
316                 d['smart_str'] = smart_str
317                 d['smart_unicode'] = smart_unicode
318                 d['force_unicode'] = force_unicode
319                 for c in clist:
320                     d.update(c)
321                 try:
322                     command = m.group(3).strip()
323                     context[m.group(1).strip()] = eval(command, d)
324                 except Exception, e:
325                     logging.error("Error in declare tag, when evaluating: %s" % m.group(3).strip())
326         return ''
327
328 @register.tag(name='declare')
329 def do_declare(parser, token):
330     nodelist = parser.parse(('enddeclare',))
331     parser.delete_first_token()
332     return DeclareNode(nodelist)
333
334 # Usage: {% random 1 999 %}
335 # Generates random number in the template
336 class RandomNumberNode(template.Node):
337     # We get the limiting numbers
338     def __init__(self, int_from, int_to):
339         self.int_from = int(int_from)
340         self.int_to = int(int_to)
341
342     # We generate the random number using the standard python interface
343     def render(self, context):
344         return str(random.randint(self.int_from, self.int_to))
345
346 @register.tag(name="random")
347 def random_number(parser, token):
348     # Try to get the limiting numbers from the token
349     try:
350         tag_name, int_from, int_to = token.split_contents()
351     except ValueError:
352         # If we had no success -- raise an exception
353         raise template.TemplateSyntaxError, "%r tag requires exactly two arguments" % token.contents.split()[0]
354
355     # Call the random Node
356     return RandomNumberNode(int_from, int_to)