def _run_commentstree(msg): fname = msg.body comment = Comment._by_fullname(fname, data=True) link = Link._byID(comment.link_id, data=True) try: add_comment_tree(comment, link) except KeyError: # Hackity hack. Try to recover from a corrupted comment # tree print "Trying to fix broken comments-tree." link_comments(link._id, _update=True) add_comment_tree(comment, link)
def _run_commentstree(msgs, chan): fnames = [msg.body for msg in msgs] comments = Comment._by_fullname(fnames, data=True, return_dict=False) links = Link._byID(set(cm.link_id for cm in comments), data=True, return_dict=True) # add the comment to the comments-tree for comment in comments: l = links[comment.link_id] try: add_comment_tree(comment, l) except KeyError: # Hackity hack. Try to recover from a corrupted # comment tree print "Trying to fix broken comments-tree." link_comments(l._id, _update=True) add_comment_tree(comment, l)
def get_items(self, num, nested=True, starting_depth=0): r = link_comments(self.link._id) cids, comment_tree, depth, num_children = r if cids: comment_dict = Comment._byID(cids, data=True, return_dict=True) else: comment_dict = {} #convert tree from lists of IDs into lists of objects for pid, cids in comment_tree.iteritems(): tree = [comment_dict.get(cid) for cid in cids] comment_tree[pid] = [c for c in tree if c is not None] items = [] extra = {} top = None dont_collapse = [] #loading a portion of the tree if isinstance(self.comment, utils.iters): candidates = [] candidates.extend(self.comment) dont_collapse.extend(cm._id for cm in self.comment) top = self.comment[0] #assume the comments all have the same parent # TODO: removed by Chris to get rid of parent being sent # when morecomments is used. #if hasattr(candidates[0], "parent_id"): # parent = comment_dict[candidates[0].parent_id] # items.append(parent) #if permalink elif self.comment: top = self.comment dont_collapse.append(top._id) #add parents for context while self.context > 0 and hasattr(top, 'parent_id'): self.context -= 1 new_top = comment_dict[top.parent_id] comment_tree[new_top._id] = [top] num_children[new_top._id] = num_children[top._id] + 1 dont_collapse.append(new_top._id) top = new_top candidates = [top] #else start with the root comments else: candidates = [] candidates.extend(comment_tree.get(top, ())) #update the starting depth if required if top and depth[top._id] > 0: delta = depth[top._id] for k, v in depth.iteritems(): depth[k] = v - delta def sort_candidates(): candidates.sort(key=self.sort_key, reverse=self.rev_sort) #find the comments num_have = 0 sort_candidates() while num_have < num and candidates: to_add = candidates.pop(0) if to_add._deleted and not comment_tree.has_key(to_add._id): pass elif depth[to_add._id] < MAX_RECURSION: #add children if comment_tree.has_key(to_add._id): candidates.extend(comment_tree[to_add._id]) sort_candidates() items.append(to_add) num_have += 1 else: #add the recursion limit p_id = to_add.parent_id w = Wrapped(MoreRecursion(self.link, 0, comment_dict[p_id])) w.children.append(to_add) extra[p_id] = w wrapped = self.wrap_items(items) cids = dict((cm._id, cm) for cm in wrapped) final = [] #make tree for cm in wrapped: # don't show spam with no children if cm.deleted and not comment_tree.has_key(cm._id): continue cm.num_children = num_children[cm._id] if cm.collapsed and cm._id in dont_collapse: cm.collapsed = False if cm.collapse_in_link_threads: cm.collapsed = True parent = cids.get(cm.parent_id) if hasattr(cm, 'parent_id') else None if parent: if not hasattr(parent, 'child'): parent.child = self.empty_listing() parent.child.parent_name = parent._fullname parent.child.things.append(cm) else: final.append(cm) #put the extras in the tree for p_id, morelink in extra.iteritems(): parent = cids[p_id] parent.child = self.empty_listing(morelink) parent.child.parent_name = parent._fullname #put the remaining comments into the tree (the show more comments link) more_comments = {} while candidates: to_add = candidates.pop(0) direct_child = True #ignore top-level comments for now if not hasattr(to_add, 'parent_id'): p_id = None else: #find the parent actually being displayed #direct_child is whether the comment is 'top-level' p_id = to_add.parent_id while p_id and not cids.has_key(p_id): p = comment_dict[p_id] if hasattr(p, 'parent_id'): p_id = p.parent_id else: p_id = None direct_child = False mc2 = more_comments.get(p_id) if not mc2: mc2 = MoreChildren(self.link, depth[to_add._id], parent=comment_dict.get(p_id)) more_comments[p_id] = mc2 w_mc2 = Wrapped(mc2) if p_id is None: final.append(w_mc2) else: parent = cids[p_id] if hasattr(parent, 'child'): parent.child.things.append(w_mc2) else: parent.child = self.empty_listing(w_mc2) parent.child.parent_name = parent._fullname #add more children if comment_tree.has_key(to_add._id): candidates.extend(comment_tree[to_add._id]) if direct_child: mc2.children.append(to_add) mc2.count += 1 return final
def get_items(self, num, nested = True, starting_depth = 0): r = link_comments(self.link._id) cids, comment_tree, depth, num_children = r if cids: comment_dict = Comment._byID(cids, data = True, return_dict = True) else: comment_dict = {} #convert tree from lists of IDs into lists of objects for pid, cids in comment_tree.iteritems(): tree = [comment_dict.get(cid) for cid in cids] comment_tree[pid] = [c for c in tree if c is not None] items = [] extra = {} top = None dont_collapse = [] #loading a portion of the tree if isinstance(self.comment, utils.iters): candidates = [] candidates.extend(self.comment) dont_collapse.extend(cm._id for cm in self.comment) top = self.comment[0] #assume the comments all have the same parent # TODO: removed by Chris to get rid of parent being sent # when morecomments is used. #if hasattr(candidates[0], "parent_id"): # parent = comment_dict[candidates[0].parent_id] # items.append(parent) #if permalink elif self.comment: top = self.comment dont_collapse.append(top._id) #add parents for context while self.context > 0 and hasattr(top, 'parent_id'): self.context -= 1 new_top = comment_dict[top.parent_id] comment_tree[new_top._id] = [top] num_children[new_top._id] = num_children[top._id] + 1 dont_collapse.append(new_top._id) top = new_top candidates = [top] #else start with the root comments else: candidates = [] candidates.extend(comment_tree.get(top, ())) #update the starting depth if required if top and depth[top._id] > 0: delta = depth[top._id] for k, v in depth.iteritems(): depth[k] = v - delta def sort_candidates(): candidates.sort(key = self.sort_key, reverse = self.rev_sort) #find the comments num_have = 0 sort_candidates() while num_have < num and candidates: to_add = candidates.pop(0) if to_add._deleted and not comment_tree.has_key(to_add._id): pass elif depth[to_add._id] < MAX_RECURSION: #add children if comment_tree.has_key(to_add._id): candidates.extend(comment_tree[to_add._id]) sort_candidates() items.append(to_add) num_have += 1 else: #add the recursion limit p_id = to_add.parent_id w = Wrapped(MoreRecursion(self.link, 0, comment_dict[p_id])) w.children.append(to_add) extra[p_id] = w wrapped = self.wrap_items(items) cids = dict((cm._id, cm) for cm in wrapped) final = [] #make tree for cm in wrapped: # don't show spam with no children if cm.deleted and not comment_tree.has_key(cm._id): continue cm.num_children = num_children[cm._id] if cm.collapsed and cm._id in dont_collapse: cm.collapsed = False if cm.collapse_in_link_threads: cm.collapsed = True parent = cids.get(cm.parent_id) if hasattr(cm, 'parent_id') else None if parent: if not hasattr(parent, 'child'): parent.child = self.empty_listing() parent.child.parent_name = parent._fullname parent.child.things.append(cm) else: final.append(cm) #put the extras in the tree for p_id, morelink in extra.iteritems(): parent = cids[p_id] parent.child = self.empty_listing(morelink) parent.child.parent_name = parent._fullname #put the remaining comments into the tree (the show more comments link) more_comments = {} while candidates: to_add = candidates.pop(0) direct_child = True #ignore top-level comments for now if not hasattr(to_add, 'parent_id'): p_id = None else: #find the parent actually being displayed #direct_child is whether the comment is 'top-level' p_id = to_add.parent_id while p_id and not cids.has_key(p_id): p = comment_dict[p_id] if hasattr(p, 'parent_id'): p_id = p.parent_id else: p_id = None direct_child = False mc2 = more_comments.get(p_id) if not mc2: mc2 = MoreChildren(self.link, depth[to_add._id], parent = comment_dict.get(p_id)) more_comments[p_id] = mc2 w_mc2 = Wrapped(mc2) if p_id is None: final.append(w_mc2) else: parent = cids[p_id] if hasattr(parent, 'child'): parent.child.things.append(w_mc2) else: parent.child = self.empty_listing(w_mc2) parent.child.parent_name = parent._fullname #add more children if comment_tree.has_key(to_add._id): candidates.extend(comment_tree[to_add._id]) if direct_child: mc2.children.append(to_add) mc2.count += 1 return final