Пример #1
0
def copy_language(self, request, object_id):
    target_language = request.POST.get('target_language')

    # CAVEAT: Avoiding self.get_object because it sets the page cache,
    #         We don't want a draft showing to a regular site visitor!
    #         source_page_content = self.get_object(request, object_id=object_id)
    source_page_content = PageContent._original_manager.get(pk=object_id)

    if source_page_content is None:
        raise self._get_404_exception(object_id)

    page = source_page_content.page

    if not target_language or target_language not in get_language_list(site_id=page.node.site_id):
        return HttpResponseBadRequest(force_str(_("Language must be set to a supported language!")))

    target_page_content = get_latest_admin_viewable_page_content(page, target_language)

    # First check that we are able to edit the target
    if not self.has_change_permission(request, obj=target_page_content):
        raise PermissionDenied

    for placeholder in source_page_content.get_placeholders():
        # Try and get a matching placeholder, only if it exists
        try:
            target = target_page_content.get_placeholders().get(slot=placeholder.slot)
        except ObjectDoesNotExist:
            continue

        plugins = placeholder.get_plugins_list(source_page_content.language)

        if not target.has_add_plugins_permission(request.user, plugins):
            return HttpResponseForbidden(force_str(_('You do not have permission to copy these plugins.')))
        copy_plugins_to_placeholder(plugins, target, language=target_language)
    return HttpResponse("ok")
Пример #2
0
    def copy_plugins(self, target_placeholder, language, root_plugin=None):
        from cms.utils.plugins import copy_plugins_to_placeholder

        new_plugins = copy_plugins_to_placeholder(
            plugins=self.get_plugins_list(language),
            placeholder=target_placeholder,
            language=language,
            root_plugin=root_plugin,
        )
        return new_plugins
Пример #3
0
 def create_module_plugin(cls, name, category, plugins):
     placeholder = category.modules
     position = placeholder.get_plugins().filter(
         parent__isnull=True).count()
     plugin_kwargs = {
         'plugin_type': cls.__name__,
         'placeholder_id': category.modules_id,
         'language': settings.LANGUAGE_CODE,
         'position': position,
     }
     plugin = CMSPlugin.add_root(**plugin_kwargs)
     instance = cls.model(module_name=name, module_category=category)
     plugin.set_base_attr(instance)
     instance.save()
     copy_plugins_to_placeholder(
         plugins,
         placeholder=placeholder,
         language=plugin.language,
         root_plugin=plugin,
     )
Пример #4
0
    def populate(self,
                 replaced_placeholder=None,
                 replaced_plugin=None,
                 plugins=None):
        if not replaced_placeholder and not replaced_plugin:
            copy_plugins_to_placeholder(
                plugins,
                placeholder=self.placeholder,
            )
            return

        if replaced_placeholder:
            plugins = replaced_placeholder.get_plugins(self.language)
            placeholder = replaced_placeholder
            add_plugin_kwargs = {}
        else:
            plugins = CMSPlugin.objects.filter(
                id__in=[replaced_plugin.pk] +
                replaced_plugin._get_descendants_ids(), )
            placeholder = replaced_plugin.placeholder
            add_plugin_kwargs = {'position': 'left', 'target': replaced_plugin}

        copy_plugins_to_placeholder(
            plugins,
            placeholder=self.placeholder,
            language=self.language,
        )
        plugins.delete()
        placeholder._recalculate_plugin_positions(self.language)

        new_plugin = add_plugin(placeholder,
                                plugin_type='Alias',
                                language=self.language,
                                alias=self.alias,
                                **add_plugin_kwargs)
        if replaced_plugin:
            new_plugin.position = replaced_plugin.position
            new_plugin.save(update_fields=['position'])
        return new_plugin
