Skip to content

Commit

Permalink
fix for issue django-cms#576
Browse files Browse the repository at this point in the history
  • Loading branch information
fivethreeo committed Jun 15, 2011
1 parent eafe411 commit a7f264c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 16 deletions.
33 changes: 20 additions & 13 deletions cms/admin/placeholderadmin.py
Expand Up @@ -225,8 +225,26 @@ def move_plugin(self, request):
# only allow POST
if request.method != "POST":
return HttpResponse(str("error"))

if 'plugin_id' in request.POST: # single plugin moving
plugin = CMSPlugin.objects.get(pk=int(request.POST['plugin_id']))

if 'placeholder_id' in request.POST:
placeholder = Placeholder.objects.get(pk=int(request.POST['placeholder_id']))
else:
placeholder = plugin.placeholder

# check permissions
if not placeholder.has_change_permission(request):
raise Http404

# plugin positions are 0 based, so just using count here should give us 'last_position + 1'
position = CMSPlugin.objects.filter(placeholder=placeholder).count()
plugin.placeholder = placeholder
plugin.position = position
plugin.save()
pos = 0
if 'ids' in request.POST: # multiple plugins
if 'ids' in request.POST: # multiple plugins/ reordering
whitelisted_placeholders = []
for id in request.POST['ids'].split("_"):
plugin = CMSPlugin.objects.get(pk=id)
Expand All @@ -244,18 +262,7 @@ def move_plugin(self, request):
plugin.position = pos
plugin.save()
pos += 1
elif 'plugin_id' in request.POST: # single plugin moving
plugin = CMSPlugin.objects.get(pk=int(request.POST['plugin_id']))

# check permissions
if not plugin.placeholder.has_change_permission(request):
raise Http404

placeholder = plugin.placeholder
# plugin positions are 0 based, so just using count here should give us 'last_position + 1'
position = CMSPlugin.objects.filter(placeholder=placeholder).count()
plugin.position = position
plugin.save()

else:
HttpResponse(str("error"))
return HttpResponse(str("ok"))
Expand Down
10 changes: 8 additions & 2 deletions cms/media/cms/js/plugin_editor.js
Expand Up @@ -97,8 +97,14 @@
{
// moved to new placeholder
var plugin_id = ui.item.attr('id').split('plugin_')[1];
var target = ui.item.parent().parent().data('name');
$.post("move-plugin/", { placeholder: target, plugin_id: plugin_id, ids: d }, function(data){}, "json");
var slot_name = ui.item.parent().parent().data('name');
var placeholder_id = ui.item.parent().parent().data('id')
$.post("move-plugin/", {
placeholder: slot_name,
placeholder_id: placeholder_id,
plugin_id: plugin_id,
ids: d
}, function(data){}, "json");
}
else
{
Expand Down
4 changes: 3 additions & 1 deletion cms/media/cms/js/plugins/cms.placeholders.js
Expand Up @@ -379,6 +379,7 @@ jQuery(document).ready(function ($) {
var text = $('.cms_placeholder-bar[class$="cms_placeholder_slot::' + item + '"]').find('.cms_placeholder-title').text();
list.append($('<li><a href="">' +text + '</a></li>').data({
'slot': item,
'placeholder_id': values.placeholder,
'plugin_id': values.plugin_id
}));
});
Expand All @@ -388,11 +389,12 @@ jQuery(document).ready(function ($) {
e.preventDefault();
// save slot var
var slot = $(this).parent().data('slot');
var placeholder_id = $(this).parent().data('placeholder_id');
// now lets do the ajax request
$.ajax({
'type': 'POST',
'url': that.options.urls.cms_page_move_plugin,
'data': { 'placeholder': slot, 'plugin_id': $(this).parent().data('plugin_id') },
'data': { 'placeholder': slot, 'placeholder_id': placeholder_id, 'plugin_id': $(this).parent().data('plugin_id') },
'success': function () {
refreshPluginPosition(slot);
},
Expand Down
26 changes: 26 additions & 0 deletions cms/tests/placeholder.py
Expand Up @@ -120,6 +120,32 @@ def test_page_only_plugins(self):
self.assertEqual(response.status_code, 200)
self.assertNotContains(response, 'InheritPagePlaceholderPlugin')

def test_inter_placeholder_plugin_move(self):
ex = Example5(
char_1='one',
char_2='two',
char_3='tree',
char_4='four'
)
ex.save()
ph1 = ex.placeholder_1
ph2 = ex.placeholder_2
ph1_pl1 = add_plugin(ph1, TextPlugin, 'en', body='ph1 plugin1').cmsplugin_ptr
ph1_pl2 = add_plugin(ph1, TextPlugin, 'en', body='ph1 plugin2').cmsplugin_ptr
ph1_pl3 = add_plugin(ph1, TextPlugin, 'en', body='ph1 plugin3').cmsplugin_ptr
ph2_pl1 = add_plugin(ph2, TextPlugin, 'en', body='ph2 plugin1').cmsplugin_ptr
ph2_pl2 = add_plugin(ph2, TextPlugin, 'en', body='ph2 plugin2').cmsplugin_ptr
ph2_pl3 = add_plugin(ph2, TextPlugin, 'en', body='ph2 plugin3').cmsplugin_ptr
response = self.client.post(reverse('admin:placeholderapp_example5_move_plugin'), {
'placeholder': ph2.slot,
'placeholder_id': str(ph2.pk),
'plugin_id': str(ph1_pl2.pk),
'ids': "_".join([str(p.pk) for p in [ph2_pl1, ph1_pl2, ph2_pl2, ph2_pl3]])
})
self.assertEqual(response.status_code, 200)
self.assertEqual([ph1_pl1, ph1_pl3], list(ph1.cmsplugin_set.order_by('position')))
self.assertEqual([ph2_pl1, ph1_pl2, ph2_pl2, ph2_pl3], list(ph2.cmsplugin_set.order_by('position')))

def test_placeholder_scanning_fail(self):
self.assertRaises(TemplateSyntaxError, get_placeholders, 'placeholder_tests/test_eleven.html')

Expand Down

0 comments on commit a7f264c

Please sign in to comment.