Advertisement
olexxxi

utils

Jun 3rd, 2021
764
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.05 KB | None | 0 0
  1.  
  2. def get_children(qs_child):
  3.     res = []
  4.     for comment in qs_child:
  5.         c = {
  6.             'id': comment.id,
  7.             'text': comment.text,
  8.             'timestamp': comment.timestamp.strftime('%Y-%m-%d %H:%m'),
  9.             'author': comment.user,
  10.             'is_child': comment.is_child,
  11.             'parent_id': comment.get_parent
  12.         }
  13.         if comment.comment_children.exists():
  14.             c['children'] = get_children(comment.comment_children.all())
  15.         res.append(c)
  16.     return res
  17.  
  18.  
  19.  
  20. def create_comments_tree(qs):
  21.     res = []
  22.     for comment in qs:
  23.         c = {
  24.             'id': comment.id,
  25.             'text': comment.text,
  26.             'timestamp': comment.timestamp.strftime('%Y-%m-%d %H:%m'),
  27.             'author': comment.user,
  28.             'is_child': comment.is_child,
  29.             'parent_id': comment.get_parent
  30.         }
  31.         if comment.comment_children:
  32.             c['children'] = get_children(comment.comment_children.all())
  33.         if not comment.is_child:
  34.             res.append(c)
  35.     return res
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement