+ # Comments are a tree, and are stored as a nested set, because:
+ # - It's easy to get a subtree with a single query.
+ # - It's easy to get a comment's depth.
+ # - It's easy to sort comments without recursion.
+ # The only real disadvantage is that adding a comment requires a quick
+ # update of all the following comments (in post-order), but that's rare
+ # enough that it shouldn't be a problem.
+ left = Field(Integer, index=True)
+ right = Field(Integer)
+
+ discussion = ManyToOne('Discussion')
+ user = ManyToOne('User')
+
+ def __init__(self, parent=None, **kwargs):
+ """Constructor override to set left/right correctly on a new comment.
+ """
+ super(Comment, self).__init__(**kwargs)
+
+ # Keep the comment count updated
+ self.discussion.count += 1
+
+ if parent:
+ # Parent comment given.
+ parent_right = parent.right
+ # Shift every left/right ahead by two to make room for the new
+ # comment's left/right
+ Comment.query.filter(Comment.discussion == self.discussion) \
+ .filter(Comment.left >= parent_right) \
+ .update({ 'left': Comment.left + 2 })
+ Comment.query.filter(Comment.discussion == self.discussion) \
+ .filter(Comment.right >= parent_right) \
+ .update({ 'right': Comment.right + 2 })
+
+ # Then stick the new comment in the right place
+ self.left = parent_right
+ self.right = parent_right + 1
+
+ else:
+ query = session.query(func.max(Comment.right)) \
+ .filter(Comment.discussion == self.discussion)
+ (max_right,) = query.one()
+
+ if not max_right:
+ # No comments yet. Use 1 and 2
+ self.left = 1
+ self.right = 2
+ else:
+ 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