Example #1
0
class BaseFormTag(FormOptionsTag, ConfigMixin):
    with_ = ttag.KeywordsArg(required=False, named=True)
    only = ttag.BooleanArg()
    using = TemplateArg(required=False, named=True)
    extends = TemplateArg(required=False, named=True)

    def __init__(self, parser, *args, **kwargs):
        super(BaseFormTag, self).__init__(parser, *args, **kwargs)
        if 'using' in self._vars and 'extends' in self._vars:
            raise template.TemplateSyntaxError("Can't provide both 'using' "
                "and 'extends'.")
        using_inline = 'using' in self._vars and not self._vars['using']
        if using_inline or 'extends' in self._vars:
            nodelist = parser.parse([self._meta.end_block])
            parser.delete_first_token()
            if using_inline:
                self._vars['using'] = template.Template('')
                self._vars['using'].nodelist = nodelist
            else:
                self.blocks = dict([(n.name, n) for n in
                    nodelist.get_nodes_by_type(BlockNode)])

    def clean(self, data, context):
        data = super(BaseFormTag, self).clean(data, context)
        form_template = data.get('using') or data.get('extends')
        if not form_template:
            if not self._meta.default_template:
                return data
            form_template = self.get_config('%s_template' % self._meta.name,
                data, context) or self._meta.default_template
        if isinstance(form_template, basestring):
            form_template = get_template(form_template)
        data['template'] = form_template
        return data

    def get_extra_context(self, data):
        return {}

    def get_block_context(self, form_template, blocks):
        block_context = BlockContext()

        # Add the block nodes from this node to the block context.
        block_context.add_blocks(blocks)

        # Add the template's nodes too if it is the root template.
        for node in form_template.nodelist:
            # The ExtendsNode has to be the first non-text node.
            if not isinstance(node, template.TextNode):
                if not isinstance(node, ExtendsNode):
                    blocks = dict([(n.name, n) for n in
                                   form_template.nodelist\
                                       .get_nodes_by_type(BlockNode)])
                    block_context.add_blocks(blocks)
                break
        return block_context

    def render(self, context):
        data = self.resolve(context)

        if 'only' in data or self.get_config('only', data, context):
            sub_context = template.Context()
            for key, value in context.keys:
                if key in ('form', CONFIG_KEY):
                    sub_context[key] = value
        else:
            sub_context = context

        extra_context = self.get_extra_context(data)
        extra_context.update(self.get_config('with', data, context) or {})
        if 'with' in data:
            extra_context.update(data['with'])
        # Update (i.e. push) context in preparation for rendering.
        sub_context.update(extra_context)
        context.render_context.push()
        blocks = getattr(self, 'blocks',
            self.get_config('extends_blocks', data, context))
        if blocks:
            context.render_context[BLOCK_CONTEXT_KEY] = \
                self.get_block_context(data['template'], blocks)
        output = data['template']._render(sub_context)
        # Clean up context changes.
        context.render_context.pop()
        sub_context.pop()
        return output
Example #2
0
class IncludeMixed(BaseInclude):
    template = ttag.Arg()
    with_ = ttag.KeywordsArg(named=True, required=False, verbose=True)
Example #3
0
class IncludeCompact(BaseInclude):
    template = ttag.Arg()
    with_ = ttag.KeywordsArg(named=True, required=False)
Example #4
0
class KeywordsEcho(TestTag):
    keywords = ttag.KeywordsArg()

    def output(self, data):
        keywords = data['keywords'].items()
        return ', '.join('%s: %s' % (key, value) for key, value in keywords)
Example #5
0
class IncludeMixed(BaseInclude):
    template = ttag.Arg(positional=True)
    with_ = ttag.KeywordsArg(required=False, verbose=True)
Example #6
0
class IncludeCompact(BaseInclude):
    template = ttag.Arg(positional=True)
    with_ = ttag.KeywordsArg(required=False)