1 from datetime import datetime, timedelta
\r
4 from forum.models import Question, Action
\r
5 from django.utils.translation import ungettext, ugettext as _
\r
6 from django.utils.html import strip_tags
\r
7 from django.utils.safestring import mark_safe
\r
8 from django.core.urlresolvers import reverse
\r
9 from django import template
\r
10 from forum.actions import *
\r
11 from forum import settings
\r
13 register = template.Library()
\r
15 @register.inclusion_tag('node/vote_buttons.html')
\r
16 def vote_buttons(post, user):
\r
17 context = dict(post=post, user_vote='none')
\r
19 if user.is_authenticated():
\r
20 context['user_vote'] = {1: 'up', -1: 'down', None: 'none'}[VoteAction.get_for(user, post)]
\r
24 @register.inclusion_tag('node/accept_button.html')
\r
25 def accept_button(answer, user):
\r
26 if not settings.DISABLE_ACCEPTING_FEATURE:
\r
28 'can_accept': user.is_authenticated() and user.can_accept_answer(answer),
\r
35 @register.inclusion_tag('node/wiki_symbol.html')
\r
36 def wiki_symbol(user, post):
\r
38 'is_wiki': post.nis.wiki,
\r
39 'post_type': post.friendly_name
\r
43 if user.can_edit_post(post):
\r
44 context['can_edit'] = True
\r
45 context['edit_url'] = reverse('edit_' + post.node_type, kwargs={'id': post.id})
\r
46 context['by'] = post.nstate.wiki.by.username
\r
47 context['at'] = post.nstate.wiki.at
\r
51 @register.inclusion_tag('node/favorite_mark.html')
\r
52 def favorite_mark(question, user):
\r
54 FavoriteAction.objects.get(canceled=False, node=question, user=user)
\r
59 return {'favorited': favorited, 'favorite_count': question.favorite_count, 'question': question}
\r
61 @register.simple_tag
\r
62 def post_classes(post):
\r
65 if post.nis.deleted:
\r
66 classes.append('deleted')
\r
68 if post.node_type == "answer":
\r
69 if (not settings.DISABLE_ACCEPTING_FEATURE) and post.nis.accepted:
\r
70 classes.append('accepted-answer')
\r
72 if post.author == post.question.author:
\r
73 classes.append('answered-by-owner')
\r
75 return " ".join(classes)
\r
77 def post_control(text, url, command=False, withprompt=False, confirm=False, title=""):
\r
78 classes = (command and "ajax-command" or " ") + (withprompt and " withprompt" or " ") + (confirm and " confirm" or " ")
\r
79 return {'text': text, 'url': url, 'classes': classes, 'title': title}
\r
81 @register.inclusion_tag('node/post_controls.html')
\r
82 def post_controls(post, user):
\r
86 if user.is_authenticated():
\r
87 post_type = post.node_type
\r
89 if post_type == "answer":
\r
90 controls.append(post_control(_('permanent link'), post.get_absolute_url(), title=_("answer permanent link")))
\r
92 edit_url = reverse('edit_' + post_type, kwargs={'id': post.id})
\r
93 if user.can_edit_post(post):
\r
94 controls.append(post_control(_('edit'), edit_url))
\r
95 elif post_type == 'question' and user.can_retag_questions():
\r
96 controls.append(post_control(_('retag'), edit_url))
\r
98 if post_type == 'question':
\r
99 if post.nis.closed and user.can_reopen_question(post):
\r
100 controls.append(post_control(_('reopen'), reverse('reopen', kwargs={'id': post.id}), command=True))
\r
101 elif not post.nis.closed and user.can_close_question(post):
\r
102 controls.append(post_control(_('close'), reverse('close', kwargs={'id': post.id}), command=True, withprompt=True))
\r
104 if user.can_flag_offensive(post):
\r
105 label = _('report')
\r
107 if user.can_view_offensive_flags(post):
\r
108 label = "%s (%d)" % (label, post.flag_count)
\r
110 controls.append(post_control(label, reverse('flag_post', kwargs={'id': post.id}),
\r
111 command=True, withprompt=True, title=_("report as offensive (i.e containing spam, advertising, malicious text, etc.)")))
\r
113 if user.can_delete_post(post):
\r
114 if post.nis.deleted:
\r
115 controls.append(post_control(_('undelete'), reverse('delete_post', kwargs={'id': post.id}),
\r
116 command=True, confirm=True))
\r
118 controls.append(post_control(_('delete'), reverse('delete_post', kwargs={'id': post.id}),
\r
119 command=True, confirm=True))
\r
121 if user.can_delete_post(post):
\r
122 menu.append(post_control(_('see revisions'),
\r
123 reverse('revisions',
\r
124 kwargs={'id': post.id}),
\r
125 command=False, confirm=False))
\r
127 if settings.WIKI_ON:
\r
128 if (not post.nis.wiki) and user.can_wikify(post):
\r
129 menu.append(post_control(_('mark as community wiki'), reverse('wikify', kwargs={'id': post.id}),
\r
130 command=True, confirm=True))
\r
132 elif post.nis.wiki and user.can_cancel_wiki(post):
\r
133 menu.append(post_control(_('cancel community wiki'), reverse('wikify', kwargs={'id': post.id}),
\r
134 command=True, confirm=True))
\r
136 if post.node_type == "answer" and user.can_convert_to_comment(post):
\r
137 menu.append(post_control(_('convert to comment'), reverse('convert_to_comment', kwargs={'id': post.id}),
\r
138 command=True, withprompt=True))
\r
140 if user.is_superuser or user.is_staff:
\r
141 plain_text = strip_tags(post.html)
\r
143 char_count = len(plain_text)
\r
144 fullStr = plain_text + " "
\r
145 left_trimmedStr = re.sub(re.compile(r"^[^\w]+", re.IGNORECASE), "", fullStr)
\r
146 cleanedStr = re.sub(re.compile(r"[^\w]+", re.IGNORECASE), " ", left_trimmedStr)
\r
147 splitString = cleanedStr.split(" ")
\r
148 word_count = len(splitString) - 1
\r
150 metrics = mark_safe("<b>%s %s / %s %s</b>" % (char_count, ungettext('character', 'characters', char_count),
\r
151 word_count, ungettext('word', 'words', word_count)))
\r
153 menu.append(post_control(metrics, "#", command=False, withprompt=False))
\r
155 return {'controls': controls, 'menu': menu, 'post': post, 'user': user}
\r
157 @register.inclusion_tag('node/comments.html')
\r
158 def comments(post, user):
\r
159 all_comments = post.comments.filter_state(deleted=False).order_by('added_at')
\r
161 if len(all_comments) <= 5:
\r
162 top_scorers = all_comments
\r
164 top_scorers = sorted(all_comments, lambda c1, c2: cmp(c2.score, c1.score))[0:5]
\r
168 for c in all_comments:
\r
170 'can_delete': user.can_delete_comment(c),
\r
171 'can_like': user.can_like_comment(c),
\r
172 'can_edit': user.can_edit_comment(c)
\r
175 if c in top_scorers or c.is_reply_to(user):
\r
176 context['top_scorer'] = True
\r
179 if context['can_like']:
\r
180 context['likes'] = VoteAction.get_for(user, c) == 1
\r
182 context['user'] = c.user
\r
183 context['comment'] = c.comment
\r
184 context.update(dict(c.__dict__))
\r
185 comments.append(context)
\r
188 'comments': comments,
\r
190 'can_comment': user.can_comment(post),
\r
191 'max_length': settings.FORM_MAX_COMMENT_BODY,
\r
192 'min_length': settings.FORM_MIN_COMMENT_BODY,
\r
193 'show_gravatar': settings.FORM_GRAVATAR_IN_COMMENTS,
\r
194 'showing': showing,
\r
195 'total': len(all_comments),
\r
200 @register.inclusion_tag("node/contributors_info.html")
\r
201 def contributors_info(node):
\r
203 'node_verb': (node.node_type == "question") and _("asked") or (
\r
204 (node.node_type == "answer") and _("answered") or _("posted")),
\r
208 @register.inclusion_tag("node/reviser_info.html")
\r
209 def reviser_info(revision):
\r
210 return {'revision': revision}
\r