Esempio n. 1
0
def test_change_label_size():
    """Verify size of the field label can be changed from the default."""
    form = FileUploadForm()
    form.helper = FormHelper()
    form.helper.layout = Layout(
        Field("file", context={"label_size": Size.for_label("l")}))
    assert parse_form(form) == parse_contents(RESULT_DIR, "label_size.html")
Esempio n. 2
0
    def checkboxes(
        cls, field, legend_size=None, legend_tag=None, small=False, **kwargs
    ):
        """
        Create a field for displaying a set of checkboxes.

        Args:
            field (str): the name of the field. The field's label will be used as
                the title of the <legend>.

            legend_size (str): the size of the legend. The default is None in which
                case the legend will be rendered at the same size as regular text.

            legend_tag (str): Wrap the field legend with this HTML tag.
                Default is None.

            small (bool): Display small checkboxes. Default is False.

            **kwargs: Attributes to add to the <input> element when the field is
                rendered.

        """
        context = {}

        if legend_size:
            context["legend_size"] = Size.for_legend(legend_size)

        if legend_tag:
            context["legend_tag"] = legend_tag

        context["checkboxes_small"] = small

        return Field(field, context=context, **kwargs)
Esempio n. 3
0
    def __init__(self,
                 *fields,
                 legend=None,
                 legend_size=None,
                 legend_tag=None,
                 **kwargs):
        self.fields = list(fields)
        self.context = {}

        if legend:
            self.context["legend"] = legend

        if legend_size:
            self.context["legend_size"] = Size.for_legend(legend_size)

        if legend_tag:
            self.context["legend_tag"] = legend_tag

        if hasattr(self, "css_class") and "css_class" in kwargs:
            self.css_class += " %s" % kwargs.pop("css_class")
        if not hasattr(self, "css_class"):
            self.css_class = kwargs.pop("css_class", None)

        self.css_id = kwargs.pop("css_id", None)
        self.template = kwargs.pop("template", self.template)
        self.flat_attrs = flatatt(kwargs)
Esempio n. 4
0
    def select(cls, field, legend_size=None, legend_tag=None, **kwargs):
        """
        Create a field for displaying a select drop-down.

        Args:
            field (str): the name of the field.

            legend_size (str): the size of the legend. The default is None in which
                case the legend will be rendered at the same size as regular text.

            legend_tag (str): Wrap the field legend with this HTML tag.
                Default is None.

            **kwargs: Attributes to add to the <select> element when the field is
                rendered.

        """
        context = {}

        if legend_size:
            context["legend_size"] = Size.for_legend(legend_size)

        if legend_tag:
            context["legend_tag"] = legend_tag

        return Field(field, context=context, **kwargs)
def test_change_legend_size():
    """Verify size of the field legend can be changed from the default."""
    form = CheckboxesForm()
    form.helper = FormHelper()
    form.helper.layout = Layout(
        Field("method", context={"legend_size": Size.for_legend("l")}))
    assert parse_form(form) == parse_contents(RESULT_DIR, "legend_size.html")
Esempio n. 6
0
    def render_layout(self, form, context, template_pack=TEMPLATE_PACK):
        """
        Returns safe html of the rendering of the layout.

        :meta private:
        """
        # Django will add a required attribute when a field is rendered, if is
        # required by the form. The browser validation interferes with the way
        # validation errors are reported so adding required attributes is
        # disabled at the form level.
        form.use_required_attribute = False

        if self.label_size:
            context["label_size"] = Size.for_label(self.label_size)
        if self.legend_size:
            context["legend_size"] = Size.for_legend(self.legend_size)

        return super().render_layout(form,
                                     context,
                                     template_pack=template_pack)
Esempio n. 7
0
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     self.helper = FormHelper()
     self.helper.label_size = Size.for_label(Size.SMALL)
     self.helper.layout = Layout(
         Fieldset(
             Field.text("name"),
             Field.text("email", field_width=Fluid.TWO_THIRDS),
             Field.text("phone", field_width=Fixed.TEN),
         ),
         Button("submit", "Submit"),
     )
