]> git.openstreetmap.org Git - osqa.git/blob - forum/templatetags/node_tags.py
Some improvements in cache.
[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(canceled=False, 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     menu = []\r
46 \r
47     if user.is_authenticated():\r
48         post_type = (post.__class__ is Question) and 'question' or 'answer'\r
49 \r
50         if post_type == "answer":\r
51             controls.append(post_control(_('permanent link'), '#%d' % post.id, title=_("answer permanent link")))\r
52 \r
53         edit_url = reverse('edit_' + post_type, kwargs={'id': post.id})\r
54         if user.can_edit_post(post):\r
55             controls.append(post_control(_('edit'), edit_url))\r
56         elif post_type == 'question' and user.can_retag_questions():\r
57             controls.append(post_control(_('retag'), edit_url))\r
58 \r
59         if post_type == 'question':\r
60             if post.closed and user.can_reopen_question(post):\r
61                 controls.append(post_control(_('reopen'), reverse('reopen', kwargs={'id': post.id}), command=True))\r
62             elif not post.closed and user.can_close_question(post):\r
63                 controls.append(post_control(_('close'), reverse('close', kwargs={'id': post.id}), command=True, withprompt=True))\r
64 \r
65         if user.can_flag_offensive(post):\r
66             label = _('report')\r
67             \r
68             if user.can_view_offensive_flags(post):\r
69                 label =  "%s (%d)" % (label, post.flag_count)\r
70 \r
71             controls.append(post_control(label, reverse('flag_post', kwargs={'id': post.id}),\r
72                     command=True, withprompt=True, title=_("report as offensive (i.e containing spam, advertising, malicious text, etc.)")))\r
73 \r
74         if user.can_delete_post(post):\r
75             if post.deleted:\r
76                 controls.append(post_control(_('undelete'), reverse('delete_post', kwargs={'id': post.id}),\r
77                         command=True))\r
78             else:\r
79                 controls.append(post_control(_('delete'), reverse('delete_post', kwargs={'id': post.id}),\r
80                         command=True))\r
81 \r
82         if user.can_wikify(post):\r
83             menu.append(post_control(_('mark as community wiki'), reverse('wikify', kwargs={'id': post.id}),\r
84                         command=True))\r
85 \r
86         if post.node_type == "answer" and user.can_convert_to_comment(post):\r
87             menu.append(post_control(_('convert to comment'), reverse('convert_to_comment', kwargs={'id': post.id}),\r
88                         command=True, withprompt=True))\r
89 \r
90     return {'controls': controls, 'menu': menu, 'post': post, 'user': user}\r
91 \r
92 @register.inclusion_tag('node/comments.html')\r
93 def comments(post, user):\r
94     all_comments = post.comments.filter(deleted=None).order_by('added_at')\r
95 \r
96     if len(all_comments) <= 5:\r
97         top_scorers = all_comments\r
98     else:\r
99         top_scorers = sorted(all_comments, lambda c1, c2: c2.score - c1.score)[0:5]\r
100 \r
101     comments = []\r
102     showing = 0\r
103     for c in all_comments:\r
104         context = {\r
105             'can_delete': user.can_delete_comment(c),\r
106             'can_like': user.can_like_comment(c),\r
107             'can_edit': user.can_edit_comment(c)\r
108         }\r
109 \r
110         if c in top_scorers or c.is_reply_to(user):\r
111             context['top_scorer'] = True\r
112             showing += 1\r
113         \r
114         if context['can_like']:\r
115             context['likes'] = VoteAction.get_for(user, c) == 1\r
116 \r
117         context['user'] = c.user\r
118         context['comment'] = c.comment\r
119         context.update(dict(c.__dict__))\r
120         comments.append(context)\r
121 \r
122     return {\r
123         'comments': comments,\r
124         'post': post,\r
125         'can_comment': user.can_comment(post),\r
126         'max_length': settings.FORM_MAX_COMMENT_BODY,\r
127         'min_length': settings.FORM_MIN_COMMENT_BODY,\r
128         'show_gravatar': settings.FORM_GRAVATAR_IN_COMMENTS,\r
129         'showing': showing,\r
130         'total': len(all_comments),\r
131         'user': user,\r
132     }\r
133 \r
134 \r
135 @register.inclusion_tag("node/contributors_info.html")\r
136 def contributors_info(node):\r
137     return {\r
138         'node_verb': (node.node_type == "question") and _("asked") or (\r
139                     (node.node_type == "answer") and _("answered") or _("posted")),\r
140         'node': node,\r
141     }\r
142 \r
143 @register.inclusion_tag("node/reviser_info.html")\r
144 def reviser_info(revision):\r
145     return {'revision': revision}\r
146 \r
147 \r