Skip to content

IPField

Field of Model for enter IP address.

IPField

Bases: Field

Field of Model for enter IP address.

Agrs

label: Text label for a web form field. placeholder: Displays prompt text. default: Value by default. 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. is_unique: The unique value of a field in a collection.

Source code in src/ramifice/fields/ip_field.py
class IPField(Field):
    """Field of Model for enter IP address.

    Agrs:
        label: Text label for a web form field.
        placeholder: Displays prompt text.
        default: Value by default.
        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.
        is_unique: The unique value of a field in a collection.
    """

    def __init__(  # ruff:ignore[undocumented-public-init]
        self,
        label: str = "",
        placeholder: str = "",
        default: str | 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,
        is_unique: bool = False,
    ) -> None:
        if Config.DEBUG:
            try:  # ruff:ignore[too-many-statements-in-try-clause]
                if default is not None:
                    if not isinstance(default, str):
                        raise AssertionError("Parameter `default` - Not а `str` type!")
                    if len(default) == 0:
                        raise AssertionError("The `default` parameter should not contain an empty string!")
                    try:
                        ipaddress.ip_address(default)
                    except ValueError:
                        raise AssertionError("Parameter `default` - Invalid IP address!")  # ruff:ignore[raise-without-from-inside-except]
                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(placeholder, str):
                    raise AssertionError("Parameter `placeholder` - Not а `str` 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!")
                if not isinstance(is_unique, bool):
                    raise AssertionError("Parameter `unique` - Not а `bool` type!")
            except AssertionError as err:
                logger.critical(str(err))
                raise err

        Field.__init__(self, supported_types=(str, type(None)))

        field_core: dict[str, Any] = {
            "id": "",
            "name": "",
            "label": label,
            "input_type": "text",
            "value": None,
            "default": default,
            "placeholder": placeholder,
            "is_hide": is_hide,
            "is_disable": is_disable,
            "is_ignore": is_ignore,
            "hint": hint,
            "warning": warning,
            "is_require": is_require,
            "is_readonly": is_readonly,
            "is_unique": is_unique,
            "errors": [],
            "field_type": "IPField",
            "group": "text",
        }

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

size(self)

Return length of field value.

Source code in src/ramifice/fields/ip_field.py
def size(self) -> int:
    """Return length of field `value`."""
    value = self.value
    if isinstance(value, str):
        return len(value)
    return 0