8000 Revert "Removed default plugin creation for placeholders (#6468)" · fsbraun/django-cms@71f0d7f · GitHub
[go: up one dir, main page]

Skip to content

Commit 71f0d7f

Browse files
committed
Revert "Removed default plugin creation for placeholders (django-cms#6468)"
This reverts commit eef5cbb.
1 parent 650f31a commit 71f0d7f

File tree

4 files changed

+153
-3
lines changed

4 files changed

+153
-3
lines changed

cms/tests/test_permmod.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,28 @@ def test_slave_cannot_add_page_to_root(self):
139139
response = self.client.get(self.get_page_add_uri('en'))
140140
self.assertEqual(response.status_code, 403)
141141

142+
@override_settings(
143+
CMS_PLACEHOLDER_CONF={
144+
'col_left': {
145+
'default_plugins': [
146+
8000 {
147+
'plugin_type': 'TextPlugin',
148+
'values': {
149+
'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.'
150+
},
151+
},
152+
]
153+
},
154+
},
155+
)
156+
def test_default_plugins(self):
157+
with self.login_user_context(self.user_slave):
158+
self.assertEqual(CMSPlugin.objects.count(), 0)
159+
response = self.client.get(self.slave_page.get_absolute_url(), {'edit': 1})
160+
self.assertEqual(response.status_code, 200)
161+
self.assertEqual(CMSPlugin.objects.count(), 1)
162+
163+
142164
def test_super_can_add_plugin(self):
143165
self._add_plugin(self.user_super, page=self.slave_page)
144166

cms/tests/test_placeholder.py

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,14 @@ def test_get_placeholder_conf(self):
607607
'main': {
608608
'name': 'main content',
609609
'plugins': ['TextPlugin', 'LinkPlugin'],
610-
'require_parent': False,
610+
'default_plugins': [
611+
{
612+
'plugin_type': 'TextPlugin',
613+
'values': {
614+
'body': '<p>Some default text</p>'
615+
},
616+
},
617+
],
611618
},
612619
'layout/home.html main': {
613620
'name': 'main content with FilerImagePlugin and limit',
@@ -642,8 +649,8 @@ def test_get_placeholder_conf(self):
642649
returned = get_placeholder_conf('excluded_plugins', 'main', 'layout/other.html')
643650
self.assertEqual(returned, TEST_CONF['layout/other.html main']['excluded_plugins'])
644651
# test grandparent inherited value
645-
returned = get_placeholder_conf('require_parent', 'main', 'layout/other.html')
646-
self.assertEqual(returned, TEST_CONF['main']['require_parent'])
652+
returned = get_placeholder_conf('default_plugins', 'main', 'layout/other.html')
653+
self.assertEqual(returned, TEST_CONF['main']['default_plugins'])
647654
# test generic configuration
648655
returned = get_placeholder_conf('plugins', 'something')
649656
self.assertEqual(returned, TEST_CONF[None]['plugins'])
@@ -738,6 +745,79 @@ def test_placeholder_field_dynamic_slot_update(self):
738745
self.assertEqual(old_placeholder_1_plugin_count, current_placeholder_1_plugin_count)
739746
self.assertEqual(old_placeholder_2_plugin_count, current_placeholder_2_plugin_count)
740747

748+
def test_plugins_prepopulate(self):
749+
""" Tests prepopulate placeholder configuration """
750+
751+
conf = {
752+
'col_left': {
753+
'default_plugins' : [
754+
{
755+
'plugin_type':'TextPlugin',
756+
'values':{'body':'<p>en default body 1</p>'},
757+
},
758+
{
759+
'plugin_type':'TextPlugin',
760+
'values':{'body':'<p>en default body 2</p>'},
761+
},
762+
]
763+
},
764+
}
765+
with self.settings(CMS_PLACEHOLDER_CONF=conf):
766+
page = create_page('page_en', 'col_two.html', 'en')
767+
placeholder = page.get_placeholders("en").get(slot='col_left')
768+
context = SekizaiContext()
769+
context['request'] = self.get_request(language="en", page=page)
770+
# Our page should have "en default body 1" AND "en default body 2"
771+
content = _render_placeholder(placeholder, context)
772+
self.assertRegexpMatches(content, "^<p>en default body 1</p>\s*<p>en default body 2</p>$")
773+
774+
def test_plugins_children_prepopulate(self):
775+
"""
776+
Validate a default textplugin with a nested default link plugin
777+
"""
778+
779+
conf = {
780+
'col_left': {
781+
'default_plugins': [
782+
{
783+
'plugin_type': 'TextPlugin',
784+
'values': {
785+
'body': '<p>body %(_tag_child_1)s and %(_tag_child_2)s</p>'
786+
},
787+
'children': [
788+
{
789+
'plugin_type': 'LinkPlugin',
790+
'values': {
791+
'name': 'django',
792+
'external_link': 'https://www.djangoproject.com/'
793+
},
794+
},
795+
{
796+
'plugin_type': 'LinkPlugin',
797+
'values': {
798+
'name': 'django-cms',
799+
'external_link': 'https://www.django-cms.org'
800+
},
801+
},
802+
]
803+
},
804+
]
805+
},
806+
}
807+
808+
with self.settings(CMS_PLACEHOLDER_CONF=conf):
809+
page = create_page('page_en', 'col_two.html', 'en')
810+
placeholder = page.get_placeholders("en").get(slot='col_left')
811+
context = SekizaiContext()
812+
context['request'] = self.get_request(language="en", page=page)
813+
_render_placeholder(placeholder, context)
814+
plugins = placeholder.get_plugins_list()
815+
self.assertEqual(len(plugins), 3)
816+
self.assertEqual(plugins[0].plugin_type, 'TextPlugin')
817+
self.assertEqual(plugins[1].plugin_type, 'LinkPlugin')
818+
self.assertEqual(plugins[2].plugin_type, 'LinkPlugin')
819+
self.assertTrue(plugins[1].parent == plugins[2].parent and plugins[1].parent == plugins[0])
820+
741821
def test_placeholder_pk_thousands_format(self):
742822
page = create_page("page", "nav_playground.html", "en")
743823
title = page.get_content_obj("en")

cms/tests/test_rendering.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,7 @@ def test_inherit_placeholder_override(self):
723723
r = self.render(self.test_page5)
724724
self.assertEqual(r, '|' + self.test_data5['text_main'] + '|' + self.test_data5['text_sub'])
725725

726+
@override_settings(CMS_PLACEHOLDER_CONF={None: {'language_fallback': False}})
726727
def test_inherit_placeholder_queries(self):
727728
with self.assertNumQueries(FuzzyInt(6, 10)):
728729
r = self.render(self.test_page2)

cms/utils/plugins.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from collections import OrderedDict, defaultdict, deque
44
from copy import deepcopy
55
from functools import lru_cache
6+
from itertools import starmap
7+
from operator import itemgetter
68

79
from django.utils.encoding import force_str
810
from django.utils.translation import gettext as _
@@ -12,6 +14,7 @@
1214
from cms.plugin_base import CMSPluginBase
1315
from cms.plugin_pool import plugin_pool
1416
from cms.utils import get_language_from_request
17+
from cms.utils.permissions import has_plugin_permission
1518
from cms.utils.placeholder import get_placeholder_conf
1619

1720
logger = logging.getLogger(__name__)
@@ -91,6 +94,9 @@ def assign_plugins(request, placeholders, template=None, lang=None):
9194
.objects
9295
.filter(placeholder__in=placeholders, language=lang)
9396
)
97+
if not plugins:
98+
# Create default plugins if enabled
99+
plugins = create_default_plugins(request, placeholders, template, lang)
94100
plugins = downcast_plugins(plugins, placeholders, request=request)
95101

