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