##// 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 282 def get_reply_tree(self):
283 283 replies = self.get_replies().prefetch_related('refposts')
284 284 tree = []
285 parent_positions = {}
286 285 for reply in replies:
287 286 parents = reply.refposts.all()
287
288 288 found_parent = False
289 searching_for_index = False
290
289 291 if len(parents) > 0:
290 292 index = 0
293 parent_depth = 0
294
295 indexes_to_insert = []
296
291 297 for depth, element in tree:
292 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 306 if element in parents:
294 307 found_parent = True
295
296 offset_under_parent = parent_positions.get(element, 1)
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
308 searching_for_index = True
309 parent_depth = depth
302 310
303 311 if not found_parent:
304 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 322 return tree
General Comments 0
You need to be logged in to leave comments. Login now