96102
# split the plugins up by placeholder
@@ -112,6 +118,47 @@ def assign_plugins(request, placeholders, template=None, lang=None):
112118
placeholder._plugins_cache = layered_plugins
113119

114120

121+
def create_default_plugins(request, placeholders, template, lang):
122+
"""
123+
Create all default plugins for the given ``placeholders`` if they have
124+
a "default_plugins" configuration value in settings.
125+
return all plugins, children, grandchildren (etc.) created
126+
"""
127+
from cms.api import add_plugin
128+
129+
def _create_default_plugins(placeholder, confs, parent=None):
130+
"""
131+
Auxillary function that builds all of a placeholder's default plugins
132+
at the current level and drives the recursion down the tree.
133+
Returns the plugins at the current level along with all descendants.
134+
"""
135+
plugins, descendants = [], []
136+
addable_confs = (conf for conf in confs
137+
if has_plugin_permission(request.user,
138+
conf['plugin_type'], 'add'))
139+
for conf in addable_confs:
140+
plugin = add_plugin(placeholder, conf['plugin_type'], lang,
141+
target=parent, **conf['values'])
142+
if 'children' in conf:
143+
args = placeholder, conf['children'], plugin
144+
descendants += _create_default_plugins(*args)
145+
plugin.notify_on_autoadd(request, conf)
146+
plugins.append(plugin)
147+
if parent:
148+
parent.notify_on_autoadd_children(request, conf, plugins)
149+
return plugins + descendants
150+
151+
unfiltered_confs = ((ph, get_placeholder_conf('default_plugins',
152+
ph.slot, template))
153+
for ph in placeholders)
154+
# Empty confs must be filtered before filtering on add permission
155+
mutable_confs = ((ph, default_plugin_confs)
156+
for ph, default_plugin_confs
157+
in filter(itemgetter(1), unfiltered_confs)
158+
if ph.has_change_permission(request.user))
159+
return sum(starmap(_create_default_plugins, mutable_confs), [])
160+
161+
115162
def get_plugins_as_layered_tree(plugins):
116163
"""
117164
Given an iterable of plugins ordered by position,

0 commit comments

Comments
 (0)
0