Skip to content

Remove default plugin creation #6468

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions cms/tests/test_permmod.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,27 +186,6 @@ def test_slave_can_add_page_under_slave_home(self):
# approve / publish as user_slave
# user master should be able to approve as well

@override_settings(
CMS_PLACEHOLDER_CONF={
'col_left': {
'default_plugins': [
{
'plugin_type': 'TextPlugin',
'values': {
'body': 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Culpa, repellendus, delectus, quo quasi ullam inventore quod quam aut voluptatum aliquam voluptatibus harum officiis officia nihil minus unde accusamus dolorem repudiandae.'
},
},
]
},
},
)
def test_default_plugins(self):
with self.login_user_context(self.user_slave):
self.assertEqual(CMSPlugin.objects.count(), 0)
response = self.client.get(self.slave_page.get_absolute_url(), {'edit': 1})
self.assertEqual(response.status_code, 200)
self.assertEqual(CMSPlugin.objects.count(), 1)

def test_page_added_by_slave_can_be_published_by_user_master(self):
# add page
page = create_page("page", "nav_playground.html", "en",
Expand Down
86 changes: 3 additions & 83 deletions cms/tests/test_placeholder.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,14 +457,7 @@ def test_get_placeholder_conf(self):
'main': {
'name': 'main content',
'plugins': ['TextPlugin', 'LinkPlugin'],
'default_plugins': [
{
'plugin_type': 'TextPlugin',
'values': {
'body': '<p>Some default text</p>'
},
},
],
'require_parent': False,
},
'layout/home.html main': {
'name': u'main content with FilerImagePlugin and limit',
Expand Down Expand Up @@ -499,8 +492,8 @@ def test_get_placeholder_conf(self):
returned = get_placeholder_conf('excluded_plugins', 'main', 'layout/other.html')
self.assertEqual(returned, TEST_CONF['layout/other.html main']['excluded_plugins'])
# test grandparent inherited value
returned = get_placeholder_conf('default_plugins', 'main', 'layout/other.html')
self.assertEqual(returned, TEST_CONF['main']['default_plugins'])
returned = get_placeholder_conf('require_parent', 'main', 'layout/other.html')
self.assertEqual(returned, TEST_CONF['main']['require_parent'])
# test generic configuration
returned = get_placeholder_conf('plugins', 'something')
self.assertEqual(returned, TEST_CONF[None]['plugins'])
Expand Down Expand Up @@ -595,79 +588,6 @@ def test_placeholder_field_dynamic_slot_update(self):
self.assertEqual(old_placeholder_1_plugin_count, current_placeholder_1_plugin_count)
self.assertEqual(old_placeholder_2_plugin_count, current_placeholder_2_plugin_count)

def test_plugins_prepopulate(self):
""" Tests prepopulate placeholder configuration """

conf = {
'col_left': {
'default_plugins' : [
{
'plugin_type':'TextPlugin',
'values':{'body':'<p>en default body 1</p>'},
},
{
'plugin_type':'TextPlugin',
'values':{'body':'<p>en default body 2</p>'},
},
]
},
}
with self.settings(CMS_PLACEHOLDER_CONF=conf):
page = create_page('page_en', 'col_two.html', 'en')
placeholder = page.get_placeholders("en").get(slot='col_left')
context = SekizaiContext()
context['request'] = self.get_request(language="en", page=page)
# Our page should have "en default body 1" AND "en default body 2"
content = _render_placeholder(placeholder, context)
self.assertRegexpMatches(content, "^<p>en default body 1</p>\s*<p>en default body 2</p>$")

def test_plugins_children_prepopulate(self):
"""
Validate a default textplugin with a nested default link plugin
"""

conf = {
'col_left': {
'default_plugins': [
{
'plugin_type': 'TextPlugin',
'values': {
'body': '<p>body %(_tag_child_1)s and %(_tag_child_2)s</p>'
},
'children': [
{
'plugin_type': 'LinkPlugin',
'values': {
'name': 'django',
'external_link': 'https://www.djangoproject.com/'
},
},
{
'plugin_type': 'LinkPlugin',
'values': {
'name': 'django-cms',
'external_link': 'https://www.django-cms.org'
},
},
]
},
]
},
}

with self.settings(CMS_PLACEHOLDER_CONF=conf):
page = create_page('page_en', 'col_two.html', 'en')
placeholder = page.get_placeholders("en").get(slot='col_left')
context = SekizaiContext()
context['request'] = self.get_request(language="en", page=page)
_render_placeholder(placeholder, context)
plugins = placeholder.get_plugins_list()
self.assertEqual(len(plugins), 3)
self.assertEqual(plugins[0].plugin_type, 'TextPlugin')
self.assertEqual(plugins[1].plugin_type, 'LinkPlugin')
self.assertEqual(plugins[2].plugin_type, 'LinkPlugin')
self.assertTrue(plugins[1].parent == plugins[2].parent and plugins[1].parent == plugins[0])

def test_placeholder_pk_thousands_format(self):
page = create_page("page", "nav_playground.html", "en", published=True)
title = page.get_title_obj("en")
Expand Down
1 change: 0 additions & 1 deletion cms/tests/test_rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,6 @@ def test_inherit_placeholder_override(self):
r = self.render(self.test_page5)
self.assertEqual(r, u'|' + self.test_data5['text_main'] + '|' + self.test_data5['text_sub'])

@override_settings(CMS_PLACEHOLDER_CONF={None: {'language_fallback': False}})
def test_inherit_placeholder_queries(self):
with self.assertNumQueries(FuzzyInt(6,8)):
r = self.render(self.test_page2)
Expand Down
47 changes: 0 additions & 47 deletions cms/utils/plugins.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# -*- coding: utf-8 -*-
from copy import deepcopy
from collections import defaultdict, deque, OrderedDict
from itertools import starmap
from operator import itemgetter

from django.utils.encoding import force_text
from django.utils.lru_cache import lru_cache
Expand All @@ -12,7 +10,6 @@
from cms.models.pluginmodel import CMSPlugin
from cms.plugin_pool import plugin_pool
from cms.utils import get_language_from_request
from cms.utils.permissions import has_plugin_permission
from cms.utils.placeholder import get_placeholder_conf


Expand Down Expand Up @@ -49,9 +46,6 @@ def assign_plugins(request, placeholders, template=None, lang=None):
.objects
.filter(placeholder__in=placeholders, language=lang)
)
if not plugins:
# Create default plugins if enabled
plugins = create_default_plugins(request, placeholders, template, lang)
plugins = downcast_plugins(plugins, placeholders, request=request)

# split the plugins up by placeholder
Expand All @@ -73,47 +67,6 @@ def assign_plugins(request, placeholders, template=None, lang=None):
setattr(placeholder, '_plugins_cache', layered_plugins)


def create_default_plugins(request, placeholders, template, lang):
"""
Create all default plugins for the given ``placeholders`` if they have
a "default_plugins" configuration value in settings.
return all plugins, children, grandchildren (etc.) created
"""
from cms.api import add_plugin

def _create_default_plugins(placeholder, confs, parent=None):
"""
Auxillary function that builds all of a placeholder's default plugins
at the current level and drives the recursion down the tree.
Returns the plugins at the current level along with all descendants.
"""
plugins, descendants = [], []
addable_confs = (conf for conf in confs
if has_plugin_permission(request.user,
conf['plugin_type'], 'add'))
for conf in addable_confs:
plugin = add_plugin(placeholder, conf['plugin_type'], lang,
target=parent, **conf['values'])
if 'children' in conf:
args = placeholder, conf['children'], plugin
descendants += _create_default_plugins(*args)
plugin.notify_on_autoadd(request, conf)
plugins.append(plugin)
if parent:
parent.notify_on_autoadd_children(request, conf, plugins)
return plugins + descendants

unfiltered_confs = ((ph, get_placeholder_conf('default_plugins',
ph.slot, template))
for ph in placeholders)
# Empty confs must be filtered before filtering on add permission
mutable_confs = ((ph, default_plugin_confs)
for ph, default_plugin_confs
in filter(itemgetter(1), unfiltered_confs)
if ph.has_change_permission(request.user))
return sum(starmap(_create_default_plugins, mutable_confs), [])


def get_plugins_as_layered_tree(plugins):
"""
Given an iterable of plugins ordered by position,
Expand Down
84 changes: 1 addition & 83 deletions docs/reference/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,6 @@ Example::
'text_only_plugins': ['LinkPlugin'],
'extra_context': {"width":640},
'name': gettext("Content"),
'language_fallback': True,
'default_plugins': [
{
'plugin_type': 'TextPlugin',
'values': {
'body':'<p>Lorem ipsum dolor sit amet...</p>',
},
},
],
'child_classes': {
'TextPlugin': ['PicturePlugin', 'LinkPlugin'],
},
Expand Down Expand Up @@ -288,76 +279,6 @@ matches; if the same configuration is retrieved for the ``content`` placeholder
this placeholder regardless of type (takes precedence over the
type-specific limits).

``language_fallback``
When ``True``, if the placeholder has no plugin for the current language
it falls back to the fallback languages as specified in :setting:`CMS_LANGUAGES`.
Defaults to ``True`` since version 3.1.

.. _placeholder_default_plugins:

``default_plugins``
You can specify the list of default plugins which will be automatically
added when the placeholder will be created (or rendered).
Each element of the list is a dictionary with following keys :

``plugin_type``
The plugin type to add to the placeholder
Example : ``TextPlugin``

``values``
Dictionary to use for the plugin creation.
It depends on the ``plugin_type``. See the documentation of each
plugin type to see which parameters are required and available.
Example for a text plugin:
``{'body':'<p>Lorem ipsum</p>'}``
Example for a link plugin:
``{'name':'Django-CMS','url':'https://www.django-cms.org'}``

``children``
It is a list of dictionaries to configure default plugins
to add as children for the current plugin (it must accepts children).
Each dictionary accepts same args than dictionaries of
``default_plugins`` : ``plugin_type``, ``values``, ``children``
(yes, it is recursive).

Complete example of default_plugins usage::

CMS_PLACEHOLDER_CONF = {
'content': {
'name' : _('Content'),
'plugins': ['TextPlugin', 'LinkPlugin'],
'default_plugins':[
{
'plugin_type':'TextPlugin',
'values':{
'body':'<p>Great websites : %(_tag_child_1)s and %(_tag_child_2)s</p>'
},
'children':[
{
'plugin_type':'LinkPlugin',
'values':{
'name':'django',
'url':'https://www.djangoproject.com/'
},
},
{
'plugin_type':'LinkPlugin',
'values':{
'name':'django-cms',
'url':'https://www.django-cms.org'
},
# If using LinkPlugin from djangocms-link which
# accepts children, you could add some grandchildren :
# 'children' : [
# ...
# ]
},
]
},
]
}
}

``plugin_modules``
A dictionary of plugins and custom module names to group plugin in the
toolbar UI.
Expand Down Expand Up @@ -595,10 +516,7 @@ will redirect to the URL of the same page in the fallback language. If
``False``, the content will be displayed in the fallback language, but there
will be no redirect.

Note that this applies to the fallback behaviour of *pages*. Starting for 3.1 *placeholders*
**will** default to the same behaviour. If you do not want a placeholder to follow a page's
fallback behaviour, you must set its ``language_fallback`` to ``False``
in :setting:`CMS_PLACEHOLDER_CONF`, above.
Note that this applies to the fallback behaviour of *pages*.

type
Boolean
Expand Down
2 changes: 1 addition & 1 deletion docs/upgrade/3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ Placeholders are pre-fillable with default plugins

In CMS_PLACEHOLDER_CONF, for each placeholder configuration, you can specify
via 'default_plugins' a list of plugins to automatically add to the
placeholder if empty. See :ref:`default_plugins in CMS_PLACEHOLDER_CONF <placeholder_default_plugins>`.
placeholder if empty. See default_plugins in CMS_PLACEHOLDER_CONF.

Custom modules and plugin labels in the toolbar UI
==================================================
Expand Down