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/wiki_symbol.html')
\r
30 def wiki_symbol(user, post):
\r
32 'is_wiki': post.nis.wiki,
\r
33 'post_type': post.friendly_name
\r
37 if user.can_edit_post(post):
\r
38 context['can_edit'] = True
\r
39 context['edit_url'] = reverse('edit_' + post.node_type, kwargs={'id': post.id})
\r
40 context['by'] = post.nstate.wiki.by.username
\r
41 context['at'] = post.nstate.wiki.at
\r
45 @register.inclusion_tag('node/favorite_mark.html')
\r
46 def favorite_mark(question, user):
\r
48 FavoriteAction.objects.get(canceled=False, node=question, user=user)
\r
53 return {'favorited': favorited, 'favorite_count': question.favorite_count, 'question': question}
\r
55 def post_control(text, url, command=False, withprompt=False, confirm=False, title=""):
\r
56 classes = (command and "ajax-command" or " ") + (withprompt and " withprompt" or " ") + (confirm and " confirm" or " ")
\r
57 return {'text': text, 'url': url, 'classes': classes, 'title': title}
\r
59 @register.inclusion_tag('node/post_controls.html')
\r
60 def post_controls(post, user):
\r
64 if user.is_authenticated():
\r
65 post_type = post.node_type
\r
67 if post_type == "answer":
\r
68 controls.append(post_control(_('permanent link'), '#%d' % post.id, title=_("answer permanent link")))
\r
70 edit_url = reverse('edit_' + post_type, kwargs={'id': post.id})
\r
71 if user.can_edit_post(post):
\r
72 controls.append(post_control(_('edit'), edit_url))
\r
73 elif post_type == 'question' and user.can_retag_questions():
\r
74 controls.append(post_control(_('retag'), edit_url))
\r
76 if post_type == 'question':
\r
77 if post.nis.closed and user.can_reopen_question(post):
\r
78 controls.append(post_control(_('reopen'), reverse('reopen', kwargs={'id': post.id}), command=True))
\r
79 elif not post.nis.closed and user.can_close_question(post):
\r
80 controls.append(post_control(_('close'), reverse('close', kwargs={'id': post.id}), command=True, withprompt=True))
\r
82 if user.can_flag_offensive(post):
\r
85 if user.can_view_offensive_flags(post):
\r
86 label = "%s (%d)" % (label, post.flag_count)
\r
88 controls.append(post_control(label, reverse('flag_post', kwargs={'id': post.id}),
\r
89 command=True, withprompt=True, title=_("report as offensive (i.e containing spam, advertising, malicious text, etc.)")))
\r
91 if user.can_delete_post(post):
\r
92 if post.nis.deleted:
\r
93 controls.append(post_control(_('undelete'), reverse('delete_post', kwargs={'id': post.id}),
\r
94 command=True, confirm=True))
\r
96 controls.append(post_control(_('delete'), reverse('delete_post', kwargs={'id': post.id}),
\r
97 command=True, confirm=True))
\r
99 if settings.WIKI_ON:
\r
100 if (not post.nis.wiki) and user.can_wikify(post):
\r
101 menu.append(post_control(_('mark as community wiki'), reverse('wikify', kwargs={'id': post.id}),
\r
102 command=True, confirm=True))
\r
104 elif post.nis.wiki and user.can_cancel_wiki(post):
\r
105 menu.append(post_control(_('cancel community wiki'), reverse('wikify', kwargs={'id': post.id}),
\r
106 command=True, confirm=True))
\r
108 if post.node_type == "answer" and user.can_convert_to_comment(post):
\r
109 menu.append(post_control(_('convert to comment'), reverse('convert_to_comment', kwargs={'id': post.id}),
\r
110 command=True, withprompt=True))
\r
112 return {'controls': controls, 'menu': menu, 'post': post, 'user': user}
\r
114 @register.inclusion_tag('node/comments.html')
\r
115 def comments(post, user):
\r
116 all_comments = post.comments.filter_state(deleted=False).order_by('added_at')
\r
118 if len(all_comments) <= 5:
\r
119 top_scorers = all_comments
\r
121 top_scorers = sorted(all_comments, lambda c1, c2: c2.score - c1.score)[0:5]
\r
125 for c in all_comments:
\r
127 'can_delete': user.can_delete_comment(c),
\r
128 'can_like': user.can_like_comment(c),
\r
129 'can_edit': user.can_edit_comment(c)
\r
132 if c in top_scorers or c.is_reply_to(user):
\r
133 context['top_scorer'] = True
\r
136 if context['can_like']:
\r
137 context['likes'] = VoteAction.get_for(user, c) == 1
\r
139 context['user'] = c.user
\r
140 context['comment'] = c.comment
\r
141 context.update(dict(c.__dict__))
\r
142 comments.append(context)
\r
145 'comments': comments,
\r
147 'can_comment': user.can_comment(post),
\r
148 'max_length': settings.FORM_MAX_COMMENT_BODY,
\r
149 'min_length': settings.FORM_MIN_COMMENT_BODY,
\r
150 'show_gravatar': settings.FORM_GRAVATAR_IN_COMMENTS,
\r
151 'showing': showing,
\r
152 'total': len(all_comments),
\r
157 @register.inclusion_tag("node/contributors_info.html")
\r
158 def contributors_info(node):
\r
160 'node_verb': (node.node_type == "question") and _("asked") or (
\r
161 (node.node_type == "answer") and _("answered") or _("posted")),
\r
165 @register.inclusion_tag("node/reviser_info.html")
\r
166 def reviser_info(revision):
\r
167 return {'revision': revision}
\r