'Called from admin interface when page is moved. Should be used on
all the places which are changing page position. Used like an interface
to django-treebeard, but after move is done page_moved signal is fired.
Note for issue #1166: url conflicts are handled by updated
check_title_slugs, overwrite_url on the moved page don\'t need any check
as it remains the same regardless of the page position in the tree'

def move_page(self, target, position='first-child'):

    assert self.publisher_is_draft
    assert target.publisher_is_draft
    self._publisher_keep_state = True
    is_inherited_template = (self.template == constants.TEMPLATE_INHERITANCE_MAGIC)
    target_public_page = None
    if ((position in ('left', 'right')) and (not target.parent)):
        if ((position == 'right') and target.publisher_public_id):
            target_public_page = Page.objects.get(pk=target.publisher_public_id)
            draft_root = target.get_root()
            public_root = target_public_page.get_root()
            assert (draft_root.get_next_sibling() == public_root)
        if is_inherited_template:
            self.template = self.get_template()
    if ((position == 'first-child') or (position == 'last-child')):
        self.parent_id = target.pk
    else:
        self.parent_id = target.parent_id
    self.save()
    if target_public_page:
        moved_page = self.move(target_public_page, pos=position)
    else:
        moved_page = self.move(target, pos=position)
    import cms.signals as cms_signals
    cms_signals.page_moved.send(sender=Page, instance=moved_page)
    page_utils.check_title_slugs(moved_page)
    page_utils.check_title_slugs(target)
    if target_public_page:
        page_utils.check_title_slugs(target)
    if moved_page.publisher_public_id:
        public_page = Page.objects.get(pk=moved_page.publisher_public_id)
        public_page = moved_page._publisher_save_public(public_page)
        cms_signals.page_moved.send(sender=Page, instance=public_page)
        page_utils.check_title_slugs(public_page)
    titles = moved_page.title_set.filter(language__in=moved_page.get_languages()).values_list('language', 'published')
    if moved_page.parent_id:
        parent_titles = moved_page.parent.title_set.exclude(publisher_state=PUBLISHER_STATE_PENDING).values_list('language', 'published')
        parent_titles_by_language = dict(parent_titles)
    else:
        parent_titles_by_language = {}
    for (language, published) in titles:
        if moved_page.parent_id:
            parent_is_published = parent_titles_by_language.get(language)
            if (parent_is_published and published):
                moved_page.mark_as_published(language)
                moved_page.mark_descendants_as_published(language)
            elif published:
                moved_page.mark_as_pending(language)
                moved_page.mark_descendants_pending(language)
        elif published:
            moved_page.mark_as_published(language)
            moved_page.mark_descendants_as_published(language)
    from cms.cache import invalidate_cms_page_cache
    invalidate_cms_page_cache()
    return moved_page
