From 00183de3106e5fadc0aecd8a1909c03c6c75812d Mon Sep 17 00:00:00 2001 From: sbussetti Date: Tue, 27 Aug 2013 19:52:56 -0400 Subject: [PATCH] simplifying get_parent_plugins and centralizing filter logic for parent_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 --- cms/middleware/toolbar.py | 13 +++++-------- cms/plugin_base.py | 5 ++--- cms/plugin_pool.py | 4 ++-- cms/templatetags/cms_tags.py | 12 ++++-------- cms/utils/placeholder.py | 11 ++++++++++- 5 files changed, 23 insertions(+), 22 deletions(-) diff --git a/cms/middleware/toolbar.py b/cms/middleware/toolbar.py index fc987495978..08a1603d5dc 100755 --- a/cms/middleware/toolbar.py +++ b/cms/middleware/toolbar.py @@ -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, diff --git a/cms/plugin_base.py b/cms/plugin_base.py index 298b8efe2b3..220cce44e80 100644 --- a/cms/plugin_base.py +++ b/cms/plugin_base.py @@ -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 = {} @@ -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 diff --git a/cms/plugin_pool.py b/cms/plugin_pool.py index 98dcc93412d..2ce562c410f 100644 --- a/cms/plugin_pool.py +++ b/cms/plugin_pool.py @@ -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)) @@ -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) diff --git a/cms/templatetags/cms_tags.py b/cms/templatetags/cms_tags.py index 7efe9e1bf4a..db1794f03b7 100644 --- a/cms/templatetags/cms_tags.py +++ b/cms/templatetags/cms_tags.py @@ -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} diff --git a/cms/utils/placeholder.py b/cms/utils/placeholder.py index 743e381c5e1..a0d92c33b4d 100644 --- a/cms/utils/placeholder.py +++ b/cms/utils/placeholder.py @@ -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 @@ -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,