Esempio n. 8
0
    def text(cls,
             field,
             label_size=None,
             label_tag=None,
             field_width=None,
             **kwargs):
        """
        Create a field for displaying a Text input.

        Args:
            field (str): the name of the field.

            label_size (str): the size of the label. The default is None in which
                case the label will be rendered at the same size as regular text.

            label_tag (str): Wrap the field label with this HTML tag.
                Default is None.

            field_width (int, str): the width of the field - fixed or fluid. The
                default is None in which case the field will be rendered full width.

            **kwargs: Attributes to add to the <input> element when the field is
                rendered.

        """
        context = {}

        if label_size:
            context["label_size"] = Size.for_label(label_size)

        if label_tag:
            context["label_tag"] = label_tag

        if field_width:
            if isinstance(field_width, int):
                css_class = Fixed.for_input(field_width)
            else:
                css_class = Fluid.for_input(field_width)
        else:
            css_class = kwargs.get("css_class")

        return Field(field, css_class=css_class, context=context, **kwargs)
Esempio n. 9
0
    def radios(
        cls,
        field,
        legend_size=None,
        legend_tag=None,
        small=False,
        inline=False,
        **kwargs,
    ):
        """
        Create a field for displaying radio buttons.

        Args:
            field (str): the name of the field.

            legend_size (str): the size of the legend. The default is None in which
                case the legend will be rendered at the same size as regular text.

            legend_tag (str): Wrap the field legend with this HTML tag.
                Default is None.

            small (bool): Display small radio buttons. Default is False.

            inline (bool): Display the radio buttons in a row. Default is False.

            **kwargs: Attributes to add to the <input> element when the field is
                rendered.

        """
        context = {}

        if legend_size:
            context["legend_size"] = Size.for_legend(legend_size)

        if legend_tag:
            context["legend_tag"] = legend_tag

        context["radios_small"] = small
        context["radios_inline"] = inline

        return Field(field, context=context, **kwargs)
Esempio n. 10
0
    def textarea(
        cls,
        field,
        label_size=None,
        label_tag=None,
        rows=10,
        max_characters=None,
        max_words=None,
        threshold=None,
        **kwargs,
    ):
        """
        Create a field for displaying a Textarea.

        Args:
            field (str): the name of the field.

            label_size (str): the size of the label. The default is None in which
                case the label will be rendered at the same size as regular text.

            label_tag (str): Wrap the field label with this HTML tag.
                Default is None.

            rows (int): the number of rows to display. If not specified then Django's
                default of 10 will be used (the default used by most browsers is 2).

            max_characters (int, optional): the maximum number of characters that
                should be entered. Default is None.

            max_words (int, optional): the maximum number of words that should be
                entered. Default is None.

            threshold (int, optional): the percentage of the count that has to
                be reached before the limit is shown. Default is None.

            **kwargs: Attributes to add to the <textarea> element when the field is
                rendered.

        Raises:
            ValueError: if you set max_characters and max_words at the same time.

        """
        context = {}

        if label_size:
            context["label_size"] = Size.for_label(label_size)

        if label_tag:
            context["label_tag"] = label_tag

        kwargs["rows"] = rows

        if max_characters and max_words:
            raise ValueError(
                "Cannot set max_characters and max_words at the same time."
            )

        if threshold and not max_characters and not max_words:
            raise ValueError(
                "Cannot set the typing threshold without setting the maximum "
                "number of characters of words."
            )

        if max_characters:
            context["max_characters"] = max_characters

        if max_words:
            context["max_words"] = max_words

        if max_characters or max_words:
            kwargs["css_class"] = "govuk-js-character-count"

            if threshold:
                context["threshold"] = threshold

        return Field(field, context=context, **kwargs)