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