##// END OF EJS Templates
One more tree fix
neko259 -
r1489:9709b2fb default
parent child Browse files
Show More
@@ -282,25 +282,41 b' class Thread(models.Model):'
282 def get_reply_tree(self):
282 def get_reply_tree(self):
283 replies = self.get_replies().prefetch_related('refposts')
283 replies = self.get_replies().prefetch_related('refposts')
284 tree = []
284 tree = []
285 parent_positions = {}
286 for reply in replies:
285 for reply in replies:
287 parents = reply.refposts.all()
286 parents = reply.refposts.all()
287
288 found_parent = False
288 found_parent = False
289 searching_for_index = False
290
289 if len(parents) > 0:
291 if len(parents) > 0:
290 index = 0
292 index = 0
293 parent_depth = 0
294
295 indexes_to_insert = []
296
291 for depth, element in tree:
297 for depth, element in tree:
292 index += 1
298 index += 1
299
300 # If this element is next after parent on the same level,
301 # insert child before it
302 if searching_for_index and depth <= parent_depth:
303 indexes_to_insert.append((index - 1, parent_depth))
304 searching_for_index = False
305
293 if element in parents:
306 if element in parents:
294 found_parent = True
307 found_parent = True
295
308 searching_for_index = True
296 offset_under_parent = parent_positions.get(element, 1)
309 parent_depth = depth
297 tree.insert(index + offset_under_parent - 1, (depth + 1, reply))
298
299 # Move whole parent tree by 1 added element
300 for parent in parents:
301 parent_positions[parent] = parent_positions.get(parent, 1) + 1
302
310
303 if not found_parent:
311 if not found_parent:
304 tree.append((0, reply))
312 tree.append((0, reply))
313 else:
314 if searching_for_index:
315 tree.append((parent_depth + 1, reply))
316
317 offset = 0
318 for last_index, parent_depth in indexes_to_insert:
319 tree.insert(last_index + offset, (parent_depth + 1, reply))
320 offset += 1
305
321
306 return tree
322 return tree
General Comments 0
You need to be logged in to leave comments. Login now