Example #1
0
    def register_plugin(self, plugin):
        """
        Registers the given plugin(s).

        Static sanity checks is also performed.

        If a plugin is already registered, this will raise PluginAlreadyRegistered.
        """
        if not issubclass(plugin, CMSPluginBase):
            raise ImproperlyConfigured(
                "CMS Plugins must be subclasses of CMSPluginBase, %r is not."
                % plugin
            )
        plugin_name = plugin.__name__
        if plugin_name in self.plugins:
            raise PluginAlreadyRegistered(
                "Cannot register %r, a plugin with this name (%r) is already "
                "registered." % (plugin, plugin_name)
            )

        plugin.value = plugin_name
        self.plugins[plugin_name] = plugin
        from cms.signals import pre_save_plugins

        signals.pre_save.connect(pre_save_plugins, sender=plugin.model,
                                 dispatch_uid='cms_pre_save_plugin_%s' % plugin_name)
        return plugin
Example #2
0
    def register_plugin(self, plugin_or_iterable):
        """
        Registers the given plugin(s).

        If a plugin is already registered, this will raise PluginAlreadyRegistered.
        """
        if not hasattr(plugin_or_iterable,'__iter__'):
            plugin_or_iterable = [plugin_or_iterable]
        for plugin in plugin_or_iterable:
            assert issubclass(plugin, CMSPluginBase)
            plugin_name = plugin.__name__
            if plugin_name in self.plugins:
                raise PluginAlreadyRegistered("[%s] a plugin with this name is already registered" % plugin_name)
            plugin.value = plugin_name
            self.plugins[plugin_name] = plugin

            if 'reversion' in settings.INSTALLED_APPS:
                try:
                    from reversion.registration import RegistrationError
                except ImportError:
                    from reversion.revisions import RegistrationError
                try:
                    reversion_register(plugin.model, follow=["cmsplugin_ptr"])
                except RegistrationError:
                    pass
    def register_plugin(self, plugin):
        """
        Registers the given plugin(s).

        If a plugin is already registered, this will raise PluginAlreadyRegistered.
        """
        if not issubclass(plugin, CMSPluginBase):
            raise ImproperlyConfigured(
                "CMS Plugins must be subclasses of CMSPluginBase, %r is not."
                % plugin
            )
        plugin_name = plugin.__name__
        if plugin_name in self.plugins:
            raise PluginAlreadyRegistered(
                "Cannot register %r, a plugin with this name (%r) is already "
                "registered." % (plugin, plugin_name)
            )
        plugin.value = plugin_name
        self.plugins[plugin_name] = plugin

        if 'reversion' in settings.INSTALLED_APPS:
            try:
                from reversion.registration import RegistrationError
            except ImportError:
                from reversion.revisions import RegistrationError
            try:
                reversion_register(plugin.model)
            except RegistrationError:
                pass
Example #4
0
    def register_plugin(self, plugin):
        """
        Registers the given plugin(s).

        If a plugin is already registered, this will raise PluginAlreadyRegistered.
        """
        if hasattr(plugin, '__iter__'):
            warnings.warn(
                "Registering more than one plugin at once will be deprecated in 2.3",
                DeprecationWarning)
            for single_plugin in plugin:
                self.register_plugin(single_plugin)
            return
        if not issubclass(plugin, CMSPluginBase):
            raise ImproperlyConfigured(
                "CMS Plugins must be subclasses of CMSPluginBase, %r is not." %
                plugin)
        plugin_name = plugin.__name__
        if plugin_name in self.plugins:
            raise PluginAlreadyRegistered(
                "Cannot register %r, a plugin with this name (%r) is already "
                "registered." % (plugin, plugin_name))
        plugin.value = plugin_name
        self.plugins[plugin_name] = plugin

        if 'reversion' in settings.INSTALLED_APPS:
            try:
                from reversion.registration import RegistrationError
            except ImportError:
                from reversion.revisions import RegistrationError
            try:
                reversion_register(plugin.model)
            except RegistrationError:
                pass
Example #5
0
    def register_plugin(self, plugin):
        """
        Registers the given plugin(s).

        Static sanity checks is also performed.

        If a plugin is already registered, this will raise PluginAlreadyRegistered.
        """
        if not issubclass(plugin, CMSPluginBase):
            raise ImproperlyConfigured(
                "CMS Plugins must be subclasses of CMSPluginBase, %r is not." %
                plugin)
        plugin_name = plugin.__name__
        if plugin_name in self.plugins:
            raise PluginAlreadyRegistered(
                "Cannot register %r, a plugin with this name (%r) is already "
                "registered." % (plugin, plugin_name))

        plugin.value = plugin_name
        self.plugins[plugin_name] = plugin
        from cms.signals import pre_save_plugins, post_delete_plugins, pre_delete_plugins

        signals.pre_save.connect(pre_save_plugins,
                                 sender=plugin.model,
                                 dispatch_uid='cms_pre_save_plugin_%s' %
                                 plugin_name)
        signals.post_delete.connect(post_delete_plugins,
                                    sender=CMSPlugin,
                                    dispatch_uid='cms_post_delete_plugin_%s' %
                                    plugin_name)
        signals.pre_delete.connect(pre_delete_plugins,
                                   sender=CMSPlugin,
                                   dispatch_uid='cms_pre_delete_plugin_%s' %
                                   plugin_name)
        if is_installed('reversion'):
            from cms.utils.reversion_hacks import RegistrationError
            try:
                reversion_register(plugin.model)
            except RegistrationError:
                pass

        return plugin
