Skip to content

Commit

Permalink
Merge pull request #3900 from yakky/feature/reorder_plugins
Browse files Browse the repository at this point in the history
Move plugin reordering code to a function
  • Loading branch information
yakky committed Feb 24, 2015
2 parents c8f06b5 + 12acfe2 commit 4d19f46
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 23 deletions.
27 changes: 6 additions & 21 deletions cms/admin/placeholderadmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from django.core.exceptions import PermissionDenied
from django.db import router, transaction
from django.http import (HttpResponse, HttpResponseBadRequest,
HttpResponseForbidden, HttpResponseRedirect)
HttpResponseForbidden, HttpResponseRedirect)
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.template.defaultfilters import force_escape, escapejs
Expand All @@ -27,7 +27,7 @@
from cms.plugin_pool import plugin_pool
from cms.utils import copy_plugins, permissions, get_language_from_request, get_cms_setting
from cms.utils.i18n import get_language_list
from cms.utils.plugins import requires_reload, has_reached_plugin_limit
from cms.utils.plugins import requires_reload, has_reached_plugin_limit, reorder_plugins
from cms.utils.urlutils import admin_reverse


Expand Down Expand Up @@ -443,25 +443,10 @@ def move_plugin(self, request):
child.placeholder = placeholder
child.language = language
child.save()
plugins = CMSPlugin.objects.filter(parent=parent_id, placeholder=placeholder, language=language).order_by('position')
x = 0
for level_plugin in plugins:
if order:
x = 0
found = False
for pk in order:
if level_plugin.pk == int(pk):
level_plugin.position = x
level_plugin.save()
found = True
break
x += 1
if not found:
return HttpResponseBadRequest('order parameter did not have all plugins of the same level in it')
else:
level_plugin.position = x
level_plugin.save()
x += 1
plugins = reorder_plugins(placeholder, parent_id, language, order)
if not plugins:
return HttpResponseBadRequest('order parameter did not have all plugins of the same level in it')

self.post_move_plugin(request, source_placeholder, placeholder, plugin)
json_response = {'reload': requires_reload(PLUGIN_MOVE_ACTION, [plugin])}
return HttpResponse(json.dumps(json_response), content_type='application/json')
Expand Down
2 changes: 1 addition & 1 deletion cms/migrations/0010_migrate_use_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def backwards(apps, schema_editor):
class Migration(migrations.Migration):

dependencies = [
('cms', '0008_auto_20150208_2149'),
('cms', '0009_merge'),
]

operations = [
Expand Down
34 changes: 33 additions & 1 deletion cms/utils/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from django.utils.translation import ugettext as _

from cms.exceptions import PluginLimitReached
from cms.models import Page
from cms.models import Page, CMSPlugin
from cms.plugin_pool import plugin_pool
from cms.utils import get_language_from_request
from cms.utils.i18n import get_fallback_languages
Expand Down Expand Up @@ -168,6 +168,38 @@ def downcast_plugins(queryset, placeholders=None, select_placeholder=False):
return [plugin_lookup.get(plugin.pk, plugin) for plugin in queryset]


def reorder_plugins(placeholder, parent_id, language, order):
"""
Reorder the plugins according the order parameter
:param placeholder: placeholder instance which contains the given plugins
:param parent_id: parent of the given plugins
:param language: language
:param order: optional custom order (given as list of plugin primary keys)
"""
plugins = CMSPlugin.objects.filter(parent=parent_id, placeholder=placeholder,
language=language).order_by('position')
x = 0
for level_plugin in plugins:
if order:
x = 0
found = False
for pk in order:
if level_plugin.pk == int(pk):
level_plugin.position = x
level_plugin.save()
found = True
break
x += 1
if not found:
return False
else:
level_plugin.position = x
level_plugin.save()
x += 1
return plugins


def get_plugins_for_page(request, page, lang=None):
if not page:
return []
Expand Down

0 comments on commit 4d19f46

Please sign in to comment.