示例#1
0
def get_plugins_from_text(text, regex=OBJ_ADMIN_RE):
    from cms.utils.plugins import downcast_plugins

    plugin_ids = plugin_tags_to_id_list(text, regex)
    plugins = CMSPlugin.objects.filter(pk__in=plugin_ids).select_related('placeholder')
    plugin_list = downcast_plugins(plugins, select_placeholder=True)
    return dict((plugin.pk, plugin) for plugin in plugin_list)
示例#2
0
    def render(self, context, instance, placeholder):
        from cms.utils.plugins import downcast_plugins, build_plugin_tree

        context = super(AliasPlugin, self).render(context, instance, placeholder)
        cms_content_renderer = context.get('cms_content_renderer')

        if not cms_content_renderer or instance.is_recursive():
            return context

        if instance.plugin_id:
            plugins = instance.plugin.get_descendants().order_by('placeholder', 'path')
            plugins = [instance.plugin] + list(plugins)
            plugins = downcast_plugins(plugins, request=cms_content_renderer.request)
            plugins = list(plugins)
            plugins[0].parent_id = None
            plugins = build_plugin_tree(plugins)
            context['plugins'] = plugins
        if instance.alias_placeholder_id:
            content = cms_content_renderer.render_placeholder(
                placeholder=instance.alias_placeholder,
                context=context,
                editable=False,
            )
            context['content'] = mark_safe(content)
        return context
示例#3
0
    def get_form_elements(self):
        from .utils import get_nested_plugins

        if self.child_plugin_instances is None:
            # 3.1 and 3.0 compatibility
            if CMS_31:
                # default ordering is by path
                ordering = ('path',)
            else:
                ordering = ('tree_id', 'level', 'position')

            descendants = self.get_descendants().order_by(*ordering)
            # Set parent_id to None in order to
            # fool the build_plugin_tree function.
            # This is sadly necessary to avoid getting all nodes
            # higher than the form.
            parent_id = self.parent_id
            self.parent_id = None
            # Important that this is a list in order to modify
            # the current instance
            descendants_with_self = [self] + list(descendants)
            # Let the cms build the tree
            build_plugin_tree(descendants_with_self)
            # Set back the original parent
            self.parent_id = parent_id

        if self._form_elements is None:
            children = get_nested_plugins(self)
            children_instances = downcast_plugins(children)
            self._form_elements = [
                p for p in children_instances if is_form_element(p)]
        return self._form_elements
示例#4
0
    def _render_plugins(self, plugins, context):
        for plg in plugins:
            inst, name = plg.get_plugin_instance()

            if inst is None:
                # Ghost plugin
                continue

            # Get child plugins for this plugin instance, if any child plugins
            # exist
            try:
                # django CMS 3-
                plugins = (
                    inst
                    .get_descendants(include_self=True)
                    .order_by('placeholder', 'tree_id', 'level', 'position')
                )
            except (FieldError, TypeError):
                # django CMS 3.1+
                plugins = inst.get_descendants().order_by('path')

            plugins = [inst] + list(plugins)

            plugin_tree = downcast_plugins(plugins, select_placeholder=True)
            plugin_tree = list(plugin_tree)
            plugin_tree[0].parent_id = None
            plugin_tree = build_plugin_tree(plugin_tree)
            #  Replace plugin instance with plugin instance with correct
            #  child_plugin_instances set
            yield self._render_plugin(plugin_tree[0], context)
示例#5
0
def render_alias_plugin(context, instance):
    renderer = context.get('cms_content_renderer')

    if instance.plugin:
        plugins = instance.plugin.get_descendants().order_by(
            'placeholder', 'path')
        plugins = [instance.plugin] + list(plugins)
        plugins = downcast_plugins(plugins, request=renderer.request)
        plugins = list(plugins)
        plugins[0].parent_id = None
        plugins = build_plugin_tree(plugins)
        content = renderer.render_plugin(
            instance=plugins[0],
            context=context,
            editable=False,
        )
        return mark_safe(content)

    if instance.alias_placeholder:
        content = renderer.render_placeholder(
            placeholder=instance.alias_placeholder,
            context=context,
            editable=False,
        )
        return mark_safe(content)
    return ''
