]> git.openstreetmap.org Git - osqa.git/blob - forum/templatetags/question_page_tags.py
fix in email sender, it was aplitting the attachments, or creating multiple, don...
[osqa.git] / forum / templatetags / question_page_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('question/vote_buttons.html')\r
11 def vote_buttons(post, user):\r
12     context = {\r
13         'post': post,\r
14         'post_type': (post.__class__ is Question) and 'question' or 'answer',\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('question/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('question/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('question/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             controls.append(post_control(_('flag'), reverse('flag_post', kwargs={'post_type': post_type, 'id': post.id}), \r
74                     command=True, title=_("report as offensive (i.e containing spam, advertising, malicious text, etc.)")))\r
75 \r
76         if user.can_delete_post(post):\r
77             controls.append(post_control(_('delete'), reverse('delete_post', kwargs={'post_type': post_type, 'id': post.id}),\r
78                     command=True))\r
79 \r
80     return {'controls': controls}\r
81 \r
82 @register.inclusion_tag('question/comments.html')\r
83 def comments(post, user):\r
84     all_comments = post.comments.filter(deleted=False).order_by('added_at')\r
85 \r
86     if len(all_comments) <= 5:\r
87         top_scorers = all_comments\r
88     else:\r
89         top_scorers = sorted(all_comments, lambda c1, c2: c2.score - c1.score)[0:5]\r
90 \r
91     comments = []\r
92     showing = 0\r
93     for c in all_comments:\r
94         context = {\r
95             'can_delete': user.can_delete_comment(c),\r
96             'can_like': user.can_like_comment(c),\r
97             'can_edit': user.can_edit_comment(c)\r
98         }\r
99 \r
100         if c in top_scorers or c.is_reply_to(user):\r
101             context['top_scorer'] = True\r
102             showing += 1\r
103         \r
104         if context['can_like']:\r
105             try:\r
106                 LikedComment.active.get(comment=c, user=user)\r
107                 context['likes'] = True\r
108             except:\r
109                 context['likes'] = False\r
110 \r
111         context['user'] = c.user\r
112         context.update(dict(c.__dict__))\r
113         comments.append(context)\r
114 \r
115     return {\r
116         'comments': comments,\r
117         'post': post,\r
118         'post_type': (post.__class__ is Question) and 'question' or 'answer',\r
119         'can_comment': user.can_comment(post),\r
120         'max_length': 300,\r
121         'showing': showing,\r
122         'total': len(all_comments),\r
123     }