Beispiel #1
0
    def add_plugin(self, request):
        """
        Shows the add plugin form and saves it on POST.

        Requires the following GET parameters:
            - placeholder_id
            - plugin_type
            - plugin_language
            - plugin_parent (optional)
            - plugin_position (optional)
        """
        form = PluginAddValidationForm(request.GET)

        if not form.is_valid():
            # list() is necessary for python 3 compatibility.
            # errors is s dict mapping fields to a list of errors
            # for that field.
            error = list(form.errors.values())[0][0]
            return HttpResponseBadRequest(force_text(error))

        plugin_data = form.cleaned_data
        placeholder = plugin_data['placeholder_id']
        plugin_type = plugin_data['plugin_type']

        if not self.has_add_plugin_permission(request, placeholder, plugin_type):
            message = force_text(_('You do not have permission to add a plugin'))
            return HttpResponseForbidden(message)

        parent = plugin_data.get('plugin_parent')

        if parent:
            position = parent.cmsplugin_set.count()
        else:
            position = CMSPlugin.objects.filter(
                parent__isnull=True,
                language=plugin_data['plugin_language'],
                placeholder=placeholder,
            ).count()

        plugin_data['position'] = position

        plugin_class = plugin_pool.get_plugin(plugin_type)
        plugin_instance = plugin_class(plugin_class.model, self.admin_site)

        # Setting attributes on the form class is perfectly fine.
        # The form class is created by modelform factory every time
        # this get_form() method is called.
        plugin_instance._cms_initial_attributes = {
            'language': plugin_data['plugin_language'],
            'placeholder': plugin_data['placeholder_id'],
            'parent': plugin_data.get('plugin_parent', None),
            'plugin_type': plugin_data['plugin_type'],
            'position': plugin_data['position'],
        }

        response = plugin_instance.add_view(request)

        if request.method == "POST" and plugin_instance.object_successfully_changed:
            self.post_add_plugin(request, plugin_instance.saved_object)
        return response
Beispiel #2
0
    def validate_add_request(self, request):
        from cms.admin.forms import PluginAddValidationForm

        if getattr(self, "cms_plugin_instance"):
            # cms_plugin_instance points to an instance of CMSPlugin
            # that has no real instance aka a "ghost plugin".
            # This can easily happen in <= CMS 3.2 if the user
            # adds a plugin and then reloads the page without canceling
            # or submitting the form.
            # No need to validate the data in this plugin because
            # it's already been created.
            plugin = self.cms_plugin_instance
            plugin_data = {
                'placeholder_id': plugin.placeholder,
                'plugin_language': plugin.language,
                'plugin_parent': plugin.parent,
                'plugin_position': plugin.position,
                'plugin_type': plugin.plugin_type,
            }
        else:
            form = PluginAddValidationForm(
                data=request.GET,
                plugin_type=self.__class__.__name__,
            )

            if form.is_valid():
                plugin_data = form.cleaned_data
                plugin_data['plugin_type'] = form.plugin_type
            else:
                # list() is necessary for python 3 compatibility.
                # errors is s dict mapping fields to a list of errors
                # for that field.
                error = list(form.errors.values())[0][0]
                raise ValidationError(message=force_text(error))

        if not plugin_data['placeholder_id'].has_add_permission(request):
            # No need to run self.has_add_permission(request)
            # This method (validate_add_request) is called on get_form
            # and get_form is called after Django checks permissions.
            raise PermissionDenied

        parent = plugin_data.get('plugin_parent')

        if parent:
            position = parent.cmsplugin_set.count()
        else:
            position = CMSPlugin.objects.filter(
                parent__isnull=True,
                language=plugin_data['plugin_language'],
                placeholder=plugin_data['placeholder_id'],
            ).count()

        plugin_data['position'] = position
        return plugin_data
