Now comments also works for KB articles.

portnov [2008-06-23 05:12:15]
Now comments also works for KB articles.
Filename
comments/utils.py
kb/views.py
mgmt/bugs_views.py
templates/themes/default/kb_article.html
diff --git a/comments/utils.py b/comments/utils.py
index e9896ab..0d516ae 100644
--- a/comments/utils.py
+++ b/comments/utils.py
@@ -30,6 +30,8 @@ def get_comment_url(comment):
     return settings.BASE_URL+"/docs/%d/#comment-%s" % (id,comment.id)
   if type=='Request':
     return settings.BASE_URL+"/requests/%d/#comment-%s" % (id,comment.id)
+  if type=='KBArticle':
+    return settings.BASE_URL+"/kb/article/%d/#comment-%s" % (id,comment.id)

 def post_comment(request,type,id,redirect_to):
   """Try post the comment, containing in request.POST,
diff --git a/kb/views.py b/kb/views.py
index d3348f9..73364c3 100644
--- a/kb/views.py
+++ b/kb/views.py
@@ -6,6 +6,9 @@ from datetime import datetime
 from django.http import HttpResponseRedirect
 from django.core.urlresolvers import reverse

+from comments.forms import CommentForm
+from comments.utils import get_comments,post_comment
+
 from mgmt.decorators import render_to,login_required
 from models import *
 from forms import *
@@ -19,8 +22,20 @@ def kb_main(request):
 @login_required
 @render_to('kb_article.html')
 def one_article(request,id):
+  form = CommentForm()
+  if request.method == 'POST':
+    valid,form = post_comment(request,'KBArticle',id,'.')
+    if valid:            # then `form` is HttpResponseRedirect
+      return form
   article = KBArticle.objects.get(pk=id)
-  return {'article': article}
+  curr,pages,comments = get_comments(request,id,'KBArticle')
+  for c in comments:
+    c.can_delete = (request.user.id == c.author.id) or request.user.is_staff
+  return {'article': article,
+    'comments': comments,
+    'form': form,
+    'page_numbers': pages,
+    'current_page': curr}

 @login_required
 @render_to('create_article.html')
diff --git a/mgmt/bugs_views.py b/mgmt/bugs_views.py
index 1a34132..7395710 100644
--- a/mgmt/bugs_views.py
+++ b/mgmt/bugs_views.py
@@ -1,3 +1,4 @@
+from datetime import datetime
 from django.http import HttpResponseForbidden,HttpResponseRedirect
 from django.core.urlresolvers import reverse
 from django.conf import settings
@@ -45,7 +46,7 @@ def one_bug(request,id):
   if request.method=='POST':
     if request.POST['action']=='comment':
       valid,form = post_comment(request,'Bug',bug.id, reverse('mgmt.views.one_bug',args=(bug.id,)))
-      if valid:
+      if valid:        # Then `form` is HttpResponseRedirect
         return form
     elif request.POST['action']=='change_state':
       new_state = request.POST['new_state']
diff --git a/templates/themes/default/kb_article.html b/templates/themes/default/kb_article.html
index 58ea852..54714c7 100644
--- a/templates/themes/default/kb_article.html
+++ b/templates/themes/default/kb_article.html
@@ -13,5 +13,7 @@

 {{article.text|markdown|safe}}

+{% include "comments.html" %}
+
 {% endblock %}
ViewGit