示例#6
0
    def get_form_elements(self):
        from .utils import get_nested_plugins

        if self.child_plugin_instances is None:
            descendants = self.get_descendants().order_by('path')
            # Set parent_id to None in order to
            # fool the build_plugin_tree function.
            # This is sadly necessary to avoid getting all nodes
            # higher than the form.
            parent_id = self.parent_id
            self.parent_id = None
            # Important that this is a list in order to modify
            # the current instance
            descendants_with_self = [self] + list(descendants)
            # Let the cms build the tree
            build_plugin_tree(descendants_with_self)
            # Set back the original parent
            self.parent_id = parent_id

        if self._form_elements is None:
            children = get_nested_plugins(self)
            children_instances = downcast_plugins(children)
            self._form_elements = [
                p for p in children_instances if is_form_element(p)
            ]
        return self._form_elements
def get_plugins_from_text(text, regex=OBJ_ADMIN_RE):
    from cms.utils.plugins import downcast_plugins

    plugin_ids = plugin_tags_to_id_list(text, regex)
    plugins = CMSPlugin.objects.filter(pk__in=plugin_ids).select_related('placeholder')
    plugin_list = downcast_plugins(plugins, select_placeholder=True)
    return dict((plugin.pk, plugin) for plugin in plugin_list)
    def test_replace_plugin_with_alias_correct_position(self):
        second_plugin = add_plugin(
            self.placeholder,
            'TextPlugin',
            language=self.language,
            body='test 2',
        )
        add_plugin(
            self.placeholder,
            'TextPlugin',
            language=self.language,
            body='test 3',
        )
        alias = self._create_alias()
        alias_plugin = alias.get_content(
            self.language).populate(replaced_plugin=second_plugin)
        plugins = self.placeholder.get_plugins()
        self.assertNotIn(
            self.plugin,
            plugins,
        )
        self.assertEqual(plugins[1].get_bound_plugin(), alias_plugin)

        ordered_plugins = sorted(
            downcast_plugins(plugins),
            key=attrgetter('position'),
        )

        self.assertEqual(
            [plugin.plugin_type for plugin in ordered_plugins],
            ['TextPlugin', 'Alias', 'TextPlugin'],
        )
示例#9
0
    def _render_plugins(self, plugins, context):
        for plg in plugins:
            inst, name = plg.get_plugin_instance()

            if inst is None:
                # Ghost plugin
                continue

            # Get child plugins for this plugin instance, if any child plugins
            # exist
            try:
                # django CMS 3-
                plugins = (
                    inst
                    .get_descendants(include_self=True)
                    .order_by('placeholder', 'tree_id', 'level', 'position')
                )
            except (FieldError, TypeError):
                # django CMS 3.1+
                plugins = inst.get_descendants().order_by('path')

            plugins = [inst] + list(plugins)

            plugin_tree = downcast_plugins(plugins, select_placeholder=True)
            plugin_tree = list(plugin_tree)
            plugin_tree[0].parent_id = None
            plugin_tree = build_plugin_tree(plugin_tree)
            #  Replace plugin instance with plugin instance with correct
            #  child_plugin_instances set
            yield self._render_plugin(plugin_tree[0], context)
示例#10
0
    def get_children(self, obj):
        """
        Some plugins can contain children
        This method supposed to get children and
        prepare and return together with parent object
        :param obj:
        :return:
        """
        data = []
        plugin = obj.get_plugin_class()
        if not (getattr(plugin, 'allow_children', False)
                and getattr(plugin, 'child_classes', None)):
            return data
        children = obj.get_descendants().order_by('placeholder', 'path')
        children = [obj] + list(children)
        children = list(downcast_plugins(children))
        children[0].parent_id = None
        children = list(build_plugin_tree(children))

        def get_plugin_data(child_plugin):
            serializer = get_serializer(child_plugin,
                                        model=child_plugin._meta.model,
                                        context=self.context)
            plugin_data = serializer.data
            plugin_data['inlines'] = self.get_inlines(child_plugin)
            if child_plugin.child_plugin_instances:
                plugin_data['children'] = []
                for plug in child_plugin.child_plugin_instances:
                    plugin_data['children'].append(get_plugin_data(plug))
            return plugin_data

        for child in children[0].child_plugin_instances or []:
            data.append(get_plugin_data(child))
        return data
