Added parent property/links for comments, and a Return link on comment threads.
[zzz-floof.git] / floof / controllers / comments.py
index 03aee3d..b096ba2 100644 (file)
@@ -3,6 +3,7 @@ import logging
 import elixir
 from pylons import config, request, response, session, tmpl_context as c
 from pylons.controllers.util import abort, redirect, redirect_to
+from sqlalchemy import and_
 
 from floof.lib.base import BaseController, render
 from floof.model.art import Art
@@ -33,30 +34,60 @@ def find_owner(owner_url):
 
 class CommentsController(BaseController):
 
-    def thread(self, owner_url):
+    def thread(self, owner_url, id=None):
         """View a thread of comments, either attached to an item or starting
         from a parent comment belonging to that item.
         """
         owner_object = find_owner(owner_url)
-        c.comments = owner_object.discussion.comments
+        c.owner_url = owner_url
+        c.root_comment_id = id
+
+        if id:
+            # Get a thread starting from a certain point
+            c.root_comment = Comment.query.get(id)
+            if c.root_comment.discussion != owner_object.discussion:
+                abort(404)
+
+            c.comments = Comment.query.filter(and_(
+                Comment.discussion_id == owner_object.discussion_id,
+                Comment.left > c.root_comment.left,
+                Comment.right < c.root_comment.right
+            )).all()
+        else:
+            # Get everything
+            c.root_comment = None
+            c.comments = owner_object.discussion.comments
+
         return render('/comments/thread.mako')
 
-    def reply(self, owner_url):
+    def reply(self, owner_url, id=None):
         """Reply to a comment or discussion."""
+
+        c.parent_comment_id = id
+        if id:
+            c.parent_comment = Comment.query.get(id)
+        else:
+            c.parent_comment = None
+
         return render('/comments/reply.mako')
 
-    def reply_done(self, owner_url):
+    def reply_done(self, owner_url, id=None):
         """Finish replying to a comment or discussion."""
         # XXX form validation woo
 
+        owner_object = find_owner(owner_url)
+
+        if id:
+            parent_comment = Comment.query.get(id)
+        else:
+            parent_comment = None
+
         new_comment = Comment(
             text=request.params['text'],
             user=c.user,
+            discussion=owner_object.discussion,
+            parent=parent_comment,
         )
-
-        owner_object = find_owner(owner_url)
-        discussion = owner_object.discussion
-        discussion.comments.append(new_comment)
         elixir.session.commit()
 
         return redirect('/' + owner_url, code=301)