Пример #5
0
    def detach_alias_plugin(cls, plugin, language):
        source_placeholder = plugin.alias.get_placeholder(language)
        target_placeholder = plugin.placeholder
        source_plugins = plugin.alias.get_plugins(language)

        # Deleting uses a copy of a plugin to preserve pk on existing
        # ``plugin`` object. This is done due to
        # plugin.get_plugin_toolbar_info requiring a PK in a passed
        # instance.
        source_placeholder.delete_plugin(copy(plugin))
        target_placeholder._shift_plugin_positions(
            language,
            plugin.position,
            offset=target_placeholder.get_last_plugin_position(language),
        )
        copied_plugins = copy_plugins_to_placeholder(
            source_plugins,
            placeholder=target_placeholder,
            language=language,
            start_positions={language: plugin.position},
        )
        return copied_plugins
Пример #6
0
    def add_module_view(cls, request, module_id):
        if not request.user.is_staff:
            raise PermissionDenied

        module_plugin = get_object_or_404(cls.model, pk=module_id)

        if request.method == 'GET':
            form = AddModuleForm(request.GET)
        else:
            form = AddModuleForm(request.POST)

        if not form.is_valid():
            return HttpResponseBadRequest('Form received unexpected values')

        if request.method == 'GET':
            opts = cls.model._meta
            context = {
                'form': form,
                'has_change_permission': True,
                'opts': opts,
                'root_path': reverse('admin:index'),
                'is_popup': True,
                'app_label': opts.app_label,
                'module': module_plugin,
            }
            return render(request, 'djangocms_modules/add_module.html',
                          context)

        language = form.cleaned_data['target_language']
        target_placeholder = form.cleaned_data.get('target_placeholder')

        if target_placeholder:
            target_plugin = None
        else:
            target_plugin = form.cleaned_data['target_plugin']
            target_placeholder = target_plugin.placeholder

        if not target_placeholder.has_add_plugin_permission(
                request.user, module_plugin.plugin_type):
            return HttpResponseForbidden(
                force_text(_('You do not have permission to add a plugin.')))

        pl_admin = target_placeholder._get_attached_admin()

        if pl_admin:
            template = pl_admin.get_placeholder_template(
                request, target_placeholder)
        else:
            template = None

        try:
            has_reached_plugin_limit(
                target_placeholder,
                module_plugin.plugin_type,
                language=language,
                template=template,
            )
        except PluginLimitReached as er:
            return HttpResponseBadRequest(er)

        tree_order = target_placeholder.get_plugin_tree_order(
            language=language,
            parent_id=target_plugin,
        )

        m_admin = module_plugin.placeholder._get_attached_admin()

        # This is needed only because we of the operation signal requiring
        # a version of the plugin that's not been committed to the db yet.
        new_module_plugin = copy.copy(module_plugin)
        new_module_plugin.pk = None
        new_module_plugin.placeholder = target_placeholder
        new_module_plugin.parent = None
        new_module_plugin.position = len(tree_order) + 1

        operation_token = m_admin._send_pre_placeholder_operation(
            request=request,
            placeholder=target_placeholder,
            tree_order=tree_order,
            operation=operations.ADD_PLUGIN,
            plugin=new_module_plugin,
        )

        new_plugins = copy_plugins_to_placeholder(
            plugins=list(module_plugin.get_unbound_plugins()),
            placeholder=target_placeholder,
            language=language,
            root_plugin=target_plugin,
        )
        tree_order.append(new_plugins[0].pk)
        reorder_plugins(
            target_placeholder,
            parent_id=target_plugin,
            language=language,
            order=tree_order,
        )
        new_module_plugin = cls.model.objects.get(pk=new_plugins[0].pk)

        m_admin._send_post_placeholder_operation(
            request,
            operation=operations.ADD_PLUGIN,
            token=operation_token,
            plugin=new_module_plugin,
            placeholder=new_module_plugin.placeholder,
            tree_order=tree_order,
        )

        response = cls().render_close_frame(request, obj=new_module_plugin)

        if form.cleaned_data.get('disable_future_confirmation'):
            response.set_cookie(key=cls.confirmation_cookie_name, value=True)
        return response