示例#11
0
    def get_form_elements(self):
        from .utils import get_nested_plugins

        if self.child_plugin_instances is None:
            # 3.1 and 3.0 compatibility
            if CMS_31:
                # default ordering is by path
                ordering = ('path', )
            else:
                ordering = ('tree_id', 'level', 'position')

            descendants = self.get_descendants().order_by(*ordering)
            # Set parent_id to None in order to
            # fool the build_plugin_tree function.
            # This is sadly necessary to avoid getting all nodes
            # higher than the form.
            parent_id = self.parent_id
            self.parent_id = None
            # Important that this is a list in order to modify
            # the current instance
            descendants_with_self = [self] + list(descendants)
            # Let the cms build the tree
            build_plugin_tree(descendants_with_self)
            # Set back the original parent
            self.parent_id = parent_id

        if self._form_elements is None:
            children = get_nested_plugins(self)
            children_instances = downcast_plugins(children)
            self._form_elements = [
                p for p in children_instances if is_form_element(p)
            ]
        return self._form_elements
示例#12
0
    def render(self, context, instance, placeholder):
        from cms.utils.plugins import downcast_plugins, build_plugin_tree

        context = super(AliasPlugin, self).render(context, instance, placeholder)
        request = context.get('request')

        if not request or instance.is_recursive():
            return context

        if instance.plugin_id:
            plugins = instance.plugin.get_descendants().order_by('placeholder', 'path')
            plugins = [instance.plugin] + list(plugins)
            plugins = downcast_plugins(plugins, request=request)
            plugins = list(plugins)
            plugins[0].parent_id = None
            plugins = build_plugin_tree(plugins)
            context['plugins'] = plugins

        if instance.alias_placeholder_id:
            toolbar = get_toolbar_from_request(request)
            renderer = toolbar.content_renderer
            content = renderer.render_placeholder(
                placeholder=instance.alias_placeholder,
                context=context,
                editable=False,
            )
            context['content'] = mark_safe(content)
        return context
    def test_post(self):
        """the slot for content is always there, the slot for navigation needs
        to be created"""
        pagecontent = PageContentWithVersionFactory(template="page.html")
        placeholder = PlaceholderFactory(slot="content", source=pagecontent)
        PlaceholderFactory(slot="navigation", source=pagecontent)
        add_plugin(placeholder,
                   "TextPlugin",
                   pagecontent.language,
                   body="Test text")
        with self.login_user_context(self.get_superuser()):
            response = self.client.post(
                self.get_admin_url(PageContent, "duplicate", pagecontent.pk),
                data={
                    "site": Site.objects.first().pk,
                    "slug": "foo bar"
                },
                follow=True,
            )
        self.assertRedirects(response,
                             self.get_admin_url(PageContent, "changelist"))
        new_pagecontent = PageContent._base_manager.latest("pk")
        new_placeholder = new_pagecontent.placeholders.get(slot="content")
        self.assertEqual(PageContent._base_manager.count(), 2)
        self.assertNotEqual(pagecontent, new_pagecontent)
        self.assertNotEqual(pagecontent.page, new_pagecontent.page)
        self.assertEqual(pagecontent.language, new_pagecontent.language)
        self.assertEqual(
            new_pagecontent.page.get_slug(new_pagecontent.language), "foo-bar")
        new_plugins = list(downcast_plugins(
            new_placeholder.get_plugins_list()))

        self.assertEqual(len(new_plugins), 1)
        self.assertEqual(new_plugins[0].plugin_type, "TextPlugin")
        self.assertEqual(new_plugins[0].body, "Test text")
示例#14
0
    def get_form_elements(self):
        from .utils import get_nested_plugins

        if self.child_plugin_instances is None:
            descendants = self.get_descendants().order_by('path')
            # Set parent_id to None in order to
            # fool the build_plugin_tree function.
            # This is sadly necessary to avoid getting all nodes
            # higher than the form.
            parent_id = self.parent_id
            self.parent_id = None
            # Important that this is a list in order to modify
            # the current instance
            descendants_with_self = [self] + list(descendants)
            # Let the cms build the tree
            build_plugin_tree(descendants_with_self)
            # Set back the original parent
            self.parent_id = parent_id

        if self._form_elements is None:
            children = get_nested_plugins(self)
            children_instances = downcast_plugins(children)
            self._form_elements = [
                p for p in children_instances if is_form_element(p)]
        return self._form_elements