Beispiel #3
0
    def validate_add_request(self, request):
        from cms.admin.forms import PluginAddValidationForm

        if getattr(self, "cms_plugin_instance"):
            # cms_plugin_instance points to an instance of CMSPlugin
            # that has no real instance aka a "ghost plugin".
            # This can easily happen in <= CMS 3.2 if the user
            # adds a plugin and then reloads the page without canceling
            # or submitting the form.
            # No need to validate the data in this plugin because
            # it's already been created.
            plugin = self.cms_plugin_instance
            plugin_data = {
                'placeholder_id': plugin.placeholder,
                'plugin_language': plugin.language,
                'plugin_parent': plugin.parent,
                'plugin_position': plugin.position,
                'plugin_type': plugin.plugin_type,
            }
        else:
            form = PluginAddValidationForm(
                data=request.GET,
                plugin_type=self.__class__.__name__,
            )

            if form.is_valid():
                plugin_data = form.cleaned_data
                plugin_data['plugin_type'] = form.plugin_type
            else:
                # list() is necessary for python 3 compatibility.
                # errors is s dict mapping fields to a list of errors
                # for that field.
                error = list(form.errors.values())[0][0]
                raise ValidationError(message=force_text(error))

        if not plugin_data['placeholder_id'].has_add_permission(request):
            # No need to run self.has_add_permission(request)
            # This method (validate_add_request) is called on get_form
            # and get_form is called after Django checks permissions.
            raise PermissionDenied

        parent = plugin_data.get('plugin_parent')

        if parent:
            position = parent.cmsplugin_set.count()
        else:
            position = CMSPlugin.objects.filter(
                parent__isnull=True,
                language=plugin_data['plugin_language'],
                placeholder=plugin_data['placeholder_id'],
            ).count()

        plugin_data['position'] = position
        return plugin_data
    def add_plugin(self, request):
        """
        Shows the add plugin form and saves it on POST.

        Requires the following GET parameters:
            - cms_path
            - placeholder_id
            - plugin_type
            - plugin_language
            - plugin_parent (optional)
            - plugin_position (optional)
        """
        form = PluginAddValidationForm(request.GET)

        if not form.is_valid():
            # list() is necessary for python 3 compatibility.
            # errors is s dict mapping fields to a list of errors
            # for that field.
            error = list(form.errors.values())[0][0]
            return HttpResponseBadRequest(conditional_escape(force_str(error)))

        plugin_data = form.cleaned_data
        placeholder = plugin_data['placeholder_id']
        plugin_type = plugin_data['plugin_type']

        if not self.has_add_plugin_permission(request, placeholder, plugin_type):
            message = force_str(_('You do not have permission to add a plugin'))
            return HttpResponseForbidden(message)

        parent = plugin_data.get('plugin_parent')

        if parent:
            position = parent.cmsplugin_set.count()
        else:
            position = CMSPlugin.objects.filter(
                parent__isnull=True,
                language=plugin_data['plugin_language'],
                placeholder=placeholder,
            ).count()

        plugin_data['position'] = position

        plugin_class = plugin_pool.get_plugin(plugin_type)
        plugin_instance = plugin_class(plugin_class.model, self.admin_site)

        # Setting attributes on the form class is perfectly fine.
        # The form class is created by modelform factory every time
        # this get_form() method is called.
        plugin_instance._cms_initial_attributes = {
            'language': plugin_data['plugin_language'],
            'placeholder': plugin_data['placeholder_id'],
            'parent': plugin_data.get('plugin_parent', None),
            'plugin_type': plugin_data['plugin_type'],
            'position': plugin_data['position'],
        }

        response = plugin_instance.add_view(request)

        plugin = getattr(plugin_instance, 'saved_object', None)

        if plugin:
            plugin.placeholder.mark_as_dirty(plugin.language, clear_cache=False)

        if plugin_instance._operation_token:
            tree_order = placeholder.get_plugin_tree_order(plugin.parent_id)
            self._send_post_placeholder_operation(
                request,
                operation=operations.ADD_PLUGIN,
                token=plugin_instance._operation_token,
                plugin=plugin,
                placeholder=plugin.placeholder,
                tree_order=tree_order,
            )
        return response