Browse Source

Calculate comment depths when building CommentTree

Previously I was just using Jinja's built-in loop.depth0 to output
comment depth in HTML, but actually attaching a depth attribute to the
comments will allow it to be used elsewhere as well. It's possible this
should even be on the Comment model itself, since it never changes and
might be useful in other ways.
merge-requests/85/head
Deimos 5 years ago
parent
commit
30e14e5d31
  1. 5
      tildes/tests/test_comment.py
  2. 4
      tildes/tildes/models/comment/comment_tree.py
  3. 2
      tildes/tildes/templates/macros/comments.jinja2

5
tildes/tests/test_comment.py

@ -189,6 +189,11 @@ def test_comment_tree(db, topic, session_user):
assert child.replies == [] assert child.replies == []
assert child2.replies == [subchild, subchild2] assert child2.replies == [subchild, subchild2]
# check depth values are as expected
assert root.depth == 0
assert child.depth == 1
assert subchild.depth == 2
# delete child2 (which has replies) and ensure it stays in the tree # delete child2 (which has replies) and ensure it stays in the tree
child2.is_deleted = True child2.is_deleted = True
db.commit() db.commit()

4
tildes/tildes/models/comment/comment_tree.py

@ -69,6 +69,8 @@ class CommentTree:
for comment in self.comments: for comment in self.comments:
if comment.parent_comment_id: if comment.parent_comment_id:
parent_comment = self.comments_by_id[comment.parent_comment_id] parent_comment = self.comments_by_id[comment.parent_comment_id]
comment.depth = parent_comment.depth + 1
parent_comment.replies.append(comment) parent_comment.replies.append(comment)
# if this comment isn't deleted, work back up towards the root, # if this comment isn't deleted, work back up towards the root,
@ -83,6 +85,7 @@ class CommentTree:
next_parent_id = parent_comment.parent_comment_id next_parent_id = parent_comment.parent_comment_id
parent_comment = self.comments_by_id[next_parent_id] parent_comment = self.comments_by_id[next_parent_id]
else: else:
comment.depth = 0
self.tree.append(comment) self.tree.append(comment)
@staticmethod @staticmethod
@ -235,6 +238,7 @@ class CommentInTree(ObjectProxy):
self.replies: List[CommentInTree] = [] self.replies: List[CommentInTree] = []
self.has_visible_descendant = False self.has_visible_descendant = False
self.num_children = 0 self.num_children = 0
self.depth: Optional[int] = None
@property @property
def has_uncollapsed_descendant(self) -> bool: def has_uncollapsed_descendant(self) -> bool:

2
tildes/tildes/templates/macros/comments.jinja2

@ -21,7 +21,7 @@
{# only add depth attr if we're rendering multiple comments at once #} {# only add depth attr if we're rendering multiple comments at once #}
{% if not is_individual_comment %} {% if not is_individual_comment %}
data-comment-depth="{{ loop.depth0 }}"
data-comment-depth="{{ comment.depth }}"
{% endif %} {% endif %}
{% if request.has_permission("label", comment) %} {% if request.has_permission("label", comment) %}

Loading…
Cancel
Save