8 from django import template
9 from django.utils.encoding import smart_unicode
10 from django.utils.safestring import mark_safe
11 from forum.const import *
12 from forum.models import Question, Answer, QuestionRevision, AnswerRevision
13 from django.utils.translation import ugettext as _
14 from django.utils.translation import ungettext
15 from django.utils import simplejson
16 from django.conf import settings
17 from django.template.defaulttags import url as default_url
18 from forum import skins
20 register = template.Library()
22 GRAVATAR_TEMPLATE = ('<img class="gravatar" width="%(size)s" height="%(size)s" '
23 'src="http://www.gravatar.com/avatar/%(gravatar_hash)s'
24 '?s=%(size)s&d=identicon&r=PG" '
25 'alt="%(username)s\'s gravatar image" />')
28 def gravatar(user, size):
30 Creates an ``<img>`` for a user's Gravatar with a given size.
32 This tag can accept a User object, or a dict containing the
36 gravatar = user['gravatar']
37 username = user['username']
38 except (TypeError, AttributeError, KeyError):
39 gravatar = user.gravatar
40 username = user.username
41 return mark_safe(GRAVATAR_TEMPLATE % {
43 'gravatar_hash': gravatar,
44 'username': template.defaultfilters.urlencode(username),
50 def tag_font_size(max_size, min_size, current_size):
52 do a logarithmic mapping calcuation for a proper size for tagging cloud
53 Algorithm from http://blogs.dekoh.com/dev/2007/10/29/choosing-a-good-font-size-variation-algorithm-for-your-tag-cloud/
55 #avoid invalid calculation
59 weight = (math.log10(current_size) - math.log10(min_size)) / (math.log10(max_size) - math.log10(min_size))
62 return MIN_FONTSIZE + round((MAX_FONTSIZE - MIN_FONTSIZE) * weight)
65 LEADING_PAGE_RANGE_DISPLAYED = TRAILING_PAGE_RANGE_DISPLAYED = 5
66 LEADING_PAGE_RANGE = TRAILING_PAGE_RANGE = 4
67 NUM_PAGES_OUTSIDE_RANGE = 1
69 @register.inclusion_tag("paginator.html")
70 def cnprog_paginator(context):
73 Inspired from http://blog.localkinegrinds.com/2007/09/06/digg-style-pagination-in-django/
75 if (context["is_paginated"]):
76 " Initialize variables "
77 in_leading_range = in_trailing_range = False
78 pages_outside_leading_range = pages_outside_trailing_range = range(0)
80 if (context["pages"] <= LEADING_PAGE_RANGE_DISPLAYED):
81 in_leading_range = in_trailing_range = True
82 page_numbers = [n for n in range(1, context["pages"] + 1) if n > 0 and n <= context["pages"]]
83 elif (context["page"] <= LEADING_PAGE_RANGE):
84 in_leading_range = True
85 page_numbers = [n for n in range(1, LEADING_PAGE_RANGE_DISPLAYED + 1) if n > 0 and n <= context["pages"]]
86 pages_outside_leading_range = [n + context["pages"] for n in range(0, -NUM_PAGES_OUTSIDE_RANGE, -1)]
87 elif (context["page"] > context["pages"] - TRAILING_PAGE_RANGE):
88 in_trailing_range = True
89 page_numbers = [n for n in range(context["pages"] - TRAILING_PAGE_RANGE_DISPLAYED + 1, context["pages"] + 1) if n > 0 and n <= context["pages"]]
90 pages_outside_trailing_range = [n + 1 for n in range(0, NUM_PAGES_OUTSIDE_RANGE)]
92 page_numbers = [n for n in range(context["page"] - ADJACENT_PAGES, context["page"] + ADJACENT_PAGES + 1) if n > 0 and n <= context["pages"]]
93 pages_outside_leading_range = [n + context["pages"] for n in range(0, -NUM_PAGES_OUTSIDE_RANGE, -1)]
94 pages_outside_trailing_range = [n + 1 for n in range(0, NUM_PAGES_OUTSIDE_RANGE)]
96 extend_url = context.get('extend_url', '')
98 "base_url": context["base_url"],
99 "is_paginated": context["is_paginated"],
100 "previous": context["previous"],
101 "has_previous": context["has_previous"],
102 "next": context["next"],
103 "has_next": context["has_next"],
104 "page": context["page"],
105 "pages": context["pages"],
106 "page_numbers": page_numbers,
107 "in_leading_range" : in_leading_range,
108 "in_trailing_range" : in_trailing_range,
109 "pages_outside_leading_range": pages_outside_leading_range,
110 "pages_outside_trailing_range": pages_outside_trailing_range,
111 "extend_url" : extend_url
114 @register.inclusion_tag("pagesize.html")
115 def cnprog_pagesize(context):
117 display the pagesize selection boxes for paginator
119 if (context["is_paginated"]):
121 "base_url": context["base_url"],
122 "pagesize" : context["pagesize"],
123 "is_paginated": context["is_paginated"]
126 @register.inclusion_tag("post_contributor_info.html")
127 def post_contributor_info(post,contributor_type='original_author'):
128 """contributor_type: original_author|last_updater
130 if isinstance(post,Question):
131 post_type = 'question'
132 elif isinstance(post,Answer):
134 elif isinstance(post,AnswerRevision) or isinstance(post,QuestionRevision):
135 post_type = 'revision'
138 'post_type':post_type,
139 'wiki_on':settings.WIKI_ON,
140 'contributor_type':contributor_type
144 def get_score_badge(user):
145 BADGE_TEMPLATE = '<span class="score" title="%(reputation)s %(reputationword)s">%(reputation)s</span>'
147 BADGE_TEMPLATE = '%s%s' % (BADGE_TEMPLATE, '<span title="%(gold)s %(badgesword)s">'
148 '<span class="badge1">●</span>'
149 '<span class="badgecount">%(gold)s</span>'
152 BADGE_TEMPLATE = '%s%s' % (BADGE_TEMPLATE, '<span title="%(silver)s %(badgesword)s">'
153 '<span class="silver">●</span>'
154 '<span class="badgecount">%(silver)s</span>'
157 BADGE_TEMPLATE = '%s%s' % (BADGE_TEMPLATE, '<span title="%(bronze)s %(badgesword)s">'
158 '<span class="bronze">●</span>'
159 '<span class="badgecount">%(bronze)s</span>'
161 BADGE_TEMPLATE = smart_unicode(BADGE_TEMPLATE, encoding='utf-8', strings_only=False, errors='strict')
162 return mark_safe(BADGE_TEMPLATE % {
163 'reputation' : user.reputation,
165 'silver' : user.silver,
166 'bronze' : user.bronze,
167 'badgesword' : _('badges'),
168 'reputationword' : _('reputation points'),
172 def get_score_badge_by_details(rep, gold, silver, bronze):
173 BADGE_TEMPLATE = '<span class="reputation-score" title="%(reputation)s %(repword)s">%(reputation)s</span>'
175 BADGE_TEMPLATE = '%s%s' % (BADGE_TEMPLATE, '<span title="%(gold)s %(badgeword)s">'
176 '<span class="badge1">●</span>'
177 '<span class="badgecount">%(gold)s</span>'
180 BADGE_TEMPLATE = '%s%s' % (BADGE_TEMPLATE, '<span title="%(silver)s %(badgeword)s">'
181 '<span class="badge2">●</span>'
182 '<span class="badgecount">%(silver)s</span>'
185 BADGE_TEMPLATE = '%s%s' % (BADGE_TEMPLATE, '<span title="%(bronze)s %(badgeword)s">'
186 '<span class="badge3">●</span>'
187 '<span class="badgecount">%(bronze)s</span>'
189 BADGE_TEMPLATE = smart_unicode(BADGE_TEMPLATE, encoding='utf-8', strings_only=False, errors='strict')
190 return mark_safe(BADGE_TEMPLATE % {
195 'repword' : _('reputation points'),
196 'badgeword' : _('badges'),
200 def get_age(birthday):
201 current_time = datetime.datetime(*time.localtime()[0:6])
203 month = birthday.month
205 diff = current_time - datetime.datetime(year,month,day,0,0,0)
206 return diff.days / 365
209 def get_total_count(up_count, down_count):
210 return up_count + down_count
213 def format_number(value):
214 strValue = str(value)
215 if len(strValue) <= 3:
219 pattern = re.compile('(-?\d+)(\d{3})')
220 m = re.match(pattern, strValue)
224 result = ',' + second + result
225 strValue = first + ',' + second
226 m = re.match(pattern, strValue)
227 return first + result
230 def convert2tagname_list(question):
231 question['tagnames'] = [name for name in question['tagnames'].split(u' ')]
235 def diff_date(date, limen=2):
236 now = datetime.datetime.now()#datetime(*time.localtime()[0:6])#???
239 hours = int(diff.seconds/3600)
240 minutes = int(diff.seconds/60)
243 if date.year == now.year:
244 return date.strftime("%b %d at %H:%M")
246 return date.strftime("%b %d '%y at %H:%M")
248 return _('2 days ago')
250 return _('yesterday')
252 return ungettext('%(hr)d hour ago','%(hr)d hours ago',hours) % {'hr':hours}
254 return ungettext('%(min)d min ago','%(min)d mins ago',minutes) % {'min':minutes}
257 def get_latest_changed_timestamp():
259 from time import localtime, strftime
261 root = settings.SITE_SRC_ROOT
265 '%s/templates' % root,
267 stamp = (path.getmtime(d) for d in dir)
269 timestr = strftime("%H:%M %b-%d-%Y %Z", localtime(latest))
276 url = skins.find_media_source(url)
278 url = '///' + settings.FORUM_SCRIPT_ALIAS + '/m/' + url
279 return posixpath.normpath(url) + '?v=%d' % settings.RESOURCE_REVISION
281 class ItemSeparatorNode(template.Node):
282 def __init__(self,separator):
283 sep = separator.strip()
284 if sep[0] == sep[-1] and sep[0] in ('\'','"'):
287 raise template.TemplateSyntaxError('separator in joinitems tag must be quoted')
289 def render(self,context):
292 class JoinItemListNode(template.Node):
293 def __init__(self,separator=ItemSeparatorNode("''"), items=()):
294 self.separator = separator
296 def render(self,context):
298 empty_re = re.compile(r'^\s*$')
299 for item in self.items:
300 bit = item.render(context)
301 if not empty_re.search(bit):
303 return self.separator.render(context).join(out)
305 @register.tag(name="joinitems")
306 def joinitems(parser,token):
308 tagname,junk,sep_token = token.split_contents()
310 raise template.TemplateSyntaxError("joinitems tag requires 'using \"separator html\"' parameters")
312 sep_node = ItemSeparatorNode(sep_token)
314 raise template.TemplateSyntaxError("joinitems tag requires 'using \"separator html\"' parameters")
317 nodelist.append(parser.parse(('separator','endjoinitems')))
318 next = parser.next_token()
319 if next.contents == 'endjoinitems':
322 return JoinItemListNode(separator=sep_node,items=nodelist)
324 class BlockMediaUrlNode(template.Node):
325 def __init__(self,nodelist):
326 self.items = nodelist
327 def render(self,context):
328 prefix = '///' + settings.FORUM_SCRIPT_ALIAS + 'm/'
332 for item in self.items:
333 url += item.render(context)
335 url = skins.find_media_source(url)
337 out = posixpath.normpath(url) + '?v=%d' % settings.RESOURCE_REVISION
338 return out.replace(' ','')
340 @register.tag(name='blockmedia')
341 def blockmedia(parser,token):
343 tagname = token.split_contents()
345 raise template.TemplateSyntaxError("blockmedia tag does not use arguments")
348 nodelist.append(parser.parse(('endblockmedia')))
349 next = parser.next_token()
350 if next.contents == 'endblockmedia':
352 return BlockMediaUrlNode(nodelist)
354 class FullUrlNode(template.Node):
355 def __init__(self, default_node):
356 self.default_node = default_node
358 def render(self, context):
359 domain = settings.APP_URL
360 #protocol = getattr(settings, "PROTOCOL", "http")
361 path = self.default_node.render(context)
362 return "%s%s" % (domain, path)
364 @register.tag(name='fullurl')
365 def fullurl(parser, token):
366 default_node = default_url(parser, token)
367 return FullUrlNode(default_node)
371 domain = settings.APP_URL
372 #protocol = getattr(settings, "PROTOCOL", "http")
374 return "%s%s" % (domain, path)
376 class UserVarNode(template.Node):
377 def __init__(self, tokens):
380 def render(self, context):
381 return "{{ %s }}" % self.tokens
383 @register.tag(name='user_var')
384 def user_var(parser, token):
385 tokens = " ".join(token.split_contents()[1:])
386 return UserVarNode(tokens)
389 class VariablesNode(template.Node):
390 def __init__(self, nodelist, var_name):
391 self.nodelist = nodelist
392 self.var_name = var_name
394 def render(self, context):
395 source = self.nodelist.render(context)
396 context[self.var_name] = simplejson.loads(source)
399 @register.tag(name='var')
400 def do_variables(parser, token):
402 tag_name, arg = token.contents.split(None, 1)
404 msg = '"%s" tag requires arguments' % token.contents.split()[0]
405 raise template.TemplateSyntaxError(msg)
406 m = re.search(r'as (\w+)', arg)
408 var_name, = m.groups()
410 msg = '"%s" tag had invalid arguments' % tag_name
411 raise template.TemplateSyntaxError(msg)
413 nodelist = parser.parse(('endvar',))
414 parser.delete_first_token()
415 return VariablesNode(nodelist, var_name)