'Repairs the tree'

def handle(self, *args, **options):

    self.stdout.write(u'fixing    page    tree')
    Page.fix_tree()
    root_draft_pages = Page.objects.filter(publisher_is_draft=True, parent__isnull=True)
    last = None
    try:
        first = root_draft_pages.order_by(u'path')[0]
    except IndexError:
        first = None
    for page in root_draft_pages.order_by(u'site__pk', u'path'):
        if last:
            last = last.reload()
            page = page.reload()
            page.move(target=last, pos=u'right')
        elif (first and (first.pk != page.pk)):
            page.move(target=first, pos=u'left')
        last = page.reload()
    root_public_pages = Page.objects.filter(publisher_is_draft=False, parent__isnull=True).order_by(u'publisher_public__path')
    root_public_pages = root_public_pages.filter(publisher_public__parent__isnull=True)
    for page in root_public_pages:
        page = page.reload()
        public = page.publisher_public
        page.move(target=public, pos=u'right')
    for root in root_draft_pages.order_by(u'site__pk', u'path'):
        self._update_descendants_tree(root)
    self.stdout.write(u'fixing    plugin    tree')
    CMSPlugin.fix_tree()
    self.stdout.write(u'all    done')
