1 from datetime import datetime, timedelta
\r
3 from forum.models import Question, Action
\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 forum.actions import *
\r
8 from forum import settings
\r
10 register = template.Library()
\r
12 @register.inclusion_tag('node/vote_buttons.html')
\r
13 def vote_buttons(post, user):
\r
14 context = dict(post=post, user_vote='none')
\r
16 if user.is_authenticated():
\r
17 context['user_vote'] = {1: 'up', -1: 'down', None: 'none'}[VoteAction.get_for(user, post)]
\r
21 @register.inclusion_tag('node/accept_button.html')
\r
22 def accept_button(answer, user):
\r
24 'can_accept': user.is_authenticated() and user.can_accept_answer(answer),
\r
29 @register.inclusion_tag('node/favorite_mark.html')
\r
30 def favorite_mark(question, user):
\r
32 FavoriteAction.objects.get(canceled=False, node=question, user=user)
\r
37 return {'favorited': favorited, 'favorite_count': question.favorite_count, 'question': question}
\r
39 def post_control(text, url, command=False, withprompt=False, confirm=False, title=""):
\r
40 classes = (command and "ajax-command" or " ") + (withprompt and " withprompt" or " ") + (confirm and " confirm" or " ")
\r
41 return {'text': text, 'url': url, 'classes': classes, 'title': title}
\r
43 @register.inclusion_tag('node/post_controls.html')
\r
44 def post_controls(post, user):
\r
48 if user.is_authenticated():
\r
49 post_type = (post.__class__ is Question) and 'question' or 'answer'
\r
51 if post_type == "answer":
\r
52 controls.append(post_control(_('permanent link'), '#%d' % post.id, title=_("answer permanent link")))
\r
54 edit_url = reverse('edit_' + post_type, kwargs={'id': post.id})
\r
55 if user.can_edit_post(post):
\r
56 controls.append(post_control(_('edit'), edit_url))
\r
57 elif post_type == 'question' and user.can_retag_questions():
\r
58 controls.append(post_control(_('retag'), edit_url))
\r
60 if post_type == 'question':
\r
61 if post.nis.closed and user.can_reopen_question(post):
\r
62 controls.append(post_control(_('reopen'), reverse('reopen', kwargs={'id': post.id}), command=True))
\r
63 elif not post.nis.closed and user.can_close_question(post):
\r
64 controls.append(post_control(_('close'), reverse('close', kwargs={'id': post.id}), command=True, withprompt=True))
\r
66 if user.can_flag_offensive(post):
\r
69 if user.can_view_offensive_flags(post):
\r
70 label = "%s (%d)" % (label, post.flag_count)
\r
72 controls.append(post_control(label, reverse('flag_post', kwargs={'id': post.id}),
\r
73 command=True, withprompt=True, title=_("report as offensive (i.e containing spam, advertising, malicious text, etc.)")))
\r
75 if user.can_delete_post(post):
\r
76 if post.nis.deleted:
\r
77 controls.append(post_control(_('undelete'), reverse('delete_post', kwargs={'id': post.id}),
\r
78 command=True, confirm=True))
\r
80 controls.append(post_control(_('delete'), reverse('delete_post', kwargs={'id': post.id}),
\r
81 command=True, confirm=True))
\r
83 if (not post.nis.wiki) and user.can_wikify(post):
\r
84 menu.append(post_control(_('mark as community wiki'), reverse('wikify', kwargs={'id': post.id}),
\r
85 command=True, confirm=True))
\r
87 if post.node_type == "answer" and user.can_convert_to_comment(post):
\r
88 menu.append(post_control(_('convert to comment'), reverse('convert_to_comment', kwargs={'id': post.id}),
\r
89 command=True, withprompt=True))
\r
91 return {'controls': controls, 'menu': menu, 'post': post, 'user': user}
\r
93 @register.inclusion_tag('node/comments.html')
\r
94 def comments(post, user):
\r
95 all_comments = post.comments.filter_state(deleted=False).order_by('added_at')
\r
97 if len(all_comments) <= 5:
\r
98 top_scorers = all_comments
\r
100 top_scorers = sorted(all_comments, lambda c1, c2: c2.score - c1.score)[0:5]
\r
104 for c in all_comments:
\r
106 'can_delete': user.can_delete_comment(c),
\r
107 'can_like': user.can_like_comment(c),
\r
108 'can_edit': user.can_edit_comment(c)
\r
111 if c in top_scorers or c.is_reply_to(user):
\r
112 context['top_scorer'] = True
\r
115 if context['can_like']:
\r
116 context['likes'] = VoteAction.get_for(user, c) == 1
\r
118 context['user'] = c.user
\r
119 context['comment'] = c.comment
\r
120 context.update(dict(c.__dict__))
\r
121 comments.append(context)
\r
124 'comments': comments,
\r
126 'can_comment': user.can_comment(post),
\r
127 'max_length': settings.FORM_MAX_COMMENT_BODY,
\r
128 'min_length': settings.FORM_MIN_COMMENT_BODY,
\r
129 'show_gravatar': settings.FORM_GRAVATAR_IN_COMMENTS,
\r
130 'showing': showing,
\r
131 'total': len(all_comments),
\r
136 @register.inclusion_tag("node/contributors_info.html")
\r
137 def contributors_info(node):
\r
139 'node_verb': (node.node_type == "question") and _("asked") or (
\r
140 (node.node_type == "answer") and _("answered") or _("posted")),
\r
144 @register.inclusion_tag("node/reviser_info.html")
\r
145 def reviser_info(revision):
\r
146 return {'revision': revision}
\r