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
23 if not settings.DISABLE_ACCEPTING_FEATURE:
\r
25 'can_accept': user.is_authenticated() and user.can_accept_answer(answer),
\r
32 @register.inclusion_tag('node/wiki_symbol.html')
\r
33 def wiki_symbol(user, post):
\r
35 'is_wiki': post.nis.wiki,
\r
36 'post_type': post.friendly_name
\r
40 if user.can_edit_post(post):
\r
41 context['can_edit'] = True
\r
42 context['edit_url'] = reverse('edit_' + post.node_type, kwargs={'id': post.id})
\r
43 context['by'] = post.nstate.wiki.by.username
\r
44 context['at'] = post.nstate.wiki.at
\r
48 @register.inclusion_tag('node/favorite_mark.html')
\r
49 def favorite_mark(question, user):
\r
51 FavoriteAction.objects.get(canceled=False, node=question, user=user)
\r
56 return {'favorited': favorited, 'favorite_count': question.favorite_count, 'question': question}
\r
58 @register.simple_tag
\r
59 def post_classes(post):
\r
62 if post.nis.deleted:
\r
63 classes.append('deleted')
\r
65 if post.node_type == "answer":
\r
66 if (not settings.DISABLE_ACCEPTING_FEATURE) and post.nis.accepted:
\r
67 classes.append('accepted-answer')
\r
69 if post.author == post.question.author:
\r
70 classes.append('answered-by-owner')
\r
72 return " ".join(classes)
\r
74 def post_control(text, url, command=False, withprompt=False, confirm=False, title=""):
\r
75 classes = (command and "ajax-command" or " ") + (withprompt and " withprompt" or " ") + (confirm and " confirm" or " ")
\r
76 return {'text': text, 'url': url, 'classes': classes, 'title': title}
\r
78 @register.inclusion_tag('node/post_controls.html')
\r
79 def post_controls(post, user):
\r
83 if user.is_authenticated():
\r
84 post_type = post.node_type
\r
86 if post_type == "answer":
\r
87 controls.append(post_control(_('permanent link'), post.get_absolute_url(), title=_("answer permanent link")))
\r
89 edit_url = reverse('edit_' + post_type, kwargs={'id': post.id})
\r
90 if user.can_edit_post(post):
\r
91 controls.append(post_control(_('edit'), edit_url))
\r
92 elif post_type == 'question' and user.can_retag_questions():
\r
93 controls.append(post_control(_('retag'), edit_url))
\r
95 if post_type == 'question':
\r
96 if post.nis.closed and user.can_reopen_question(post):
\r
97 controls.append(post_control(_('reopen'), reverse('reopen', kwargs={'id': post.id}), command=True))
\r
98 elif not post.nis.closed and user.can_close_question(post):
\r
99 controls.append(post_control(_('close'), reverse('close', kwargs={'id': post.id}), command=True, withprompt=True))
\r
101 if user.can_flag_offensive(post):
\r
102 label = _('report')
\r
104 if user.can_view_offensive_flags(post):
\r
105 label = "%s (%d)" % (label, post.flag_count)
\r
107 controls.append(post_control(label, reverse('flag_post', kwargs={'id': post.id}),
\r
108 command=True, withprompt=True, title=_("report as offensive (i.e containing spam, advertising, malicious text, etc.)")))
\r
110 if user.can_delete_post(post):
\r
111 if post.nis.deleted:
\r
112 controls.append(post_control(_('undelete'), reverse('delete_post', kwargs={'id': post.id}),
\r
113 command=True, confirm=True))
\r
115 controls.append(post_control(_('delete'), reverse('delete_post', kwargs={'id': post.id}),
\r
116 command=True, confirm=True))
\r
118 if user.can_delete_post(post):
\r
119 menu.append(post_control(_('see revisions'),
\r
120 reverse('revisions',
\r
121 kwargs={'id': post.id}),
\r
122 command=False, confirm=False))
\r
124 if settings.WIKI_ON:
\r
125 if (not post.nis.wiki) and user.can_wikify(post):
\r
126 menu.append(post_control(_('mark as community wiki'), reverse('wikify', kwargs={'id': post.id}),
\r
127 command=True, confirm=True))
\r
129 elif post.nis.wiki and user.can_cancel_wiki(post):
\r
130 menu.append(post_control(_('cancel community wiki'), reverse('wikify', kwargs={'id': post.id}),
\r
131 command=True, confirm=True))
\r
133 if post.node_type == "answer" and user.can_convert_to_comment(post):
\r
134 menu.append(post_control(_('convert to comment'), reverse('convert_to_comment', kwargs={'id': post.id}),
\r
135 command=True, withprompt=True))
\r
137 return {'controls': controls, 'menu': menu, 'post': post, 'user': user}
\r
139 @register.inclusion_tag('node/comments.html')
\r
140 def comments(post, user):
\r
141 all_comments = post.comments.filter_state(deleted=False).order_by('added_at')
\r
143 if len(all_comments) <= 5:
\r
144 top_scorers = all_comments
\r
146 top_scorers = sorted(all_comments, lambda c1, c2: cmp(c2.score, c1.score))[0:5]
\r
150 for c in all_comments:
\r
152 'can_delete': user.can_delete_comment(c),
\r
153 'can_like': user.can_like_comment(c),
\r
154 'can_edit': user.can_edit_comment(c)
\r
157 if c in top_scorers or c.is_reply_to(user):
\r
158 context['top_scorer'] = True
\r
161 if context['can_like']:
\r
162 context['likes'] = VoteAction.get_for(user, c) == 1
\r
164 context['user'] = c.user
\r
165 context['comment'] = c.comment
\r
166 context.update(dict(c.__dict__))
\r
167 comments.append(context)
\r
170 'comments': comments,
\r
172 'can_comment': user.can_comment(post),
\r
173 'max_length': settings.FORM_MAX_COMMENT_BODY,
\r
174 'min_length': settings.FORM_MIN_COMMENT_BODY,
\r
175 'show_gravatar': settings.FORM_GRAVATAR_IN_COMMENTS,
\r
176 'showing': showing,
\r
177 'total': len(all_comments),
\r
182 @register.inclusion_tag("node/contributors_info.html")
\r
183 def contributors_info(node):
\r
185 'node_verb': (node.node_type == "question") and _("asked") or (
\r
186 (node.node_type == "answer") and _("answered") or _("posted")),
\r
190 @register.inclusion_tag("node/reviser_info.html")
\r
191 def reviser_info(revision):
\r
192 return {'revision': revision}
\r