Limit the number of nested plugins
See original GitHub issueI 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:
- Created 7 years ago
- Reactions:3
- Comments:13 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
What is the progress about this issue? Is there a solution now?
I needed something similar and made a quick workaround by overriding the template. Simply add
max_children = <number>
to yourPlugin
class and create a newdragitem.html
template in your templates foldertemplates/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.
I’ll wrap this as PR when I added a test.