1 from datetime import datetime, timedelta
\r
3 from forum.models import Question, FavoriteQuestion
\r
4 from django.utils.translation import ugettext as _
\r
5 from django.core.urlresolvers import reverse
\r
6 from django import template
\r
7 from django.conf import settings
\r
9 register = template.Library()
\r
11 @register.inclusion_tag('node/vote_buttons.html')
\r
12 def vote_buttons(post, user):
\r
18 if user.is_authenticated():
\r
20 vote = post.votes.get(user=user)
\r
21 context['user_vote'] = vote.is_upvote() and 'up' or 'down'
\r
27 @register.inclusion_tag('node/accept_button.html')
\r
28 def accept_button(answer, user):
\r
30 'can_accept': user.is_authenticated() and user.can_accept_answer(answer),
\r
35 @register.inclusion_tag('node/favorite_mark.html')
\r
36 def favorite_mark(question, user):
\r
38 FavoriteQuestion.objects.get(question=question, user=user)
\r
43 favorite_count = question.favorited_by.count()
\r
45 return {'favorited': favorited, 'favorite_count': favorite_count, 'question': question}
\r
47 def post_control(text, url, command=False, title=""):
\r
48 return {'text': text, 'url': url, 'command': command, 'title': title}
\r
50 @register.inclusion_tag('node/post_controls.html')
\r
51 def post_controls(post, user):
\r
54 if user.is_authenticated():
\r
55 post_type = (post.__class__ is Question) and 'question' or 'answer'
\r
57 if post_type == "answer":
\r
58 controls.append(post_control(_('permanent link'), '#%d' % post.id, title=_("answer permanent link")))
\r
60 edit_url = reverse('edit_' + post_type, kwargs={'id': post.id})
\r
61 if user.can_edit_post(post):
\r
62 controls.append(post_control(_('edit'), edit_url))
\r
63 elif post_type == 'question' and user.can_retag_questions():
\r
64 controls.append(post_control(_('retag'), edit_url))
\r
66 if post_type == 'question':
\r
67 if post.closed and user.can_reopen_question(post):
\r
68 controls.append(post_control(_('reopen'), reverse('reopen', kwargs={'id': post.id})))
\r
69 elif not post.closed and user.can_close_question(post):
\r
70 controls.append(post_control(_('close'), reverse('close', kwargs={'id': post.id})))
\r
72 if user.can_flag_offensive(post):
\r
75 if user.can_view_offensive_flags(post):
\r
76 label = "%s (%d)" % (label, post.flaggeditems.count())
\r
78 controls.append(post_control(label, reverse('flag_post', kwargs={'id': post.id}),
\r
79 command=True, title=_("report as offensive (i.e containing spam, advertising, malicious text, etc.)")))
\r
81 if user.can_delete_post(post):
\r
82 controls.append(post_control(_('delete'), reverse('delete_post', kwargs={'id': post.id}),
\r
85 return {'controls': controls}
\r
87 @register.inclusion_tag('node/comments.html')
\r
88 def comments(post, user):
\r
89 all_comments = post.comments.filter(deleted=False).order_by('added_at')
\r
91 if len(all_comments) <= 5:
\r
92 top_scorers = all_comments
\r
94 top_scorers = sorted(all_comments, lambda c1, c2: c2.score - c1.score)[0:5]
\r
98 for c in all_comments:
\r
100 'can_delete': user.can_delete_comment(c),
\r
101 'can_like': user.can_like_comment(c),
\r
102 'can_edit': user.can_edit_comment(c)
\r
105 if c in top_scorers or c.is_reply_to(user):
\r
106 context['top_scorer'] = True
\r
109 if context['can_like']:
\r
111 c.votes.get(user=user)
\r
112 context['likes'] = True
\r
114 context['likes'] = False
\r
116 context['user'] = c.user
\r
117 context.update(dict(c.__dict__))
\r
118 comments.append(context)
\r
121 'comments': comments,
\r
123 'can_comment': user.can_comment(post),
\r
124 'max_length': settings.FORM_MAX_COMMENT_BODY,
\r
125 'showing': showing,
\r
126 'total': len(all_comments),
\r