示例#15
0
    def get_children(self, obj):
        """
        Some plugins can contain children
        This method supposed to get children and
        prepare and return together with parent object
        :param obj:
        :return:
        """
        data = []
        plugin = obj.get_plugin_class()
        if not(getattr(plugin, 'allow_children', False) and getattr(plugin, 'child_classes', None)):
            return data
        children = obj.get_descendants().order_by('placeholder', 'path')
        children = [obj] + list(children)
        children = list(downcast_plugins(children))
        children[0].parent_id = None
        children = list(build_plugin_tree(children))

        def get_plugin_data(child_plugin):
            serializer = get_serializer(child_plugin, model=child_plugin._meta.model, context=self.context)
            plugin_data = serializer.data
            plugin_data['inlines'] = self.get_inlines(child_plugin)
            if child_plugin.child_plugin_instances:
                plugin_data['children'] = []
                for plug in child_plugin.child_plugin_instances:
                    plugin_data['children'].append(get_plugin_data(plug))
            return plugin_data

        for child in children[0].child_plugin_instances or []:
            data.append(get_plugin_data(child))
        return data
def _plugin_dict(text, regex=OBJ_ADMIN_RE):
    try:
        from cms.utils.plugins import downcast_plugins
    except ImportError:
        from cms.plugins.utils import downcast_plugins
    plugin_ids = plugin_tags_to_id_list(text, regex)
    plugin_list = downcast_plugins(CMSPlugin.objects.filter(pk__in=plugin_ids), select_placeholder=True)
    return dict((plugin.pk, plugin) for plugin in plugin_list)
示例#17
0
    def render(self, context, instance, placeholder):
        template_vars = {
            'object': instance,
            'placeholder': placeholder,
        }
        lang = instance.from_language
        request = context.get('request', None)
        if not lang:
            if 'request' in context:
                lang = get_language_from_request(request)
            else:
                lang = settings.LANGUAGE_CODE
        page = instance.placeholder.page
        from_page = instance.from_page

        if page.publisher_is_draft:
            from_page = from_page.get_draft_object()
        else:
            from_page = from_page.get_public_object()

        plugins = get_cmsplugin_queryset(request).filter(
            placeholder__page=from_page,
            language=lang,
            placeholder__slot__iexact=placeholder,
            parent__isnull=True).order_by('position').select_related()

        plugin_output = []
        template_vars['parent_plugins'] = plugins

        for plg in plugins:
            tmpctx = copy.copy(context)
            tmpctx.update(template_vars)
            inst, name = plg.get_plugin_instance()
            if inst is None:
                continue
            # Get child plugins for this plugin instance, if any child plugins
            # exist
            try:
                # django CMS 3-
                plugins = [inst] + list(
                    inst.get_descendants(include_self=True).order_by(
                        'placeholder', 'tree_id', 'level', 'position'))
            except (FieldError, TypeError):
                # django CMS 3.1+
                plugins = [inst] + list(
                    inst.get_descendants().order_by('path'))
            plugin_tree = downcast_plugins(plugins)
            plugin_tree[0].parent_id = None
            plugin_tree = build_plugin_tree(plugin_tree)
            #  Replace plugin instance with plugin instance with correct
            #  child_plugin_instances set
            inst = plugin_tree[0]
            outstr = inst.render_plugin(tmpctx, placeholder)
            plugin_output.append(outstr)
        template_vars['parent_output'] = plugin_output
        context.update(template_vars)
        return context
示例#18
0
    def render(self, context, instance, placeholder):
        template_vars = {
            'object': instance,
            'placeholder': placeholder,
        }
        lang = instance.from_language
        request = context.get('request', None)
        if not lang:
            if 'request' in context:
                lang = get_language_from_request(request)
            else:
                lang = settings.LANGUAGE_CODE
        page = instance.placeholder.page
        from_page = instance.from_page

        if page.publisher_is_draft:
            from_page = from_page.get_draft_object()
        else:
            from_page = from_page.get_public_object()

        plugins = get_cmsplugin_queryset(request).filter(
            placeholder__page=from_page,
            language=lang,
            placeholder__slot__iexact=placeholder,
            parent__isnull=True
        ).order_by('position').select_related()

        plugin_output = []
        template_vars['parent_plugins'] = plugins

        for plg in plugins:
            tmpctx = copy.copy(context)
            tmpctx.update(template_vars)
            inst, name = plg.get_plugin_instance()
            if inst is None:
                continue
            # Get child plugins for this plugin instance, if any child plugins
            # exist
            try:
                # django CMS 3-
                plugins = [inst] + list(inst.get_descendants(include_self=True)
                                        .order_by('placeholder', 'tree_id',
                                                  'level', 'position'))
            except (FieldError, TypeError):
                # django CMS 3.1+
                plugins = [inst] + list(inst.get_descendants().order_by('path'))
            plugin_tree = downcast_plugins(plugins)
            plugin_tree[0].parent_id = None
            plugin_tree = build_plugin_tree(plugin_tree)
            #  Replace plugin instance with plugin instance with correct
            #  child_plugin_instances set
            inst = plugin_tree[0]
            outstr = inst.render_plugin(tmpctx, placeholder)
            plugin_output.append(outstr)
        template_vars['parent_output'] = plugin_output
        context.update(template_vars)
        return context
