Skip to content

ChoiceFloatMultField

Field of Model.

Type of selective float field with static of elements.

ChoiceFloatMultField

Bases: Field

Field of Model.

Type of selective float field with static of elements. With multiple choice.

Source code in src/ramifice/fields/choice_float_mult_field.py
class ChoiceFloatMultField(Field):
    """Field of Model.

    Type of selective float field with static of elements.
    With multiple choice.
    """

    def __init__(
        self,
        label: str = "",
        default: list[float] | None = None,
        is_hide: bool = False,
        is_disable: bool = False,
        is_ignore: bool = False,
        hint: str = "",
        warning: list[str] = [],  # ruff:ignore[mutable-argument-default]
        is_require: bool = False,
        is_readonly: bool = False,
        choices: list[list[float | str]] | None = None,  # [[value, Title], ...]
    ) -> None:
        """Field of Model.

        Type of selective float field with static of elements.
        With multiple choice.

        Args:
            label: Text label for a web form field.
            default: Default value.
            is_hide: Hide field from user.
            is_disable: Blocks access and modification of the element.
            is_ignore: If true, the value of this field is not saved in the database.
            hint: An alternative for the `placeholder` parameter.
            warning: Warning information.
            is_require: Required field.
            is_readonly: Specifies that the field cannot be modified by the user.
            choices: For a predefined set of options - [[value, Title], ...].
        """
        Field.__init__(self, supported_types=(list, type(None)))

        field_core: dict[str, Any] = {
            "id": "",
            "name": "",
            "label": label,
            "value": None,
            "default": default,
            "is_hide": is_hide,
            "is_disable": is_disable,
            "is_ignore": is_ignore,
            "hint": hint,
            "warning": warning,
            "is_require": is_require,
            "is_readonly": is_readonly,
            "unique": False,
            "is_multiple": True,
            "choices": choices,
            "errors": [],
            "field_type": "ChoiceFloatMultField",
            "group": "choice",
        }

        self.__dict__["field_core"] = FieldCore(**field_core)
        self.field_core.has_value = MethodType(has_value, self.field_core)

        if Config.DEBUG:
            try:  # ruff:ignore[too-many-statements-in-try-clause]
                if choices is not None:
                    if not isinstance(choices, list):
                        raise AssertionError("Parameter `choices` - Not а `list` type!")
                    if len(choices) == 0:
                        raise AssertionError("The `choices` parameter should not contain an empty list!")
                if default is not None:
                    if not isinstance(default, list):
                        raise AssertionError("Parameter `default` - Not а `list` type!")
                    if len(default) == 0:
                        raise AssertionError("The `default` parameter should not contain an empty list!")
                    if choices is not None and not self.field_core.has_value():
                        raise AssertionError(
                            "Parameter `default` does not coincide with " + "list of permissive values in `choicees`.",
                        )
                if not isinstance(label, str):
                    raise AssertionError("Parameter `label` - Not а `str` type!")
                if not isinstance(is_disable, bool):
                    raise AssertionError("Parameter `is_disable` - Not а `bool` type!")
                if not isinstance(is_hide, bool):
                    raise AssertionError("Parameter `is_hide` - Not а `bool` type!")
                if not isinstance(is_ignore, bool):
                    raise AssertionError("Parameter `is_ignore` - Not а `bool` type!")
                if not isinstance(hint, str):
                    raise AssertionError("Parameter `hint` - Not а `str` type!")
                if not isinstance(warning, list):
                    raise AssertionError("Parameter `warning` - Not а `list` type!")
                if not isinstance(is_require, bool):
                    raise AssertionError("Parameter `is_require` - Not а `bool` type!")
                if not isinstance(is_readonly, bool):
                    raise AssertionError("Parameter `is_readonly` - Not а `bool` type!")
            except AssertionError as err:
                logger.critical(str(err))
                raise err

__init__(label='', default=None, is_hide=False, is_disable=False, is_ignore=False, hint='', warning=[], is_require=False, is_readonly=False, choices=None)

Field of Model.

Type of selective float field with static of elements. With multiple choice.

Parameters:

Name Type Description Default
label str

Text label for a web form field.

''
default list[float] | None

Default value.

None
is_hide bool

Hide field from user.

False
is_disable bool

Blocks access and modification of the element.

False
is_ignore bool

If true, the value of this field is not saved in the database.

