Rearranged user/art relations.
[zzz-floof.git] / floof / controllers / comments.py
index 70c9073..342b009 100644 (file)
@@ -3,9 +3,10 @@ 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
+from floof.model import Art
 from floof.model.comments import Comment
 
 log = logging.getLogger(__name__)
@@ -33,28 +34,59 @@ 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,
         )
         elixir.session.commit()