Skip to content

Commit

Permalink
Merge pull request #3825 from yakky/feature/plugin_order_publish
Browse files Browse the repository at this point in the history
Merge #3661
  • Loading branch information
yakky committed Feb 7, 2015
2 parents 379e120 + eee4107 commit 7189222
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 11 deletions.
7 changes: 1 addition & 6 deletions cms/models/pluginmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def copy_plugin(self, target_placeholder, target_language, parent_cache, no_sign
# get a new generic plugin instance
# assign the position in the plugin tree
# save it to let mptt calculate the tree attributes
# save it to let mptt/treebeard calculate the tree attributes
# then get a copy of the current plugin instance
# assign to it the id of the generic plugin instance above;
this will effectively change the generic plugin created above
Expand All @@ -278,15 +278,13 @@ def copy_plugin(self, target_placeholder, target_language, parent_cache, no_sign
new_plugin.placeholder = target_placeholder
# we assign a parent to our new plugin
parent_cache[self.pk] = new_plugin
parent = None
if self.parent:
parent = parent_cache[self.parent_id]
parent = CMSPlugin.objects.get(pk=parent.pk)
new_plugin.parent_id = parent.pk
new_plugin.parent = parent
new_plugin.language = target_language
new_plugin.plugin_type = self.plugin_type
new_plugin.position = CMSPlugin.objects.filter(parent=parent, language=target_language, placeholder=target_placeholder).count()
if no_signals:
from cms.signals import pre_save_plugins

Expand All @@ -306,11 +304,8 @@ def copy_plugin(self, target_placeholder, target_language, parent_cache, no_sign
plugin_instance.depth = new_plugin.depth
plugin_instance.path = new_plugin.path
plugin_instance.numchild = new_plugin.numchild
# added to retain the position when creating a public copy of a plugin
plugin_instance.position = new_plugin.position
plugin_instance._no_reorder = True
plugin_instance.save()
#new_plugin._inst = plugin_instance
old_instance = plugin_instance.__class__.objects.get(pk=self.pk)
plugin_instance.copy_relations(old_instance)
if no_signals:
Expand Down
8 changes: 5 additions & 3 deletions cms/test_utils/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,13 @@ def configure(db_url, **extra):
CMS_PLACEHOLDER_CONF={
'col_sidebar': {
'plugins': ('FilePlugin', 'FlashPlugin', 'LinkPlugin', 'PicturePlugin',
'TextPlugin', 'SnippetPlugin'),
'TextPlugin', 'MultiColumnPlugin', 'SnippetPlugin'),
'name': gettext("sidebar column")
},
'col_left': {
'plugins': ('FilePlugin', 'FlashPlugin', 'LinkPlugin', 'PicturePlugin',
'TextPlugin', 'SnippetPlugin', 'GoogleMapPlugin', 'MultiColumnPlugin', 'StylePlugin'),
'TextPlugin', 'SnippetPlugin', 'GoogleMapPlugin',
'MultiColumnPlugin', 'StylePlugin'),
'name': gettext("left column"),
'plugin_modules': {
'LinkPlugin': 'Different Grouper'
Expand All @@ -230,7 +231,8 @@ def configure(db_url, **extra):
},
'col_right': {
'plugins': ('FilePlugin', 'FlashPlugin', 'LinkPlugin', 'PicturePlugin',
'TextPlugin', 'SnippetPlugin', 'GoogleMapPlugin', 'MultiColumnPlugin', 'StylePlugin'),
'TextPlugin', 'SnippetPlugin', 'GoogleMapPlugin', 'MultiColumnPlugin',
'StylePlugin'),
'name': gettext("right column")
},
'extra_context': {
Expand Down
47 changes: 46 additions & 1 deletion cms/tests/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def test_plugin_order(self):
Test that plugin position is saved after creation
"""
page_en = api.create_page("PluginOrderPage", "col_two.html", "en",
slug="page1", published=True, in_navigation=True)
slug="page1", published=True, in_navigation=True)
ph_en = page_en.placeholders.get(slot="col_left")

# We check created objects and objects from the DB to be sure the position value
Expand All @@ -200,6 +200,51 @@ def test_plugin_order(self):
rendered_placeholder = ph_en.render(self.get_context(page_en.get_absolute_url(), page=page_en), None)
self.assertEqual(rendered_placeholder, "I'm the firstI'm the second")

def test_plugin_order_alt(self):
"""
Test that plugin position is saved after creation
"""
draft_page = api.create_page("PluginOrderPage", "col_two.html", "en",
slug="page1", published=False, in_navigation=True)
placeholder = draft_page.placeholders.get(slot="col_left")

# We check created objects and objects from the DB to be sure the position value
# has been saved correctly
text_plugin_2 = api.add_plugin(placeholder, "TextPlugin", "en", body="I'm the second")
text_plugin_3 = api.add_plugin(placeholder, "TextPlugin", "en", body="I'm the third")
# Publish to create a 'live' version
draft_page.publish('en')
draft_page = draft_page.reload()
placeholder = draft_page.placeholders.get(slot="col_left")

# Add a plugin and move it to the first position
text_plugin_1 = api.add_plugin(placeholder, "TextPlugin", "en", body="I'm the first")
data = {
'placeholder_id': placeholder.id,
'plugin_id': text_plugin_1.id,
'plugin_parent': '',
'plugin_language': 'en',
'plugin_order[]': [text_plugin_1.id, text_plugin_2.id, text_plugin_3.id],
}
self.client.post(URL_CMS_PLUGIN_MOVE, data)

draft_page.publish('en')
draft_page = draft_page.reload()
live_page = draft_page.get_public_object()
placeholder = draft_page.placeholders.get(slot="col_left")
live_placeholder = live_page.placeholders.get(slot="col_left")

with SettingsOverride(CMS_PERMISSION=False):
self.assertEqual(CMSPlugin.objects.get(pk=text_plugin_1.pk).position, 0)
self.assertEqual(CMSPlugin.objects.get(pk=text_plugin_2.pk).position, 1)
self.assertEqual(CMSPlugin.objects.get(pk=text_plugin_3.pk).position, 2)

## Finally we render the placeholder to test the actual content
rendered_placeholder = placeholder.render(self.get_context(draft_page.get_absolute_url(), page=draft_page), None)
self.assertEqual(rendered_placeholder, "I'm the firstI'm the secondI'm the third")
rendered_live_placeholder = live_placeholder.render(self.get_context(live_page.get_absolute_url(), page=live_page), None)
self.assertEqual(rendered_live_placeholder, "I'm the firstI'm the secondI'm the third")

def test_add_cancel_plugin(self):
"""
Test that you can cancel a new plugin before editing and
Expand Down
2 changes: 1 addition & 1 deletion cms/tests/static_placeholder.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def test_copy_plugin(self):
'source_plugin_id': sourceplugin.pk,
'target_language': 'en',
'target_placeholder_id': static_placeholder_target.draft.pk,
'targetplugin_id': targetplugin.pk,
'target_plugin_id': targetplugin.pk,
})
response = self.admin_class.copy_plugins(request)

Expand Down

0 comments on commit 7189222

Please sign in to comment.