X-Git-Url: http://git.veekun.com/zzz-floof.git/blobdiff_plain/dce049a4e2dd5fee51fe9d8ebafd4c9401c0c5dd..db4c5f0b56d22815ecc31754271797b2401c9458:/floof/model/comments.py diff --git a/floof/model/comments.py b/floof/model/comments.py index bcfee9f..ef3a948 100644 --- a/floof/model/comments.py +++ b/floof/model/comments.py @@ -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 @@ -81,10 +83,10 @@ class Comment(Entity): # comment's left/right Comment.query.filter(Comment.discussion == self.discussion) \ .filter(Comment.left >= parent_right) \ - .update({ Comment.left: Comment.left + 2 }) + .update({ 'left': Comment.left + 2 }) Comment.query.filter(Comment.discussion == self.discussion) \ .filter(Comment.right >= parent_right) \ - .update({ Comment.right: Comment.right + 2 }) + .update({ 'right': Comment.right + 2 }) # Then stick the new comment in the right place self.left = parent_right @@ -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