示例#19
0
 def prepare_plugin_types(self, obj):
     plugins = downcast_plugins(
         CMSPlugin.objects.filter(
             placeholder__content_type=ContentType.objects.get_for_model(obj),
             placeholder__object_id=obj.pk,
             language=obj.language,
         )
     )
     return list(set(plugin.plugin_type for plugin in plugins))
示例#20
0
 def test_copy_plugin_author(self):
     post1 = self._get_post(self.data['en'][0])
     post2 = self._get_post(self.data['en'][1])
     plugin = add_plugin(post1.content, 'BlogAuthorPostsPlugin', language='en')
     plugin.authors.add(self.user)
     plugins = list(post1.content.cmsplugin_set.filter(language='en').order_by('tree_id', 'level', 'position'))
     copy_plugins_to(plugins, post2.content)
     new = downcast_plugins(post2.content.cmsplugin_set.all())
     self.assertEqual(set(new[0].authors.all()), set([self.user]))
示例#21
0
def _plugin_dict(text, regex=OBJ_ADMIN_RE):
    try:
        from cms.utils.plugins import downcast_plugins
    except ImportError:
        from cms.plugins.utils import downcast_plugins

    plugin_ids = plugin_tags_to_id_list(text, regex)
    plugin_list = downcast_plugins(CMSPlugin.objects.filter(pk__in=plugin_ids), select_placeholder=True)
    return dict((plugin.pk, plugin) for plugin in plugin_list)
示例#22
0
 def test_copy_plugin_latest(self):
     post1 = self._get_post(self.data['en'][0])
     post2 = self._get_post(self.data['en'][1])
     tag = Tag.objects.create(name='tag 1')
     plugin = add_plugin(post1.content, 'BlogLatestEntriesPlugin', language='en')
     plugin.tags.add(tag)
     plugins = list(post1.content.cmsplugin_set.filter(language='en').order_by('tree_id', 'level', 'position'))
     copy_plugins_to(plugins, post2.content)
     new = downcast_plugins(post2.content.cmsplugin_set.all())
     self.assertEqual(set(new[0].tags.all()), set([tag]))
示例#23
0
    def get_form_elements(self):
        from .utils import get_nested_plugins

        if self.child_plugin_instances is None:
            self.child_plugin_instances = self.get_descendants().order_by('tree_id', 'level', 'position')

        if self._form_elements is None:
            children = get_nested_plugins(self)
            children_instances = downcast_plugins(children)
            self._form_elements = [plugin for plugin in children_instances if is_form_element(plugin)]
        return self._form_elements
示例#24
0
 def test_copy_plugin_author(self):
     post1 = self._get_post(self._post_data[0]['en'])
     post2 = self._get_post(self._post_data[1]['en'])
     plugin = add_plugin(post1.content, 'BlogAuthorPostsPlugin', language='en', app_config=self.app_config_1)
     plugin.authors.add(self.user)
     if CMS_30:
         plugins = list(post1.content.cmsplugin_set.filter(language='en').order_by('tree_id', 'level', 'position'))
     else:
         plugins = list(post1.content.cmsplugin_set.filter(language='en').order_by('path', 'depth', 'position'))
     copy_plugins_to(plugins, post2.content)
     new = downcast_plugins(post2.content.cmsplugin_set.all())
     self.assertEqual(set(new[0].authors.all()), set([self.user]))