False
hint str

An alternative for the placeholder parameter.

''
warning list[str]

Warning information.

[]
is_require bool

Required field.

False
is_readonly bool

Specifies that the field cannot be modified by the user.

False
choices list[list[float | str]] | None

For a predefined set of options - [[value, Title], ...].

None
Source code in src/ramifice/fields/choice_float_mult_field.py
def __init__(
    self,
    label: str = "",
    default: list[float] | None = None,
    is_hide: bool = False,
    is_disable: bool = False,
    is_ignore: bool = False,
    hint: str = "",
    warning: list[str] = [],  # ruff:ignore[mutable-argument-default]
    is_require: bool = False,
    is_readonly: bool = False,
    choices: list[list[float | str]] | None = None,  # [[value, Title], ...]
) -> None:
    """Field of Model.

    Type of selective float field with static of elements.
    With multiple choice.

    Args:
        label: Text label for a web form field.
        default: Default value.
        is_hide: Hide field from user.
        is_disable: Blocks access and modification of the element.
        is_ignore: If true, the value of this field is not saved in the database.
        hint: An alternative for the `placeholder` parameter.
        warning: Warning information.
        is_require: Required field.
        is_readonly: Specifies that the field cannot be modified by the user.
        choices: For a predefined set of options - [[value, Title], ...].
    """
    Field.__init__(self, supported_types=(list, type(None)))

    field_core: dict[str, Any] = {
        "id": "",
        "name": "",
        "label": label,
        "value": None,
        "default": default,
        "is_hide": is_hide,
        "is_disable": is_disable,
        "is_ignore": is_ignore,
        "hint": hint,
        "warning": warning,
        "is_require": is_require,
        "is_readonly": is_readonly,
        "unique": False,
        "is_multiple": True,
        "choices": choices,
        "errors": [],
        "field_type": "ChoiceFloatMultField",
        "group": "choice",
    }

    self.__dict__["field_core"] = FieldCore(**field_core)
    self.field_core.has_value = MethodType(has_value, self.field_core)

    if Config.DEBUG:
        try:  # ruff:ignore[too-many-statements-in-try-clause]
            if choices is not None:
                if not isinstance(choices, list):
                    raise AssertionError("Parameter `choices` - Not а `list` type!")
                if len(choices) == 0:
                    raise AssertionError("The `choices` parameter should not contain an empty list!")
            if default is not None:
                if not isinstance(default, list):
                    raise AssertionError("Parameter `default` - Not а `list` type!")
                if len(default) == 0:
                    raise AssertionError("The `default` parameter should not contain an empty list!")
                if choices is not None and not self.field_core.has_value():
                    raise AssertionError(
                        "Parameter `default` does not coincide with " + "list of permissive values in `choicees`.",
                    )
            if not isinstance(label, str):
                raise AssertionError("Parameter `label` - Not а `str` type!")
            if not isinstance(is_disable, bool):
                raise AssertionError("Parameter `is_disable` - Not а `bool` type!")
            if not isinstance(is_hide, bool):
                raise AssertionError("Parameter `is_hide` - Not а `bool` type!")
            if not isinstance(is_ignore, bool):
                raise AssertionError("Parameter `is_ignore` - Not а `bool` type!")
            if not isinstance(hint, str):
                raise AssertionError("Parameter `hint` - Not а `str` type!")
            if not isinstance(warning, list):
                raise AssertionError("Parameter `warning` - Not а `list` type!")
            if not isinstance(is_require, bool):
                raise AssertionError("Parameter `is_require` - Not а `bool` type!")
            if not isinstance(is_readonly, bool):
                raise AssertionError("Parameter `is_readonly` - Not а `bool` type!")
        except AssertionError as err:
            logger.critical(str(err))
            raise err

has_value(self, is_migrate=False)

Does the field value match the possible options in choices.

Source code in src/ramifice/fields/choice_float_mult_field.py
def has_value(self, is_migrate: bool = False) -> bool:  # ruff: ignore[unused-function-argument]
    """Does the field value match the possible options in choices."""
    value = self.value
    if value is None:
        value = self.default
    if value is not None:
        choices = self.choices
        if len(value) == 0 or not bool(choices):
            return False
        value_list = [item[0] for item in choices]  # type: ignore[union-attr]
        for item in value:
            if item not in value_list:
                return False
    return True