Added parent property/links for comments, and a Return link on comment threads.
authorEevee <git@veekun.com>
Fri, 23 Oct 2009 02:27:57 +0000 (19:27 -0700)
committerEevee <git@veekun.com>
Fri, 23 Oct 2009 02:27:57 +0000 (19:27 -0700)
floof/model/comments.py
floof/templates/comments/lib.mako
floof/templates/comments/thread.mako

index bcfee9f..5e57c14 100644 (file)
@@ -12,6 +12,8 @@ def indent_comments(comments):
     The first comment's indent level is 0.
 
     The comments must be a complete subtree, ordered by their `left` property.
+
+    This function will also cache the `parent` property for each comment.
     """
 
     last_comment = None
@@ -103,3 +105,16 @@ class Comment(Entity):
                 self.left = max_right + 1
                 self.right = max_right + 2
 
+    @property
+    def parent(self):
+        """Returns this comment's parent.  This is cached, hence its being a
+        property and not a method.
+        """
+        if not hasattr(self, '_parent'):
+            self._parent = Comment.query \
+                .filter(Comment.discussion_id == self.discussion_id) \
+                .filter(Comment.left < self.left) \
+                .filter(Comment.right > self.right) \
+                .order_by(Comment.left.desc()) \
+                .first()
+        return self._parent
index 0c8ebfb..e822a17 100644 (file)
@@ -13,11 +13,21 @@ ${single_comment(comment)}
 </%def>
 
 <%def name="single_comment(comment)">
+% if hasattr(comment, 'indent'):
 <div class="comment" style="margin-left: ${comment.indent}em;">
+% else:
+<div class="comment">
+% endif
     <div class="header">
         <div class="user">${comment.user.name}</div>
         <div class="time">${comment.time}</div>
-        <div class="link"><a href="${url(controller='comments', action='thread', id=comment.id, owner_url=h.get_comment_owner_url(**c.route))}">Link</a></div>
+        <div class="links">
+            <a href="${url(controller='comments', action='thread', id=comment.id, owner_url=h.get_comment_owner_url(**c.route))}">Link</a>
+            <a href="${url(controller='comments', action='reply', id=comment.id, owner_url=h.get_comment_owner_url(**c.route))}">Reply</a>
+            % if comment.parent:
+            <a href="${url(controller='comments', action='thread', id=comment.parent.id, owner_url=h.get_comment_owner_url(**c.route))}">Parent</a>
+            % endif
+        </div>
     </div>
     <p>${comment.text}</p>
 </div>
index 359c296..947c500 100644 (file)
@@ -1,6 +1,8 @@
 <%inherit file="/base.mako" />
 <%namespace name="comments" file="/comments/lib.mako" />
 
+<p><a href="/${c.owner_url}">« Return</a></p>
+
 % if c.root_comment:
 ${comments.single_comment(c.root_comment)}
 % endif