I'm trying to make a copy of a FeinCMS page tree, which is managed using django-mptt. I wrote this function:
def make_tree_copy(page, parent=None):
'''
Makes a copy of the tree starting at "page", reparenting it to "parent"
'''
new_page = Page.objects.create_copy(page)
new_page.save()
Page.tree.move_node(new_page, parent)
# re-read so django-mptt fields get updated
new_page = Page.objects.get(id=new_page.id)
for child in page.get_children():
# re-read so django-mptt fields get updated
child = Page.objects.get(id=child.id)
make_tree_copy(child, new_page)
and call it using
make_tree_copy(Page.tree.root_nodes()[0])
It works in general but when I have a page tree looking like this:
A
|- B
|- C
|- D
It comes out as this:
A
|- B
|- D
|- C
From my stepping through the mptt code, the magic seems to happen in mptt/managers.py/_inter_tree_move_and_close_gap(), where for some reason the "lft" values of the grandchildren get changed. Before the move they are C=3, D=5, afterwards they are C=5, D=3.
Which explains why D gets sorted before C but I have no idea why these values get switched. Any thoughts?