示例#25
0
def get_plugin_tree_as_json(request, plugins):
    from cms.utils.plugins import (
        build_plugin_tree,
        downcast_plugins,
        get_plugin_restrictions,
    )

    tree_data = []
    tree_structure = []
    restrictions = {}
    toolbar = get_toolbar_from_request(request)
    template = toolbar.templates.drag_item_template
    placeholder = plugins[0].placeholder
    host_page = placeholder.page
    copy_to_clipboard = placeholder.pk == toolbar.clipboard.pk
    plugins = downcast_plugins(plugins, select_placeholder=True)
    plugin_tree = build_plugin_tree(plugins)
    get_plugin_info = get_plugin_toolbar_info

    def collect_plugin_data(plugin):
        child_classes, parent_classes = get_plugin_restrictions(
            plugin=plugin,
            page=host_page,
            restrictions_cache=restrictions,
        )
        plugin_info = get_plugin_info(
            plugin,
            children=child_classes,
            parents=parent_classes,
        )

        tree_data.append(plugin_info)

        for plugin in plugin.child_plugin_instances or []:
            collect_plugin_data(plugin)

    with force_language(toolbar.toolbar_language):
        for root_plugin in plugin_tree:
            collect_plugin_data(root_plugin)
            context = {
                'plugin': root_plugin,
                'request': request,
                'clipboard': copy_to_clipboard,
                'cms_toolbar': toolbar,
            }
            tree_structure.append(template.render(context))
    tree_data.reverse()
    return json.dumps({
        'html': '\n'.join(tree_structure),
        'plugins': tree_data
    })
示例#26
0
 def test_copy_plugin_latest(self):
     post1 = self._get_post(self.data['en'][0])
     post2 = self._get_post(self.data['en'][1])
     tag = Tag.objects.create(name='tag 1')
     plugin = add_plugin(post1.content,
                         'BlogLatestEntriesPlugin',
                         language='en')
     plugin.tags.add(tag)
     plugins = list(
         post1.content.cmsplugin_set.filter(language='en').order_by(
             'tree_id', 'level', 'position'))
     copy_plugins_to(plugins, post2.content)
     new = downcast_plugins(post2.content.cmsplugin_set.all())
     self.assertEqual(set(new[0].tags.all()), set([tag]))
示例#27
0
 def render(self, context, instance, placeholder):
     context['instance'] = instance
     context['placeholder'] = placeholder
     if instance.plugin_id:
         plugins = instance.plugin.get_descendants(include_self=True).order_by('placeholder', 'tree_id', 'level',
                                                                               'position')
         plugins = downcast_plugins(plugins)
         plugins[0].parent_id = None
         plugins = build_plugin_tree(plugins)
         context['plugins'] = plugins
     if instance.alias_placeholder_id:
         content = render_placeholder(instance.alias_placeholder, context)
         context['content'] = mark_safe(content)
     return context
示例#28
0
 def test_post_with_parent(self):
     pagecontent1 = PageContentWithVersionFactory(
         template="page.html",
         page__node__depth=0,
         page__node__path="0001",
         page__node__numchild=1,
     )
     PageUrl.objects.create(
         slug="foo",
         path="foo",
         language=pagecontent1.language,
         page=pagecontent1.page,
     )
     pagecontent2 = PageContentWithVersionFactory(
         template="page.html",
         language=pagecontent1.language,
         page__node__parent_id=pagecontent1.page.node_id,
         page__node__depth=1,
         page__node__path="00010001",
     )
     placeholder = PlaceholderFactory(slot="content", source=pagecontent2)
     add_plugin(placeholder,
                "TextPlugin",
                pagecontent2.language,
                body="Test text")
     with self.login_user_context(self.get_superuser()):
         response = self.client.post(
             self.get_admin_url(PageContent, "duplicate_content",
                                pagecontent2.pk),
             data={
                 "site": Site.objects.first().pk,
                 "slug": "bar"
             },
             follow=True,
         )
     self.assertRedirects(response,
                          self.get_admin_url(PageContent, "changelist"))
     new_pagecontent = PageContent._base_manager.latest("pk")
     new_placeholder = new_pagecontent.placeholders.get(slot="content")
     self.assertEqual(PageContent._base_manager.count(), 3)
     self.assertNotEqual(pagecontent2, new_pagecontent)
     self.assertNotEqual(pagecontent2.page, new_pagecontent.page)
     self.assertEqual(pagecontent2.language, new_pagecontent.language)
     self.assertEqual(
         new_pagecontent.page.get_path(new_pagecontent.language), "foo/bar")
     new_plugins = list(downcast_plugins(
         new_placeholder.get_plugins_list()))
     self.assertEqual(len(new_plugins), 1)
     self.assertEqual(new_plugins[0].plugin_type, "TextPlugin")
     self.assertEqual(new_plugins[0].body, "Test text")
