Beispiel #1
0
    def register(self, extension):
        """
        Registers the given extension.

        Example::

            class MyExtension(PageExtension):
                pass

            extension_pool.register(MyExtension)

        or as decorator::

            @extension_pool.register
            class MyExtension(PageExtension):
                pass

        """

        if issubclass(extension, PageExtension):
            self.page_extensions.add(extension)
        elif issubclass(extension, TitleExtension):
            self.title_extensions.add(extension)
        else:
            raise SubClassNeededError(
                'Extension has to subclass either %r or %r. %r does not!' % (PageExtension, TitleExtension, extension)
            )
        self._activate_signaling()
        return extension
Beispiel #2
0
 def __new__(cls, name, bases, attrs):
     super_new = super(CMSPluginBaseMetaclass, cls).__new__
     parents = [base for base in bases if isinstance(base, CMSPluginBaseMetaclass)]
     if not parents:
         # If this is CMSPluginBase itself, and not a subclass, don't do anything
         return super_new(cls, name, bases, attrs)
     new_plugin = super_new(cls, name, bases, attrs)
     # validate model is actually a CMSPlugin subclass.
     if not issubclass(new_plugin.model, CMSPlugin):
         raise SubClassNeededError(
             "The 'model' attribute on CMSPluginBase subclasses must be "
             "either CMSPlugin or a subclass of CMSPlugin. %r on %r is not."
             % (new_plugin.model, new_plugin)
         )
     # validate the template:
     if not hasattr(new_plugin, 'render_template'):
         raise ImproperlyConfigured(
             "CMSPluginBase subclasses must have a render_template attribute"
         )
     # Set the default form
     if not new_plugin.form:
         form_meta_attrs = {
             'model': new_plugin.model,
             'exclude': ('position', 'placeholder', 'language', 'plugin_type')
         }
         form_attrs = {
             'Meta': type('Meta', (object,), form_meta_attrs)
         }
         new_plugin.form = type('%sForm' % name, (ModelForm,), form_attrs)
     # Set the default fieldsets
     if not new_plugin.fieldsets:
         basic_fields = []
         advanced_fields = []
         for f in new_plugin.model._meta.fields:
             if not f.auto_created and f.editable:
                 if hasattr(f, 'advanced'):
                     advanced_fields.append(f.name)
                 else: basic_fields.append(f.name)
         if advanced_fields:
             new_plugin.fieldsets = [
                 (
                     None,
                     {
                         'fields': basic_fields
                     }
                 ),
                 (
                     _('Advanced options'),
                     {
                         'fields' : advanced_fields,
                         'classes' : ('collapse',)
                     }
                 )
             ]
     # Set default name
     if not new_plugin.name:
         new_plugin.name = re.sub("([a-z])([A-Z])", "\g<1> \g<2>", name)
     return new_plugin
Beispiel #3
0
    def register(self, extension):
        """
        Registers the given extension.
        """

        if issubclass(extension, PageExtension):
            self.page_extensions.add(extension)
        elif issubclass(extension, TitleExtension):
            self.title_extensions.add(extension)
        else:
            raise SubClassNeededError(
                'Extension has to subclass either %r or %r. %r does not!' % (PageExtension, TitleExtension, extension)
            )
        self._activate_signaling()