class URLField(Field):
"""Field of Model for enter URL address."""
def __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:
"""Field of Model for enter URL 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.
"""
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!")
result = urlparse(default)
if not result.scheme or not result.netloc:
raise AssertionError("Parameter `default` - Invalid URL address!")
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": "url",
"value": None,
"placeholder": placeholder,
"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,
"is_unique": is_unique,
"errors": [],
"field_type": "URLField",
"group": "text",
}
self.__dict__["field_core"] = FieldCore(**field_core)
self.field_core.size = MethodType(size, self.field_core)