Example #6
0
    def register_plugin(self, plugin):
        """
        Registers the given plugin(s).

        Static sanity checks is also performed.

        If a plugin is already registered, this will raise PluginAlreadyRegistered.
        """
        if not issubclass(plugin, CMSPluginBase):
            raise ImproperlyConfigured(
                "CMS Plugins must be subclasses of CMSPluginBase, %r is not." %
                plugin)
        plugin_name = plugin.__name__
        if plugin_name in self.plugins:
            raise PluginAlreadyRegistered(
                "Cannot register %r, a plugin with this name (%r) is already "
                "registered." % (plugin, plugin_name))

        plugin.value = plugin_name
        self.plugins[plugin_name] = plugin
        return plugin
Example #7
0
    def register_plugin(self, plugin):
        """
        Registers the given plugin(s).

        If a plugin is already registered, this will raise PluginAlreadyRegistered.
        """
        if not issubclass(plugin, CMSPluginBase):
            raise ImproperlyConfigured(
                "CMS Plugins must be subclasses of CMSPluginBase, %r is not." %
                plugin)
        if plugin.render_plugin and not type(
                plugin.render_plugin) == property or hasattr(
                    plugin.model, 'render_template'):
            if plugin.render_template is None and not hasattr(
                    plugin.model, 'render_template'):
                raise ImproperlyConfigured(
                    "CMS Plugins must define a render template or set render_plugin=False: %s"
                    % plugin)
            else:
                from django.template import loader

                template = hasattr(
                    plugin.model, 'render_template'
                ) and plugin.model.render_template or plugin.render_template
                if isinstance(template, string_types) and template:
                    try:
                        loader.get_template(template)
                    except TemplateDoesNotExist:
                        raise ImproperlyConfigured(
                            "CMS Plugins must define a render template (%s) that exist: %s"
                            % (plugin, template))
                    except TemplateSyntaxError:
                        pass
        else:
            if plugin.allow_children:
                raise ImproperlyConfigured(
                    "CMS Plugins can not define render_plugin=False and allow_children=True: %s"
                    % plugin)
        plugin_name = plugin.__name__
        if plugin_name in self.plugins:
            raise PluginAlreadyRegistered(
                "Cannot register %r, a plugin with this name (%r) is already "
                "registered." % (plugin, plugin_name))

        plugin.value = plugin_name
        self.plugins[plugin_name] = plugin
        from cms.signals import pre_save_plugins, post_delete_plugins, pre_delete_plugins

        signals.pre_save.connect(pre_save_plugins,
                                 sender=plugin.model,
                                 dispatch_uid='cms_pre_save_plugin_%s' %
                                 plugin_name)
        signals.post_delete.connect(post_delete_plugins,
                                    sender=CMSPlugin,
                                    dispatch_uid='cms_post_delete_plugin_%s' %
                                    plugin_name)
        signals.pre_delete.connect(pre_delete_plugins,
                                   sender=CMSPlugin,
                                   dispatch_uid='cms_pre_delete_plugin_%s' %
                                   plugin_name)
        if 'reversion' in settings.INSTALLED_APPS:
            try:
                from reversion.registration import RegistrationError
            except ImportError:
                from reversion.revisions import RegistrationError
            try:
                reversion_register(plugin.model)
            except RegistrationError:
                pass
