Ejemplo n.º 1
0
    def render_layout(self, form, context, template_pack=TEMPLATE_PACK):
        """
        Returns safe html of the rendering of the layout
        """

        form.rendered_fields = set()
        form.crispy_field_template = self.field_template

        # This renders the specified Layout strictly
        html = self.layout.render(
            form,
            self.form_style,
            context,
            template_pack=template_pack
        )

        # Rendering some extra fields if specified
        if self.render_unmentioned_fields or self.render_hidden_fields or self.render_required_fields:
            fields = set(form.fields.keys())
            left_fields_to_render = fields - form.rendered_fields
            for field in left_fields_to_render:
                if (
                    self.render_unmentioned_fields or
                    self.render_hidden_fields and form.fields[field].widget.is_hidden or
                    self.render_required_fields and form.fields[field].widget.is_required
                ):
                    html += render_field(
                        field,
                        form,
                        self.form_style,
                        context,
                        template_pack=template_pack
                    )

        # If the user has Meta.fields defined, not included in the layout,
        # we suppose they need to be rendered
        if hasattr(form, 'Meta'):
            if hasattr(form.Meta, 'fields'):
                current_fields = tuple(getattr(form, 'fields', {}).keys())

                meta_fields = getattr(form.Meta, 'fields')

                fields_to_render = list_intersection(current_fields, meta_fields)
                left_fields_to_render = list_difference(fields_to_render, form.rendered_fields)

                for field in left_fields_to_render:
                    # We still respect the configuration of the helper
                    # regarding which fields to render
                    if (
                        self.render_unmentioned_fields or
                        (self.render_hidden_fields and
                         form.fields[field].widget.is_hidden) or
                        (self.render_required_fields and
                         form.fields[field].widget.is_required)
                    ):

                        html += render_field(field, form, self.form_style, context)


        return mark_safe(html)
Ejemplo n.º 2
0
    def render_layout(self, form, context, template_pack=TEMPLATE_PACK):
        """
        Returns safe html of the rendering of the layout
        """
        form.rendered_fields = set()
        form.crispy_field_template = self.field_template

        # This renders the specified Layout strictly
        html = self.layout.render(
            form,
            self.form_style,
            context,
            template_pack=template_pack
        )

        # Rendering some extra fields if specified
        if self.render_unmentioned_fields or self.render_hidden_fields or self.render_required_fields:
            fields = set(form.fields.keys())
            left_fields_to_render = fields - form.rendered_fields
            for field in left_fields_to_render:
                if (
                    self.render_unmentioned_fields or
                    self.render_hidden_fields and form.fields[field].widget.is_hidden or
                    self.render_required_fields and form.fields[field].widget.is_required
                ):
                    html += render_field(
                        field,
                        form,
                        self.form_style,
                        context,
                        template_pack=template_pack
                    )

        # If the user has Meta.fields defined, not included in the layout,
        # we suppose they need to be rendered
        if hasattr(form, 'Meta'):
            if hasattr(form.Meta, 'fields'):
                current_fields = tuple(getattr(form, 'fields', {}).keys())
                meta_fields = getattr(form.Meta, 'fields')

                fields_to_render = list_intersection(current_fields, meta_fields)
                left_fields_to_render = list_difference(fields_to_render, form.rendered_fields)

                for field in left_fields_to_render:
                    # We still respect the configuration of the helper
                    # regarding which fields to render
                    if (
                        self.render_unmentioned_fields or
                        (self.render_hidden_fields and
                         form.fields[field].widget.is_hidden) or
                        (self.render_required_fields and
                         form.fields[field].widget.is_required)
                    ):
                        html += render_field(field, form, self.form_style, context)

        return mark_safe(html)
Ejemplo n.º 3
0
def test_list_intersection():
    assert list_intersection([1, 3], [2, 3]) == [3]