def save(self, **kwargs):
    from cms.api import create_page, add_plugin
    if ('sub_page' in self.cleaned_data):
        sub_page = self.cleaned_data['sub_page']
    else:
        sub_page = False
    if (self.page and sub_page):
        position = 'last-child'
        parent = self.page
        has_perm = user_can_add_subpage(self.user, target=parent)
    elif (self.page and self.page.parent_id):
        position = 'last-child'
        parent = self.page.parent
        has_perm = user_can_add_subpage(self.user, target=parent)
    else:
        parent = None
        position = 'last-child'
        has_perm = user_can_add_page(self.user)
    if (not has_perm):
        raise NoPermissionsException(_(u'User does not have permission to add page.'))
    page = create_page(title=self.cleaned_data['title'], slug=self.cleaned_data['slug'], template=get_cms_setting('PAGE_WIZARD_DEFAULT_TEMPLATE'), language=self.language_code, created_by=smart_text(self.user), parent=parent, position=position, in_navigation=True, published=False)
    page_type = self.cleaned_data.get('page_type')
    if page_type:
        copy_target = Page.objects.filter(pk=page_type).first()
    else:
        copy_target = None
    if copy_target:
        if (not copy_target.has_view_permission(self.user)):
            raise PermissionDenied()
        copy_target._copy_attributes(page, clean=True)
        page.save()
        for lang in copy_target.get_languages():
            copy_target._copy_contents(page, lang)
        from cms.extensions import extension_pool
        extension_pool.copy_extensions(copy_target, page)
    else:
        content = self.cleaned_data.get('content')
        plugin_type = get_cms_setting('PAGE_WIZARD_CONTENT_PLUGIN')
        plugin_body = get_cms_setting('PAGE_WIZARD_CONTENT_PLUGIN_BODY')
        slot = get_cms_setting('PAGE_WIZARD_CONTENT_PLACEHOLDER')
        if ((plugin_type in plugin_pool.plugins) and plugin_body):
            if (content and permissions.has_plugin_permission(self.user, plugin_type, 'add')):
                placeholder = self.get_placeholder(page, slot=slot)
                if placeholder:
                    opts = {'placeholder': placeholder, 'plugin_type': plugin_type, 'language': self.language_code, plugin_body: content}
                    add_plugin(**opts)
    if ((not self.page) and (Page.objects.filter(site_id=page.site_id).count() == 1)):
        page.publish(self.language_code)
        Page.set_homepage(page, user=self.user)
    return page
def save(self, **kwargs):
    from cms.api import create_page, add_plugin
    if ('sub_page' in self.cleaned_data):
        sub_page = self.cleaned_data['sub_page']
    else:
        sub_page = False
    if (self.page and sub_page):
        position = 'last-child'
        parent = self.page
        has_perm = user_can_add_subpage(self.user, target=parent)
    elif (self.page and self.page.parent_id):
        position = 'last-child'
        parent = self.page.parent
        has_perm = user_can_add_subpage(self.user, target=parent)
    else:
        parent = None
        position = 'last-child'
        has_perm = user_can_add_page(self.user)
    if (not has_perm):
        raise NoPermissionsException(_(u'User does not have permission to add page.'))
    page = create_page(title=self.cleaned_data['title'], slug=self.cleaned_data['slug'], template=get_cms_setting('PAGE_WIZARD_DEFAULT_TEMPLATE'), language=self.language_code, created_by=smart_text(self.user), parent=parent, position=position, in_navigation=True, published=False)
    page_type = self.cleaned_data.get('page_type')
    if page_type:
        copy_target = Page.objects.filter(pk=page_type).first()
    else:
        copy_target = None
    if copy_target:
        if (not copy_target.has_view_permission(self.user)):
            raise PermissionDenied()
        copy_target._copy_attributes(page, clean=True)
        page.save()
        for lang in copy_target.get_languages():
            copy_target._copy_contents(page, lang)
        from cms.extensions import extension_pool
        extension_pool.copy_extensions(copy_target, page)
    else:
        content = self.cleaned_data.get('content')
        plugin_type = get_cms_setting('PAGE_WIZARD_CONTENT_PLUGIN')
        plugin_body = get_cms_setting('PAGE_WIZARD_CONTENT_PLUGIN_BODY')
        slot = get_cms_setting('PAGE_WIZARD_CONTENT_PLACEHOLDER')
        if ((plugin_type in plugin_pool.plugins) and plugin_body):
            if (content and permissions.has_plugin_permission(self.user, plugin_type, 'add')):
                placeholder = self.get_placeholder(page, slot=slot)
                if placeholder:
                    opts = {'placeholder': placeholder, 'plugin_type': plugin_type, 'language': self.language_code, plugin_body: content}
                    add_plugin(**opts)
    if ((not self.page) and (Page.objects.filter(site_id=page.site_id).count() == 1)):
        page.publish(self.language_code)
        Page.set_homepage(page, user=self.user)
    return page