]> git.openstreetmap.org Git - osqa.git/blobdiff - osqa/forum/templatetags/question_page_tags.py
initial import
[osqa.git] / osqa / forum / templatetags / question_page_tags.py
diff --git a/osqa/forum/templatetags/question_page_tags.py b/osqa/forum/templatetags/question_page_tags.py
new file mode 100644 (file)
index 0000000..88ac015
--- /dev/null
@@ -0,0 +1,123 @@
+from datetime import datetime, timedelta\r
+\r
+from forum.models import Question, FavoriteQuestion, LikedComment\r
+from django.utils.translation import ugettext as _\r
+from django.core.urlresolvers import reverse\r
+from django import template\r
+\r
+register = template.Library()\r
+\r
+@register.inclusion_tag('question/vote_buttons.html')\r
+def vote_buttons(post, user):\r
+    context = {\r
+        'post': post,\r
+        'post_type': (post.__class__ is Question) and 'question' or 'answer',\r
+        'user_vote': 'none'\r
+    }\r
+\r
+    if user.is_authenticated():\r
+        try:\r
+            vote = post.votes.get(user=user)\r
+            context['user_vote'] = vote.is_upvote() and 'up' or 'down'\r
+        except:\r
+            pass\r
+\r
+    return context\r
+\r
+@register.inclusion_tag('question/accept_button.html')    \r
+def accept_button(answer, user):\r
+    return {\r
+        'can_accept': user.is_authenticated() and user.can_accept_answer(answer),\r
+        'answer': answer,\r
+        'user': user\r
+    }\r
+\r
+@register.inclusion_tag('question/favorite_mark.html')\r
+def favorite_mark(question, user):\r
+    try:\r
+        FavoriteQuestion.objects.get(question=question, user=user)\r
+        favorited = True\r
+    except:\r
+        favorited = False\r
+\r
+    favorite_count = question.favorited_by.count()\r
+\r
+    return {'favorited': favorited, 'favorite_count': favorite_count, 'question': question}\r
+\r
+def post_control(text, url, command=False, title=""):\r
+    return {'text': text, 'url': url, 'command': command, 'title': title}\r
+\r
+@register.inclusion_tag('question/post_controls.html')\r
+def post_controls(post, user):\r
+    controls = []\r
+\r
+    if user.is_authenticated():\r
+        post_type = (post.__class__ is Question) and 'question' or 'answer'\r
+\r
+        if post_type == "answer":\r
+            controls.append(post_control(_('permanent link'), '#%d' % post.id, title=_("answer permanent link")))\r
+\r
+        edit_url = reverse('edit_' + post_type, kwargs={'id': post.id})\r
+        if user.can_edit_post(post):\r
+            controls.append(post_control(_('edit'), edit_url))\r
+        elif post_type == 'question' and user.can_retag_questions():\r
+            controls.append(post_control(_('retag'), edit_url))\r
+\r
+        if post_type == 'question':\r
+            if post.closed and user.can_reopen_question(post):\r
+                controls.append(post_control(_('reopen'), reverse('reopen', kwargs={'id': post.id})))\r
+            elif not post.closed and user.can_close_question(post):\r
+                controls.append(post_control(_('close'), reverse('close', kwargs={'id': post.id})))\r
+\r
+        if user.can_flag_offensive(post):\r
+            controls.append(post_control(_('flag'), reverse('flag_post', kwargs={'post_type': post_type, 'id': post.id}), \r
+                    command=True, title=_("report as offensive (i.e containing spam, advertising, malicious text, etc.)")))\r
+\r
+        if user.can_delete_post(post):\r
+            controls.append(post_control(_('delete'), reverse('delete_post', kwargs={'post_type': post_type, 'id': post.id}),\r
+                    command=True))\r
+\r
+    return {'controls': controls}\r
+\r
+@register.inclusion_tag('question/comments.html')\r
+def comments(post, user):\r
+    all_comments = post.comments.filter(deleted=False).order_by('added_at')\r
+\r
+    if len(all_comments) <= 5:\r
+        top_scorers = all_comments\r
+    else:\r
+        top_scorers = sorted(all_comments, lambda c1, c2: c2.score - c1.score)[0:5]\r
+\r
+    comments = []\r
+    showing = 0\r
+    for c in all_comments:\r
+        context = {\r
+            'can_delete': user.can_delete_comment(c),\r
+            'can_like': user.can_like_comment(c),\r
+            'can_edit': user.can_edit_comment(c)\r
+        }\r
+\r
+        if c in top_scorers or c.is_reply_to(user):\r
+            context['top_scorer'] = True\r
+            showing += 1\r
+        \r
+        if context['can_like']:\r
+            try:\r
+                LikedComment.active.get(comment=c, user=user)\r
+                context['likes'] = True\r
+            except:\r
+                context['likes'] = False\r
+\r
+        context['user'] = c.user\r
+        context.update(dict(c.__dict__))\r
+        comments.append(context)\r
+\r
+    return {\r
+        'comments': comments,\r
+        'post': post,\r
+        'post_type': (post.__class__ is Question) and 'question' or 'answer',\r
+        'can_comment': user.can_comment(post),\r
+        'max_length': 300,\r
+        'showing': showing,\r
+        'total': len(all_comments),\r
+    }
\ No newline at end of file