示例#29
0
 def render(self, context, instance, placeholder):
     context['instance'] = instance
     context['placeholder'] = placeholder
     if instance.plugin_id:
         plugins = instance.plugin.get_descendants().order_by('placeholder', 'path')
         plugins = [instance.plugin] + list(plugins)
         plugins = downcast_plugins(plugins)
         plugins[0].parent_id = None
         plugins = build_plugin_tree(plugins)
         context['plugins'] = plugins
     if instance.alias_placeholder_id:
         content = render_placeholder(instance.alias_placeholder, context)
         context['content'] = mark_safe(content)
     return context
示例#30
0
 def test_copy_plugin_latest(self):
     post1 = self._get_post(self._post_data[0]['en'])
     post2 = self._get_post(self._post_data[1]['en'])
     tag1 = Tag.objects.create(name='tag 1')
     tag2 = Tag.objects.create(name='tag 2')
     plugin = add_plugin(post1.content, 'BlogLatestEntriesPlugin', language='en', app_config=self.app_config_1)
     plugin.tags.add(tag1)
     plugin.tags.add(tag2)
     if CMS_30:
         plugins = list(post1.content.cmsplugin_set.filter(language='en').order_by('tree_id', 'level', 'position'))
     else:
         plugins = list(post1.content.cmsplugin_set.filter(language='en').order_by('path', 'depth', 'position'))
     copy_plugins_to(plugins, post2.content)
     new = downcast_plugins(post2.content.cmsplugin_set.all())
     self.assertEqual(set(new[0].tags.all()), set([tag1, tag2]))
     self.assertEqual(set(new[0].tags.all()), set(plugin.tags.all()))
示例#31
0
 def test_copy_plugin_latest(self):
     post1 = self._get_post(self._post_data[0]['en'])
     post2 = self._get_post(self._post_data[1]['en'])
     tag1 = Tag.objects.create(name='tag 1')
     tag2 = Tag.objects.create(name='tag 2')
     plugin = add_plugin(post1.content, 'BlogLatestEntriesPlugin', language='en', app_config=self.app_config_1)
     plugin.tags.add(tag1)
     plugin.tags.add(tag2)
     if CMS_30:
         plugins = list(post1.content.cmsplugin_set.filter(language='en').order_by('tree_id', 'level', 'position'))
     else:
         plugins = list(post1.content.cmsplugin_set.filter(language='en').order_by('path', 'depth', 'position'))
     copy_plugins_to(plugins, post2.content)
     new = downcast_plugins(post2.content.cmsplugin_set.all())
     self.assertEqual(set(new[0].tags.all()), set([tag1, tag2]))
     self.assertEqual(set(new[0].tags.all()), set(plugin.tags.all()))
示例#32
0
 def render(self, context, instance, placeholder):
     from cms.utils.plugins import downcast_plugins, build_plugin_tree
     context['instance'] = instance
     context['placeholder'] = placeholder
     if instance.plugin_id:
         plugins = instance.plugin.get_descendants().order_by(
             'placeholder', 'path')
         plugins = [instance.plugin] + list(plugins)
         plugins = downcast_plugins(plugins)
         plugins[0].parent_id = None
         plugins = build_plugin_tree(plugins)
         context['plugins'] = plugins
     if instance.alias_placeholder_id:
         content = render_placeholder(instance.alias_placeholder, context)
         context['content'] = mark_safe(content)
     return context
示例#33
0
    def get_form_elements(self):
        from .utils import get_nested_plugins

        if self.child_plugin_instances is None:
            # 3.1 and 3.0 compatibility
            if CMS_31:
                # default ordering is by path
                ordering = ('path', 'position')
            else:
                ordering = ('tree_id', 'level', 'position')
            self.child_plugin_instances = self.get_descendants().order_by(*ordering)

        if self._form_elements is None:
            children = get_nested_plugins(self)
            children_instances = downcast_plugins(children)
            self._form_elements = [plugin for plugin in children_instances if is_form_element(plugin)]
        return self._form_elements
示例#34
0
def get_plugin_tree(model, **kwargs):
    """
    Plugins in django CMS are highly related to a placeholder.

    This function builds a plugin tree for a plugin with no placeholder context.

    Makes as many database queries as many levels are in the tree.

    This is ok as forms shouldn't form very deep trees.
    """
    plugin = model.objects.get(**kwargs)
    plugin.parent = None
    current_level = [plugin]
    plugin_list = [plugin]
    while get_next_level(current_level).exists():
        current_level = get_next_level(current_level)
        current_level = downcast_plugins(queryset=current_level)
        plugin_list += current_level
    return build_plugin_tree(plugin_list)[0]
