示例#1
0
class CoderedAdvSettings(blocks.StructBlock):
    """
    Common fields each block should have,
    which are hidden under the block's "Advanced Settings" dropdown.
    """
    # placeholder, real value get set in __init__()
    custom_template = blocks.Block()

    custom_css_class = blocks.CharBlock(
        required=False,
        max_length=255,
        label=_('Custom CSS Class'),
    )
    custom_id = blocks.CharBlock(
        required=False,
        max_length=255,
        label=_('Custom ID'),
    )

    class Meta:
        form_template = 'wagtailadmin/block_forms/base_block_settings_struct.html'
        label = _('Advanced Settings')

    def __init__(self, local_blocks=None, template_choices=None, **kwargs):
        if not local_blocks:
            local_blocks = ()

        local_blocks += (('custom_template',
                          blocks.ChoiceBlock(choices=template_choices,
                                             default=None,
                                             required=False,
                                             label=_('Template'))), )

        super().__init__(local_blocks, **kwargs)
示例#2
0
class BaseBlock(blocks.StructBlock):
    """
    Common attributes for all blocks used in CodeRed CMS.
    """
    # subclasses can override this to determine the advanced settings class
    advsettings_class = CoderedAdvSettings

    # placeholder, real value get set in __init__() from advsettings_class
    settings = blocks.Block()

    def __init__(self, local_blocks=None, **kwargs):
        """
        Construct and inject settings block, then initialize normally.
        """
        klassname = self.__class__.__name__.lower()
        choices = cr_settings['FRONTEND_TEMPLATES_BLOCKS'].get('*', ()) + \
                  cr_settings['FRONTEND_TEMPLATES_BLOCKS'].get(klassname, ())

        if not local_blocks:
            local_blocks = ()

        local_blocks += (('settings',
                          self.advsettings_class(template_choices=choices)), )

        super().__init__(local_blocks, **kwargs)

    def render(self, value, context=None):
        template = value['settings']['custom_template']

        if not template:
            template = self.get_template(context=context)
            if not template:
                return self.render_basic(value, context=context)

        if context is None:
            new_context = self.get_context(value)
        else:
            new_context = self.get_context(value, parent_context=dict(context))

        return mark_safe(render_to_string(template, new_context))