Skip to content

Commit

Permalink
simplifying get_parent_plugins and centralizing filter logic for pare…
Browse files Browse the repository at this point in the history
…nt_classes under get_toolbar_plugin_struct. renamed child_only to require_parent as the wording makes more sense in context of the other PluginBase attributes
  • Loading branch information
sbussetti committed Aug 27, 2013
1 parent e596067 commit 00183de
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 22 deletions.
13 changes: 5 additions & 8 deletions cms/middleware/toolbar.py
Expand Up @@ -16,16 +16,13 @@ def toolbar_plugin_processor(instance, placeholder, rendered_content, original_c
template = None
if placeholder and placeholder.page:
template = placeholder.page.template
if instance.get_plugin_class().allow_children:
plugin_class = instance.get_plugin_class()
if plugin_class.allow_children:
instance, plugin = instance.get_plugin_instance()
childs = []
for child in [plugin_pool.get_plugin(cls) for cls in plugin.get_child_classes(placeholder, original_context['request'].current_page)]:
parents = child().get_parent_classes(placeholder, original_context['request'].current_page)
if plugin.__class__.__name__ in parents:
childs.append(child)

page = original_context['request'].current_page
childs = [plugin_pool.get_plugin(cls) for cls in plugin.get_child_classes(placeholder, page)]
# Builds the list of dictionaries containing module, name and value for the plugin dropdowns
child_plugin_classes = get_toolbar_plugin_struct(childs, placeholder.slot, template)
child_plugin_classes = get_toolbar_plugin_struct(childs, placeholder.slot, template, parent=(plugin_class, page))
data = {
'instance': instance,
'rendered_content': rendered_content,
Expand Down
5 changes: 2 additions & 3 deletions cms/plugin_base.py
Expand Up @@ -108,7 +108,7 @@ class CMSPluginBase(with_metaclass(CMSPluginBaseMetaclass, admin.ModelAdmin)):
allow_children = False
child_classes = None

child_only = False
require_parent = False
parent_classes = None

opts = {}
Expand Down Expand Up @@ -272,8 +272,7 @@ def get_parent_classes(self, slot, page):
if self.parent_classes:
return self.parent_classes
else:
installed_plugins = plugin_pool.get_all_plugins(slot, page)
return [cls.__name__ for cls in installed_plugins]
return None

def get_action_options(self):
return self.action_options
Expand Down
4 changes: 2 additions & 2 deletions cms/plugin_pool.py
Expand Up @@ -66,7 +66,7 @@ def unregister_plugin(self, plugin):
)
del self.plugins[plugin_name]

def get_all_plugins(self, placeholder=None, page=None, setting_key="plugins", include_page_only=True, include_child_only=False):
def get_all_plugins(self, placeholder=None, page=None, setting_key="plugins", include_page_only=True, include_require_parent=False):
self.discover_plugins()
plugins = list(self.plugins.values())
plugins.sort(key=lambda obj: force_unicode(obj.name))
Expand All @@ -90,7 +90,7 @@ def get_all_plugins(self, placeholder=None, page=None, setting_key="plugins", in
include_plugin = True
if plugin.page_only and not include_page_only:
include_plugin = False
if plugin.child_only and not include_child_only:
if plugin.require_parent and not include_require_parent:
include_plugin = False
if include_plugin:
final_plugins.append(plugin)
Expand Down
12 changes: 4 additions & 8 deletions cms/templatetags/cms_tags.py
Expand Up @@ -325,16 +325,12 @@ def get_context(self, context, plugin):
page = request.current_page
slot = context['slot']
child_plugin_classes = []
if plugin.get_plugin_class().allow_children:
plugin_class = plugin.get_plugin_class()
if plugin_class.allow_children:
instance, plugin = plugin.get_plugin_instance()
childs = []
for child in [plugin_pool.get_plugin(cls) for cls in plugin.get_child_classes(slot, page)]:
parents = child().get_parent_classes(slot, page)
if plugin.__class__.__name__ in parents:
childs.append(child)

childs = [plugin_pool.get_plugin(cls) for cls in plugin.get_child_classes(slot, page)]
# Builds the list of dictionaries containing module, name and value for the plugin dropdowns
child_plugin_classes = get_toolbar_plugin_struct(childs, slot, page.template)
child_plugin_classes = get_toolbar_plugin_struct(childs, slot, page.template, parent=(plugin_class, page))
return {'plugin_classes': child_plugin_classes}


Expand Down
11 changes: 10 additions & 1 deletion cms/utils/placeholder.py
Expand Up @@ -8,7 +8,7 @@
from django.db.models.query_utils import Q


def get_toolbar_plugin_struct(plugins_list, slot, template):
def get_toolbar_plugin_struct(plugins_list, slot, template, parent=None):
"""
Return the list of plugins to render in the toolbar.
The dictionary contains the label, the classname and the module for the
Expand All @@ -19,10 +19,19 @@ def get_toolbar_plugin_struct(plugins_list, slot, template):
:param plugins_list: list of plugins valid for the placeholder
:param slot: placeholder slot name
:param template: template name
:param parent: a 2-tuple consiting of: (parent plugin class, placeholder, page) if any
:return: list of dictionaries
"""
main_list = []
for plugin in plugins_list:
if parent:
parent_plugin_cls, page = parent
allowed_parents = plugin().get_parent_classes(slot, page)
## skip to the next if this plugin is not allowed to be a child
## of the parent
if allowed_parents and parent_plugin_cls.__name__ not in allowed_parents:
continue

modules = get_placeholder_conf("plugin_modules", slot, template, default={})
names = get_placeholder_conf("plugin_labes", slot, template, default={})
main_list.append({'value': plugin.value,
Expand Down

0 comments on commit 00183de

Please sign in to comment.