1 from datetime import datetime, timedelta
\r
3 from forum.models import Question, FavoriteQuestion, LikedComment
\r
4 from django.utils.translation import ugettext as _
\r
5 from django.core.urlresolvers import reverse
\r
6 from django import template
\r
8 register = template.Library()
\r
10 @register.inclusion_tag('question/vote_buttons.html')
\r
11 def vote_buttons(post, user):
\r
14 'post_type': (post.__class__ is Question) and 'question' or 'answer',
\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('question/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('question/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('question/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
73 controls.append(post_control(_('flag'), reverse('flag_post', kwargs={'post_type': post_type, 'id': post.id}),
\r
74 command=True, title=_("report as offensive (i.e containing spam, advertising, malicious text, etc.)")))
\r
76 if user.can_delete_post(post):
\r
77 controls.append(post_control(_('delete'), reverse('delete_post', kwargs={'post_type': post_type, 'id': post.id}),
\r
80 return {'controls': controls}
\r
82 @register.inclusion_tag('question/comments.html')
\r
83 def comments(post, user):
\r
84 all_comments = post.comments.filter(deleted=False).order_by('added_at')
\r
86 if len(all_comments) <= 5:
\r
87 top_scorers = all_comments
\r
89 top_scorers = sorted(all_comments, lambda c1, c2: c2.score - c1.score)[0:5]
\r
93 for c in all_comments:
\r
95 'can_delete': user.can_delete_comment(c),
\r
96 'can_like': user.can_like_comment(c),
\r
97 'can_edit': user.can_edit_comment(c)
\r
100 if c in top_scorers or c.is_reply_to(user):
\r
101 context['top_scorer'] = True
\r
104 if context['can_like']:
\r
106 LikedComment.active.get(comment=c, user=user)
\r
107 context['likes'] = True
\r
109 context['likes'] = False
\r
111 context['user'] = c.user
\r
112 context.update(dict(c.__dict__))
\r
113 comments.append(context)
\r
116 'comments': comments,
\r
118 'post_type': (post.__class__ is Question) and 'question' or 'answer',
\r
119 'can_comment': user.can_comment(post),
\r
121 'showing': showing,
\r
122 'total': len(all_comments),
\r