]> git.openstreetmap.org Git - osqa.git/blob - forum/templatetags/node_tags.py
Fix error reported at http://meta.osqa.net/question/1673/default-values-on-badges...
[osqa.git] / forum / templatetags / node_tags.py
1 from datetime import datetime, timedelta\r
2 \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
9 \r
10 register = template.Library()\r
11 \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
15 \r
16     if user.is_authenticated():\r
17         context['user_vote'] = {1: 'up', -1: 'down', None: 'none'}[VoteAction.get_for(user, post)]\r
18 \r
19     return context\r
20 \r
21 @register.inclusion_tag('node/accept_button.html')\r
22 def accept_button(answer, user):\r
23     return {\r
24         'can_accept': user.is_authenticated() and user.can_accept_answer(answer),\r
25         'answer': answer,\r
26         'user': user\r
27     }\r
28 \r
29 @register.inclusion_tag('node/favorite_mark.html')\r
30 def favorite_mark(question, user):\r
31     try:\r
32         FavoriteAction.objects.get(node=question, user=user)\r
33         favorited = True\r
34     except:\r
35         favorited = False\r
36 \r
37     return {'favorited': favorited, 'favorite_count': question.favorite_count, 'question': question}\r
38 \r
39 def post_control(text, url, command=False, withprompt=False, title=""):\r
40     return {'text': text, 'url': url, 'command': command, 'withprompt': withprompt ,'title': title}\r
41 \r
42 @register.inclusion_tag('node/post_controls.html')\r
43 def post_controls(post, user):\r
44     controls = []\r
45 \r
46     if user.is_authenticated():\r
47         post_type = (post.__class__ is Question) and 'question' or 'answer'\r
48 \r
49         if post_type == "answer":\r
50             controls.append(post_control(_('permanent link'), '#%d' % post.id, title=_("answer permanent link")))\r
51 \r
52         edit_url = reverse('edit_' + post_type, kwargs={'id': post.id})\r
53         if user.can_edit_post(post):\r
54             controls.append(post_control(_('edit'), edit_url))\r
55         elif post_type == 'question' and user.can_retag_questions():\r
56             controls.append(post_control(_('retag'), edit_url))\r
57 \r
58         if post_type == 'question':\r
59             if post.closed and user.can_reopen_question(post):\r
60                 controls.append(post_control(_('reopen'), reverse('reopen', kwargs={'id': post.id}), command=True))\r
61             elif not post.closed and user.can_close_question(post):\r
62                 controls.append(post_control(_('close'), reverse('close', kwargs={'id': post.id}), command=True, withprompt=True))\r
63 \r
64         if user.can_flag_offensive(post):\r
65             label = _('report')\r
66             \r
67             if user.can_view_offensive_flags(post):\r
68                 label =  "%s (%d)" % (label, post.flag_count)\r
69 \r
70             controls.append(post_control(label, reverse('flag_post', kwargs={'id': post.id}),\r
71                     command=True, withprompt=True, title=_("report as offensive (i.e containing spam, advertising, malicious text, etc.)")))\r
72 \r
73         if user.can_delete_post(post):\r
74             if post.deleted:\r
75                 controls.append(post_control(_('undelete'), reverse('delete_post', kwargs={'id': post.id}),\r
76                         command=True))\r
77             else:\r
78                 controls.append(post_control(_('delete'), reverse('delete_post', kwargs={'id': post.id}),\r
79                         command=True))\r
80 \r
81     return {'controls': controls}\r
82 \r
83 @register.inclusion_tag('node/comments.html')\r
84 def comments(post, user):\r
85     all_comments = post.comments.filter(deleted=None).order_by('added_at')\r
86 \r
87     if len(all_comments) <= 5:\r
88         top_scorers = all_comments\r
89     else:\r
90         top_scorers = sorted(all_comments, lambda c1, c2: c2.score - c1.score)[0:5]\r
91 \r
92     comments = []\r
93     showing = 0\r
94     for c in all_comments:\r
95         context = {\r
96             'can_delete': user.can_delete_comment(c),\r
97             'can_like': user.can_like_comment(c),\r
98             'can_edit': user.can_edit_comment(c)\r
99         }\r
100 \r
101         if c in top_scorers or c.is_reply_to(user):\r
102             context['top_scorer'] = True\r
103             showing += 1\r
104         \r
105         if context['can_like']:\r
106             context['likes'] = VoteAction.get_for(user, c) == 1\r
107 \r
108         context['user'] = c.user\r
109         context['comment'] = c.comment\r
110         context.update(dict(c.__dict__))\r
111         comments.append(context)\r
112 \r
113     return {\r
114         'comments': comments,\r
115         'post': post,\r
116         'can_comment': user.can_comment(post),\r
117         'max_length': settings.FORM_MAX_COMMENT_BODY,\r
118         'min_length': settings.FORM_MIN_COMMENT_BODY,\r
119         'show_gravatar': settings.FORM_GRAVATAR_IN_COMMENTS,\r
120         'showing': showing,\r
121         'total': len(all_comments),\r
122         'user': user,\r
123     }\r
124 \r
125 \r
126 @register.inclusion_tag("node/contributors_info.html")\r
127 def contributors_info(node):\r
128     return {\r
129         'node_verb': (node.node_type == "question") and _("asked") or (\r
130                     (node.node_type == "answer") and _("answered") or _("posted")),\r
131         'node': node,\r
132     }\r
133 \r
134 \r
135 @register.inclusion_tag("node/reviser_info.html")\r
136 def reviser_info(revision):\r
137     return {'revision': revision}\r
138 \r