示例#1
0
    def render(self, context):
        fieldset = context.get('fieldset')
        if not fieldset:
            raise FormeInvalidTemplate(
                'Missing *fieldset* in context of RowNode. Probably'
                ' misplaced *field* tag?')

        if self.target:
            fields = []
            for target in self.target:
                if isinstance(target, Field):
                    fields.append(target)
                else:
                    # Find field in fieldset
                    for field in fieldset:
                        if field.name == field:
                            fields.append(target)
        else:
            fields = fieldset

        output = ''
        for field in fields:
            with update_context(context, {'field': field}):
                output += super(FieldNode, self).render(context)

        return output
示例#2
0
    def render(self, context):
        field = context.get('field')
        if not field:
            raise FormeInvalidTemplate(
                'Missing *field* in context of LabelNode. Probably'
                ' misplaced *label* tag?')

        with update_context(context, {'label': Label.create(field)}):
            return super(LabelNode, self).render(context)
示例#3
0
    def render(self, context):
        forms = [form.resolve(context) for form in self.target]
        if not any(forms):
            raise template.TemplateSyntaxError('Need form to render.')
        elif len(self.target) > 1:
            context_variable = 'forms'
        else:
            context_variable = 'form'
            forms = forms[0]

        with update_context(context, {context_variable: forms}):
            return super(FormeNode, self).render(context)
示例#4
0
    def render(self, context):
        form = context.get('form')
        if not form:
            raise FormeInvalidTemplate(
                'Missing *form* in context of NonFieldErrorsNode. Probably'
                ' misplaced *nonfielderrors* tag?')

        errors = form.non_field_errors()
        if not errors:
            return ''

        with update_context(context, {'non_field_errors': errors}):
            return super(NonFieldErrorsNode, self).render(context)
示例#5
0
    def render(self, context):
        form = context.get('form')
        if not form:
            raise FormeInvalidTemplate(
                'Missing *form* in context of HiddenFieldsNode. Probably'
                ' misplaced *hiddenfields* tag?')

        hidden_fields = form.hidden_fields()
        if not hidden_fields:
            return ''

        with update_context(context, {'hidden_fields': hidden_fields}):
            return super(HiddenFieldsNode, self).render(context)
示例#6
0
    def render(self, context):
        field = context.get('field')
        if not field:
            raise FormeInvalidTemplate(
                'Missing *field* in context of ErrorsNode. Probably'
                ' misplaced *errors* tag?')

        errors = getattr(field, 'errors', None)
        if not errors:
            return ''

        with update_context(context, {'errors': errors}):
            return super(ErrorsNode, self).render(context)
示例#7
0
    def render(self, context):
        form = context.get('form')
        if not form:
            raise FormeInvalidTemplate(
                'Missing *form* in context of FieldsetNode. Probably'
                ' misplaced *fieldset* tag?')

        if self.target:
            fields = [form[field.resolve(context)] for field in self.target]
        else:
            fields = list(form)

        with update_context(context, {'fieldset': fields}):
            return super(FieldsetNode, self).render(context)
示例#8
0
    def render(self, context):
        if not self.nodelist:
            if isinstance(self.target, list):
                target = self.target[0] if len(self.target) else None
            else:
                target = self.target

            tmpl = self.get_template(self.tag_name, target, context)
        else:
            tmpl = self.nodelist

        if not tmpl:
            msg = ('Missing template for tag {0}'.format(self.tag_name))
            raise template.TemplateSyntaxError(msg)

        with update_context(context, {'forme_style': self.styles}):
            return tmpl.render(context)
示例#9
0
def test_push_context():
    context = template.Context({'field': 'Foo'})
    with update_context(context, {'field': 'Bar'}):
        assert context['field'] == 'Bar'
    assert context['field'] == 'Foo'