示例#35
0
def get_plugin_tree(model, **kwargs):
    """
    Plugins in django CMS are highly related to a placeholder.

    This function builds a plugin tree for a plugin with no placeholder context.

    Makes as many database queries as many levels are in the tree.

    This is ok as forms shouldn't form very deep trees.
    """
    plugin = model.objects.get(**kwargs)
    plugin.parent = None
    current_level = [plugin]
    plugin_list = [plugin]
    while get_next_level(current_level).exists():
        current_level = get_next_level(current_level)
        current_level = downcast_plugins(queryset=current_level)
        plugin_list += current_level
    return build_plugin_tree(plugin_list)[0]
    def test_detach_alias_correct_position(self):
        alias = self._create_alias([])
        alias_placeholder = alias.get_placeholder(self.language)
        add_plugin(
            alias_placeholder,
            'TextPlugin',
            language=self.language,
            body='test 1',
        )
        add_plugin(
            alias_placeholder,
            'TextPlugin',
            language=self.language,
            body='test 2',
        )
        plugins = self.placeholder.get_plugins()
        self.assertEqual(plugins.count(), 1)
        alias_plugin = add_plugin(
            self.placeholder,
            Alias,
            language=self.language,
            alias=alias,
        )
        add_plugin(
            self.placeholder,
            'TextPlugin',
            language=self.language,
            body='test 3',
        )
        self.assertEqual(plugins.count(), 3)
        Alias.detach_alias_plugin(alias_plugin, self.language)
        self.assertEqual(plugins.count(), 4)

        ordered_plugins = sorted(
            downcast_plugins(plugins),
            key=attrgetter('position'),
        )
        self.assertEqual(
            [str(plugin) for plugin in ordered_plugins],
            ['test', 'test 1', 'test 2', 'test 3'],
        )
def render_plugin(request, plugin_id, template_name='responsive_wrapper/render_plugin.html'):
    try:
        instance = CMSPlugin.objects.get(pk=hashid_to_int(plugin_id))
    except CMSPlugin.DoesNotExist:
        msg = _('Plugin not found.')
        return HttpResponseNotFound(force_text(msg))

    try:
        # django CMS 3-
        descendants = instance.get_descendants(include_self=True) \
            .order_by('placeholder', 'tree_id', 'level', 'position')
    except (FieldError, TypeError):
        # django CMS 3.1+
        descendants = instance.get_descendants().order_by('path')

    plugins = [instance] + list(descendants)
    plugins = downcast_plugins(plugins)
    plugins[0].parent_id = None
    plugins = build_plugin_tree(plugins)

    context = RequestContext(request)
    context['plugins'] = plugins
    return render_to_response(template_name, {}, context)
示例#38
0
def render_alias_plugin(context, instance):
    request = context['request']
    toolbar = get_toolbar_from_request(request)
    renderer = toolbar.content_renderer
    source = (instance.plugin or instance.alias_placeholder)

    # In edit mode, content is shown regardless of the source page publish status.
    # In published mode, content is shown only if the source page is published.
    if not (toolbar.edit_mode_active) and source and source.page:
        # this is bad but showing unpublished content is worse
        can_see_content = source.page.is_published(instance.language)
    else:
        can_see_content = True

    if can_see_content and instance.plugin:
        plugins = instance.plugin.get_descendants().order_by(
            'placeholder', 'path')
        plugins = [instance.plugin] + list(plugins)
        plugins = downcast_plugins(plugins, request=request)
        plugins = list(plugins)
        plugins[0].parent_id = None
        plugins = build_plugin_tree(plugins)
        content = renderer.render_plugin(
            instance=plugins[0],
            context=context,
            editable=False,
        )
        return mark_safe(content)

    if can_see_content and instance.alias_placeholder:
        content = renderer.render_placeholder(
            placeholder=instance.alias_placeholder,
            context=context,
            editable=False,
        )
        return mark_safe(content)
    return ''
示例#39
0
def get_moderated_children_from_placeholder(placeholder, parent_version_filters):
    """
    Get all moderated children version objects from a placeholder
    """
    for plugin in downcast_plugins(placeholder.get_plugins()):
        yield from _get_nested_moderated_children_from_placeholder_plugin(plugin, placeholder, parent_version_filters)