def copy_plugins(self, request): """ POST request should have the following data: - source_language - source_placeholder_id - source_plugin_id (optional) - target_language - target_placeholder_id - target_plugin_id (optional, new parent) """ source_language = request.POST['source_language'] source_placeholder_id = request.POST['source_placeholder_id'] source_plugin_id = request.POST.get('source_plugin_id', None) target_language = request.POST['target_language'] target_placeholder_id = request.POST['target_placeholder_id'] target_plugin_id = request.POST.get('target_plugin_id', None) source_placeholder = get_object_or_404(Placeholder, pk=source_placeholder_id) target_placeholder = get_object_or_404(Placeholder, pk=target_placeholder_id) if not target_language or not target_language in get_language_list(): return HttpResponseBadRequest(force_text(_("Language must be set to a supported language!"))) if source_plugin_id: source_plugin = get_object_or_404(CMSPlugin, pk=source_plugin_id) reload_required = requires_reload(PLUGIN_COPY_ACTION, [source_plugin]) if source_plugin.plugin_type == "PlaceholderPlugin": # if it is a PlaceholderReference plugin only copy the plugins it references inst, cls = source_plugin.get_plugin_instance(self) plugins = inst.placeholder_ref.get_plugins_list() else: plugins = list( source_placeholder.cmsplugin_set.filter( path__startswith=source_plugin.path, depth__gte=source_plugin.depth).order_by('path') ) else: plugins = list( source_placeholder.cmsplugin_set.filter(language=source_language).order_by('path')) reload_required = requires_reload(PLUGIN_COPY_ACTION, plugins) if not self.has_copy_plugin_permission(request, source_placeholder, target_placeholder, plugins): return HttpResponseForbidden(force_text(_('You do not have permission to copy these plugins.'))) if target_placeholder.pk == request.toolbar.clipboard.pk and not source_plugin_id and not target_plugin_id: # if we copy a whole placeholder to the clipboard create PlaceholderReference plugin instead and fill it # the content of the source_placeholder. ref = PlaceholderReference() ref.name = source_placeholder.get_label() ref.plugin_type = "PlaceholderPlugin" ref.language = target_language ref.placeholder = target_placeholder ref.save() ref.copy_from(source_placeholder, source_language) else: copy_plugins.copy_plugins_to(plugins, target_placeholder, target_language, target_plugin_id) plugin_list = CMSPlugin.objects.filter(language=target_language, placeholder=target_placeholder).order_by( 'path') reduced_list = [] for plugin in plugin_list: reduced_list.append( { 'id': plugin.pk, 'type': plugin.plugin_type, 'parent': plugin.parent_id, 'position': plugin.position, 'desc': force_text(plugin.get_short_description()), 'language': plugin.language, 'placeholder_id': plugin.placeholder_id } ) self.post_copy_plugins(request, source_placeholder, target_placeholder, plugins) json_response = {'plugin_list': reduced_list, 'reload': reload_required} return HttpResponse(json.dumps(json_response), content_type='application/json')
def copy_plugins(self, request): """ POST request should have the following data: - source_language - source_placeholder_id - source_plugin_id (optional) - target_language - target_placeholder_id - target_plugin_id (optional, new parent) """ source_language = request.POST['source_language'] source_placeholder_id = request.POST['source_placeholder_id'] source_plugin_id = request.POST.get('source_plugin_id', None) target_language = request.POST['target_language'] target_placeholder_id = request.POST['target_placeholder_id'] target_plugin_id = request.POST.get('target_plugin_id', None) source_placeholder = get_object_or_404(Placeholder, pk=source_placeholder_id) target_placeholder = get_object_or_404(Placeholder, pk=target_placeholder_id) if not target_language or not target_language in get_language_list(): return HttpResponseBadRequest( force_text(_("Language must be set to a supported language!"))) copy_to_clipboard = target_placeholder.pk == request.toolbar.clipboard.pk if source_plugin_id: source_plugin = get_object_or_404(CMSPlugin, pk=source_plugin_id) reload_required = requires_reload(PLUGIN_COPY_ACTION, [source_plugin]) if source_plugin.plugin_type == "PlaceholderPlugin": # if it is a PlaceholderReference plugin only copy the plugins it references inst, cls = source_plugin.get_plugin_instance(self) plugins = inst.placeholder_ref.get_plugins_list() else: plugins = list(source_placeholder.get_plugins().filter( path__startswith=source_plugin.path, depth__gte=source_plugin.depth).order_by('path')) else: plugins = list( source_placeholder.get_plugins( language=source_language).order_by('path')) reload_required = requires_reload(PLUGIN_COPY_ACTION, plugins) if copy_to_clipboard: has_permissions = self.has_copy_plugins_permission( request, plugins) else: # Plugins are being copied from a placeholder in another language # using the "Copy from language" placeholder action. # Check if the user can copy plugins from source placeholder to # target placeholder. has_permissions = self.has_copy_from_placeholder_permission( request, source_placeholder, target_placeholder, plugins, ) if not has_permissions: return HttpResponseForbidden( force_text( _('You do not have permission to copy these plugins.'))) if copy_to_clipboard and not source_plugin_id and not target_plugin_id: # if we copy a whole placeholder to the clipboard create # PlaceholderReference plugin instead and fill it the content of the # source_placeholder. ref = PlaceholderReference() ref.name = source_placeholder.get_label() ref.plugin_type = "PlaceholderPlugin" ref.language = target_language ref.placeholder = target_placeholder ref.save() ref.copy_from(source_placeholder, source_language) else: copy_plugins.copy_plugins_to(plugins, target_placeholder, target_language, target_plugin_id) plugin_list = CMSPlugin.objects.filter( language=target_language, placeholder=target_placeholder).order_by('path') reduced_list = [] for plugin in plugin_list: reduced_list.append({ 'id': plugin.pk, 'type': plugin.plugin_type, 'parent': plugin.parent_id, 'position': plugin.position, 'desc': force_text(plugin.get_short_description()), 'language': plugin.language, 'placeholder_id': plugin.placeholder_id }) self.post_copy_plugins(request, source_placeholder, target_placeholder, plugins) # When this is executed we are in the admin class of the source placeholder # It can be a page or a model with a placeholder field. # Because of this we need to get the admin class instance of the # target placeholder and call post_copy_plugins() on it. # By doing this we make sure that both the source and target are # informed of the operation. target_placeholder_admin = self._get_attached_admin(target_placeholder) if (target_placeholder_admin and target_placeholder_admin.model != self.model): target_placeholder_admin.post_copy_plugins( request, source_placeholder=source_placeholder, target_placeholder=target_placeholder, plugins=plugins, ) json_response = { 'plugin_list': reduced_list, 'reload': reload_required } return HttpResponse(json.dumps(json_response), content_type='application/json')