Example #8
0
    def register_plugin(self, plugin):
        """
        Registers the given plugin(s).

        If a plugin is already registered, this will raise PluginAlreadyRegistered.
        """
        if not issubclass(plugin, CMSPluginBase):
            raise ImproperlyConfigured(
                "CMS Plugins must be subclasses of CMSPluginBase, %r is not."
                % plugin
            )
        if (plugin.render_plugin and not type(plugin.render_plugin) == property
                or hasattr(plugin.model, 'render_template')
                or hasattr(plugin, 'get_render_template')):
            if (plugin.render_template is None and
                    not hasattr(plugin.model, 'render_template') and
                    not hasattr(plugin, 'get_render_template')):
                raise ImproperlyConfigured(
                    "CMS Plugins must define a render template, "
                    "a get_render_template method or "
                    "set render_plugin=False: %s" % plugin
                )
            # If plugin class defines get_render_template we cannot
            # statically check for valid template file as it depends
            # on plugin configuration and context.
            # We cannot prevent developer to shoot in the users' feet
            elif not hasattr(plugin, 'get_render_template'):
                from django.template import loader

                template = ((hasattr(plugin.model, 'render_template') and
                            plugin.model.render_template) or
                            plugin.render_template)
                if isinstance(template, six.string_types) and template:
                    try:
                        loader.get_template(template)
                    except TemplateDoesNotExist as e:
                        # Note that the template loader will throw
                        # TemplateDoesNotExist if the plugin's render_template
                        # does in fact exist, but it includes a template that
                        # doesn't.
                        if six.text_type(e) == template:
                            raise ImproperlyConfigured(
                                "CMS Plugins must define a render template (%s) that exists: %s"
                                % (plugin, template)
                            )
                        else:
                            pass
                    except TemplateSyntaxError:
                        pass
        else:
            if plugin.allow_children:
                raise ImproperlyConfigured(
                    "CMS Plugins can not define render_plugin=False and allow_children=True: %s"
                    % plugin
                )
        plugin_name = plugin.__name__
        if plugin_name in self.plugins:
            raise PluginAlreadyRegistered(
                "Cannot register %r, a plugin with this name (%r) is already "
                "registered." % (plugin, plugin_name)
            )

        plugin.value = plugin_name
        self.plugins[plugin_name] = plugin
        from cms.signals import pre_save_plugins, post_delete_plugins, pre_delete_plugins

        signals.pre_save.connect(pre_save_plugins, sender=plugin.model,
                                 dispatch_uid='cms_pre_save_plugin_%s' % plugin_name)
        signals.post_delete.connect(post_delete_plugins, sender=CMSPlugin,
                                    dispatch_uid='cms_post_delete_plugin_%s' % plugin_name)
        signals.pre_delete.connect(pre_delete_plugins, sender=CMSPlugin,
                                   dispatch_uid='cms_pre_delete_plugin_%s' % plugin_name)
        if is_installed('reversion'):
            from reversion.revisions import RegistrationError
            try:
                reversion_register(plugin.model)
            except RegistrationError:
                pass

        return plugin
    def register_segment_plugin(self,
                                plugin_instance,
                                suppress_discovery=False):
        '''
        Registers the provided plugin_instance into the SegmentPool.
        Raises:
            PluginAlreadyRegistered: if the plugin is already registered and
            ImproperlyConfigured: if not an appropriate type of plugin.

        Note: plugin_instance.configuration_string can return either of:

            1. A normal string of text,
            2. A gettext_lazy object,
            3. A extra-lazy object (Promise to return a gettext_lazy object)

        the `suppress_discovery` flag, when set to true, prevents recursion
        and should be only used by the self.discovery() method.
        '''

        if not suppress_discovery and not self.segments:
            self.discover()

        if isinstance(plugin_instance, SegmentBasePluginModel):
            plugin_class_instance = plugin_instance.get_plugin_class_instance()

            if plugin_class_instance.allow_overrides:
                #
                # There is no need to register a plugin that doesn't
                # allow overrides.
                #
                plugin_class_name = plugin_class_instance.__class__.__name__
                plugin_name = plugin_class_instance.name

                if plugin_class_name not in self.segments:
                    self.segments[plugin_class_name] = {
                        self.NAME: plugin_name,
                        self.CFGS: dict(),
                    }
                    self._sorted_segments = dict()
                segment_class = self.segments[plugin_class_name]

                plugin_config = plugin_instance.configuration_string

                #
                # NOTE: We always use the 'en' version of the configuration string
                # as the key.
                #
                lang = get_language()
                activate('en')

                if isinstance(plugin_config, Promise):
                    plugin_config_key = force_text(plugin_config)
                elif isinstance(plugin_config, six.text_type):
                    plugin_config_key = plugin_config
                else:
                    warnings.warn(
                        'register_segment: Not really sure what '
                        '‘plugin_instance.configuration_string’ returned!')

                activate(lang)

                segment_configs = segment_class[self.CFGS]

                if plugin_config_key not in segment_configs:
                    # We store the un-translated version as the LABEL
                    segment_configs[plugin_config_key] = {
                        self.LABEL: plugin_config,
                        self.OVERRIDES: dict(),
                        self.INSTANCES: list(),
                    }
                    self._sorted_segments = dict()

                segment = segment_configs[plugin_config_key]

                if plugin_instance not in segment[self.INSTANCES]:
                    segment[self.INSTANCES].append(plugin_instance)
                    self._sorted_segments = dict()
                else:
                    cls = plugin_instance.get_plugin_class_instance(
                    ).__class__.__name__
                    raise PluginAlreadyRegistered(
                        'The segment plugin {0} cannot '
                        'be registered because it already is.'.format(cls))

        else:
            cls = plugin_instance.__class__.__name__
            raise ImproperlyConfigured(
                'Segment Plugin models must '
                'subclass SegmentBasePluginModel. {0!r} does not.'.format(cls))