Comment posting support.
[zzz-floof.git] / floof / controllers / comments.py
diff --git a/floof/controllers/comments.py b/floof/controllers/comments.py
new file mode 100644 (file)
index 0000000..03aee3d
--- /dev/null
@@ -0,0 +1,62 @@
+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 floof.lib.base import BaseController, render
+from floof.model.art import Art
+from floof.model.comments import Comment
+
+log = logging.getLogger(__name__)
+
+def find_owner(owner_url):
+    """Returns whatever thing owns a group of comments."""
+
+    # Need to prepend a slash to make this an absolute URL
+    route = config['routes.map'].match('/' + owner_url)
+
+    if route['action'] not in ('show', 'view'):
+        abort(404)
+
+    if route['controller'] == 'art':
+        model = Art
+    else:
+        abort(404)
+
+    owner = model.query.get(route['id'])
+    if not owner:
+        abort(404)
+
+    return owner
+
+
+class CommentsController(BaseController):
+
+    def thread(self, owner_url):
+        """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
+        return render('/comments/thread.mako')
+
+    def reply(self, owner_url):
+        """Reply to a comment or discussion."""
+        return render('/comments/reply.mako')
+
+    def reply_done(self, owner_url):
+        """Finish replying to a comment or discussion."""
+        # XXX form validation woo
+
+        new_comment = Comment(
+            text=request.params['text'],
+            user=c.user,
+        )
+
+        owner_object = find_owner(owner_url)
+        discussion = owner_object.discussion
+        discussion.comments.append(new_comment)
+        elixir.session.commit()
+
+        return redirect('/' + owner_url, code=301)