1 from datetime import datetime, timedelta
\r
4 from forum.models import Question, Action
\r
5 from django.template import Template, Context
\r
6 from django.utils.translation import ungettext, ugettext as _
\r
7 from django.utils.html import strip_tags
\r
8 from django.utils.encoding import smart_unicode
\r
9 from django.utils.safestring import mark_safe
\r
10 from django.core.urlresolvers import reverse
\r
11 from django import template
\r
12 from forum.actions import *
\r
13 from forum import settings
\r
15 register = template.Library()
\r
17 @register.inclusion_tag('node/vote_buttons.html')
\r
18 def vote_buttons(post, user):
\r
19 context = dict(post=post, user_vote='none')
\r
21 if user.is_authenticated():
\r
22 context['user_vote'] = {1: 'up', -1: 'down', None: 'none'}[VoteAction.get_for(user, post)]
\r
26 @register.inclusion_tag('node/accept_button.html')
\r
27 def accept_button(answer, user):
\r
28 if not settings.DISABLE_ACCEPTING_FEATURE:
\r
30 'can_accept': user.is_authenticated() and user.can_accept_answer(answer),
\r
37 @register.inclusion_tag('node/wiki_symbol.html')
\r
38 def wiki_symbol(user, post):
\r
40 'is_wiki': post.nis.wiki,
\r
41 'post_type': post.friendly_name
\r
45 if user.can_edit_post(post):
\r
46 context['can_edit'] = True
\r
47 context['edit_url'] = reverse('edit_' + post.node_type, kwargs={'id': post.id})
\r
48 context['by'] = post.nstate.wiki.by.username
\r
49 context['at'] = post.nstate.wiki.at
\r
53 @register.inclusion_tag('node/favorite_mark.html')
\r
54 def favorite_mark(question, user):
\r
56 FavoriteAction.objects.get(canceled=False, node=question, user=user)
\r
61 return {'favorited': favorited, 'favorite_count': question.favorite_count, 'question': question}
\r
63 @register.simple_tag
\r
64 def post_classes(post):
\r
67 if post.nis.deleted:
\r
68 classes.append('deleted')
\r
70 if post.node_type == "answer":
\r
71 if (not settings.DISABLE_ACCEPTING_FEATURE) and post.nis.accepted:
\r
72 classes.append('accepted-answer')
\r
74 if post.author == post.question.author:
\r
75 classes.append('answered-by-owner')
\r
77 return " ".join(classes)
\r
79 def post_control(text, url, command=False, withprompt=False, confirm=False, title="", copy=False):
\r
80 classes = (command and "ajax-command" or " ") + (withprompt and " withprompt" or " ") + (confirm and " confirm" or " ") + \
\r
81 (copy and " copy" or " ")
\r
82 return {'text': text, 'url': url, 'classes': classes, 'title': title}
\r
84 @register.inclusion_tag('node/post_controls.html')
\r
85 def post_controls(post, user):
\r
88 post_type = post.node_type
\r
90 # We show the link tool if the post is an Answer. It is visible to Guests too.
\r
91 if post_type == "answer":
\r
92 # Answer permanent link tool
\r
93 controls.append(post_control(_('permanent link'), reverse('answer_permanent_link', kwargs={'id' : post.id,}),
\r
94 title=_("answer permanent link"), command=True, withprompt=True, copy=True))
\r
96 # Users should be able to award points for an answer. Users cannot award their own answers
\r
97 if user != post.author and user.is_authenticated():
\r
98 controls.append(post_control(_("award points"), reverse('award_points', kwargs={'user_id' : post.author.id,
\r
99 'answer_id' : post.id}), title=_("award points to %s") % smart_unicode(post.author.username),
\r
100 command=True, withprompt=True))
\r
102 # The other controls are visible only to authenticated users.
\r
103 if user.is_authenticated():
\r
105 edit_url = reverse('edit_' + post_type, kwargs={'id': post.id})
\r
106 if user.can_edit_post(post):
\r
107 controls.append(post_control(_('edit'), edit_url))
\r
108 elif post_type == 'question' and user.can_retag_questions():
\r
109 controls.append(post_control(_('retag'), edit_url))
\r
113 if post_type == 'question':
\r
114 if post.nis.closed and user.can_reopen_question(post):
\r
115 controls.append(post_control(_('reopen'), reverse('reopen', kwargs={'id': post.id}), command=True))
\r
116 elif not post.nis.closed and user.can_close_question(post):
\r
117 controls.append(post_control(_('close'), reverse('close', kwargs={'id': post.id}), command=True, withprompt=True))
\r
119 if user.can_flag_offensive(post):
\r
120 label = _('report')
\r
122 if user.can_view_offensive_flags(post):
\r
123 label = "%s (%d)" % (label, post.flag_count)
\r
125 controls.append(post_control(label, reverse('flag_post', kwargs={'id': post.id}),
\r
126 command=True, withprompt=True, title=_("report as offensive (i.e containing spam, advertising, malicious text, etc.)")))
\r
128 if user.can_delete_post(post):
\r
129 if post.nis.deleted:
\r
130 controls.append(post_control(_('undelete'), reverse('delete_post', kwargs={'id': post.id}),
\r
131 command=True, confirm=True))
\r
133 controls.append(post_control(_('delete'), reverse('delete_post', kwargs={'id': post.id}),
\r
134 command=True, confirm=True))
\r
136 if user.can_delete_post(post):
\r
137 menu.append(post_control(_('see revisions'),
\r
138 reverse('revisions',
\r
139 kwargs={'id': post.id}),
\r
140 command=False, confirm=False))
\r
142 if settings.WIKI_ON:
\r
143 if (not post.nis.wiki) and user.can_wikify(post):
\r
144 menu.append(post_control(_('mark as community wiki'), reverse('wikify', kwargs={'id': post.id}),
\r
145 command=True, confirm=True))
\r
147 elif post.nis.wiki and user.can_cancel_wiki(post):
\r
148 menu.append(post_control(_('cancel community wiki'), reverse('wikify', kwargs={'id': post.id}),
\r
149 command=True, confirm=True))
\r
151 if post.node_type == "answer" and user.can_convert_to_comment(post):
\r
152 menu.append(post_control(_('convert to comment'), reverse('convert_to_comment', kwargs={'id': post.id}),
\r
153 command=True, withprompt=True))
\r
155 if post.node_type == "answer" and user.can_convert_to_question(post):
\r
156 menu.append(post_control(_('convert to question'), reverse('convert_to_question', kwargs={'id': post.id}),
\r
157 command=False, confirm=True))
\r
159 if user.is_superuser or user.is_staff:
\r
160 plain_text = strip_tags(post.html)
\r
162 char_count = len(plain_text)
\r
163 fullStr = plain_text + " "
\r
164 left_trimmedStr = re.sub(re.compile(r"^[^\w]+", re.IGNORECASE), "", fullStr)
\r
165 cleanedStr = re.sub(re.compile(r"[^\w]+", re.IGNORECASE), " ", left_trimmedStr)
\r
166 splitString = cleanedStr.split(" ")
\r
167 word_count = len(splitString) - 1
\r
169 metrics = mark_safe("<b>%s %s / %s %s</b>" % (char_count, ungettext('character', 'characters', char_count),
\r
170 word_count, ungettext('word', 'words', word_count)))
\r
172 menu.append(post_control(metrics, "#", command=False, withprompt=False))
\r
174 return {'controls': controls, 'menu': menu, 'post': post, 'user': user}
\r
176 @register.inclusion_tag('node/comments.html')
\r
177 def comments(post, user):
\r
178 all_comments = post.comments.filter_state(deleted=False).order_by('added_at')
\r
180 if len(all_comments) <= 5:
\r
181 top_scorers = all_comments
\r
183 top_scorers = sorted(all_comments, lambda c1, c2: cmp(c2.score, c1.score))[0:5]
\r
187 for c in all_comments:
\r
189 'can_delete': user.can_delete_comment(c),
\r
190 'can_like': user.can_like_comment(c),
\r
191 'can_edit': user.can_edit_comment(c),
\r
192 'can_convert': user.can_convert_comment_to_answer(c)
\r
195 if c in top_scorers or c.is_reply_to(user):
\r
196 context['top_scorer'] = True
\r
199 if context['can_like']:
\r
200 context['likes'] = VoteAction.get_for(user, c) == 1
\r
202 context['user'] = c.user
\r
203 context['comment'] = c.comment
\r
204 context.update(dict(c.__dict__))
\r
205 comments.append(context)
\r
207 # Generate canned comments
\r
208 canned_comments = []
\r
209 for comment in settings.CANNED_COMMENTS:
\r
210 t = Template(smart_unicode(comment))
\r
213 'settings' : settings,
\r
215 canned_comments.append(t.render(c))
\r
218 'comments': comments,
\r
219 'canned_comments': canned_comments,
\r
221 'can_comment': user.can_comment(post),
\r
222 'max_length': settings.FORM_MAX_COMMENT_BODY,
\r
223 'min_length': settings.FORM_MIN_COMMENT_BODY,
\r
224 'show_gravatar': settings.FORM_GRAVATAR_IN_COMMENTS,
\r
225 'showing': showing,
\r
226 'total': len(all_comments),
\r
231 @register.inclusion_tag("node/contributors_info.html")
\r
232 def contributors_info(node, verb=None):
\r
234 'node_verb': verb and verb or ((node.node_type == "question") and _("asked") or (
\r
235 (node.node_type == "answer") and _("answered") or _("posted"))),
\r
239 @register.inclusion_tag("node/reviser_info.html")
\r
240 def reviser_info(revision):
\r
241 return {'revision': revision}
\r