question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Limit the number of nested plugins

See original GitHub issue

I know that django-cms allows to limit the number of plugins inside a placeholder, but I need to limit the number of the nested plugins. I spent some time on how to do and then I found a possible solution, that I call inception plugin, see below.

from cms.plugin_base import CMSPluginBase
from cms.plugin_pool import plugin_pool
from django.template.response import SimpleTemplateResponse
import models

class CMSPluginMaxPluginAllowed(CMSPluginBase):
    max_plugins = None

    def add_view(self, request, form_url='', extra_context=None):
        num_children = len([v for v in self.parent.get_children() if v.get_plugin_instance()[0] is not None])
        if self.max_plugins and num_children >= self.max_plugins:
                return SimpleTemplateResponse('djangocms_inception/max_reached.html', {
                    'num_allowed': self.max_plugins,
                })
        return super(CMSPluginMaxPluginAllowed, self).add_view(request, form_url, extra_context)

class ParentPlugin(CMSPluginBase):
    admin_preview = False
    model = models.ParentModel
    render_template = "djangocms_inception/parent.html"
    allow_children = True
    child_classes = ['ChildPlugin']

plugin_pool.register_plugin(ParentPlugin)

class ChildPlugin(CMSPluginMaxPluginAllowed):
    admin_preview = False
    model = models.ChildModel
    render_template = "djangocms_inception/child.html"
    require_parent = True
    parent_classes = ['ParentPlugin']
    allow_children = True
    child_classes = ['SweetChildPlugin']
    max_plugins = 3

plugin_pool.register_plugin(ChildPlugin)

class SweetChildPlugin(CMSPluginMaxPluginAllowed):
    admin_preview = False
    model = models.SweetChild
    name = "SweetChild"
    render_template = "djangocms_inception/sweetchild.html"
    require_parent = True
    parent_classes = ['ChildPlugin']
    max_plugins = 1

plugin_pool.register_plugin(SweetChildPlugin)

My thoughts are:

  • Is it a good choice using the add_view method?
  • Are there any other solutions?
  • It could be interesting to have this kind of feature in django-cms, what do you think about it?

Thanks!

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:3
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
joshyucommented, Jun 28, 2018

What is the progress about this issue? Is there a solution now?

3reactions
joeribekkercommented, Feb 8, 2017

I needed something similar and made a quick workaround by overriding the template. Simply add max_children = <number> to your Plugin class and create a new dragitem.html template in your templates folder templates/cms/toolbar/ to override the existing Django CMS template. See the diff below:

This nicely disabled the + icon when the maximum number of children has been reached.

--- env/lib/python3.5/site-packages/cms/templates/cms/toolbar/dragitem.html       2016-09-15 12:06:26.132803200 +0200
+++ templates/cms/toolbar/dragitem.html    2017-02-08 12:26:59.343312100 +0100
@@ -9,6 +9,17 @@
         {% if plugin.child_plugin_instances %} cms-dragitem-collapsable{% endif %}">
         {% language request.toolbar.toolbar_language %}
         {% if not disabled_child %}
+            {% with max_children=plugin.get_plugin_instance.1.max_children child_count=plugin.child_plugin_instances|length %}
+                {% if max_children %}
+                    <div class="cms-submenu-btn cms-submenu-add cms-btn
+                        {% if child_count >= max_children %} cms-btn-disabled{% endif %}">
+                        {% if child_count >= max_children %}
+                            <span class="cms-hover-tooltip" data-cms-tooltip="{% trans "You cannot add plugins to this plugin." %}"></span>
+                        {% else %}
+                            <span class="cms-hover-tooltip cms-hover-tooltip-left cms-hover-tooltip-delay" data-cms-tooltip="{% trans "Add plugin" %}"></span>
+                        {% endif %}
+                    </div>
+                {% else %}
             <div class="cms-submenu-btn cms-submenu-add cms-btn
                 {% if not allow_children %} cms-btn-disabled{% endif %}">
                 {% if not allow_children %}
@@ -17,6 +28,8 @@
                 <span class="cms-hover-tooltip cms-hover-tooltip-left cms-hover-tooltip-delay" data-cms-tooltip="{% trans "Add plugin" %}"></span>
                 {% endif %}
             </div>
+                {% endif %}
+            {% endwith %}
             <div class="cms-submenu-btn cms-submenu-edit cms-btn" data-rel="edit">
                 <span class="cms-hover-tooltip cms-hover-tooltip-left cms-hover-tooltip-delay" data-cms-tooltip="{% trans "Edit" %}"></span>
             </div>

I’ll wrap this as PR when I added a test.

Read more comments on GitHub >

github_iconTop Results From Across the Web

It's possible to limit the number of nested plugins in django-cms?
My plugin has child plugins but I wanna to limit the number of child plugins to a maximum of 2. It's possible to...
Read more >
Limit the number of nested plugins · Issue #5102 · django-cms ...
I know that django-cms allows to limit the number of plugins inside a placeholder, but I need to limit the number of the...
Read more >
It's possible to limit the number of nested plugins in django-cms?
Currently, this cannot be done in Django CMS for nested plugins. ... Simply add max_children = <number> to your Plugin class and create...
Read more >
Limit the number of nested subalbums | WordPress.org
Hi. Is there a way to limit the number of nested subalbums? For example I need to allow users to create “User's Main...
Read more >
Removing the limit on the number of nested comments
1 Answer 1 ... thread_comments_depth is the option you need to change, and while the dropdown gives you several predefined values, the option...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found