control Package

control Package

_const Module

_control_api Module

class silf.backend.commons.api.stanza_content.control._control_api.Control(name, label, type, style=None, default_value=None, live=False, validators=None, required=True, description=None, order=None)

Bases: object

Class representing a input field in WEB UI.

>>> c = Control("foo", "Foo label", "Foo type")
>>> c
<Control name=foo type=Foo type live=False label='Foo label'>
>>> c.to_json_dict() == {'type': 'Foo type', 'metadata': {'label': 'Foo label'}, 'live': False, 'name': 'foo', 'order': None}
True
>>> c.default_value=3
>>> c.to_json_dict() ==  {'default_value': 3, 'name': 'foo', 'live': False, 'metadata': {'label': 'Foo label'}, 'type': 'Foo type', 'order': None}
True
>>> c.style = "indicator"
>>> c.description = "Foo Bar"
>>> c.to_json_dict() == {'name': 'foo', 'default_value': 3, 'type': 'Foo type', 'live': False, 'metadata': {'style': 'indicator', 'label': 'Foo label', 'description': 'Foo Bar'}, 'order': None}
True
convert_json_to_python(value)

Converts python value to json representation of this value.

Parameters:value (object) – Unparsed value reclieved from json schema.

Note

Do not override this method, rather override _json_to_python() as this method performs additional checks,

convert_python_to_json(value, omit_validation=False)

Converts json value to python object.

Note

Do not override this method, rather override _python_to_json() as this method performs additional checks,

get_value_type()
html_class
id = None

Html id of the field. Defaults to "id_{}".format(self.name)

live = None

Value of data-live attribute. This value governs when settings from this field are sent. Defaults to False

name

Html name of the field. Required.

post_construct_validate()

Overriden to perform any post-construction validation.

style = None

HTML class of the field. Defaults to None.

to_json_dict()
validators = None

List of validators attached to this field. Validators are python callables that should raise silf.backend.commons.api.error.SILFProtocolError.

value_is_empty(raw_value)

Checks whether raw_value shoule be considered empty. :param object raw_value: Unparsed value reclieved from json schema. :return:

_controls Module

class silf.backend.commons.api.stanza_content.control._controls.IntegerControl(name, label, type='number', style=None, default_value=None, live=False, validators=None, required=True, description=None, min_value=None, max_value=None, step=None)

Bases: silf.backend.commons.api.stanza_content.control._controls.NumberControl

class silf.backend.commons.api.stanza_content.control._controls.NumberControl(name, label, type='number', style=None, default_value=None, live=False, validators=None, required=True, description=None, min_value=None, max_value=None, step=None)

Bases: silf.backend.commons.api.stanza_content.control._control_api.Control

Control that take integer input, rendered as “number” field.

>>> control = NumberControl("foo", "Enter a number")
>>> control.to_json_dict() == {'type': 'number', 'name': 'foo', 'metadata': {'label': 'Enter a number'}, 'live': False, 'validations': {}, 'order': None}
True
>>> control.min_value = 1
>>> control.max_value = 10
>>> control.description = "Foo"
>>> control.style = "gauge"
>>> control.to_json_dict() ==  {
...     'type': 'number',
...     'validations': {'min_value': 1, 'max_value': 10},
...     'live': False,
...     'metadata': {'label': 'Enter a number', 'description': 'Foo', 'style': 'gauge'},
...     'name': 'foo',
...     'order': None}
True
>>> control.convert_json_to_python("5")
5.0
>>> control.convert_json_to_python("5.5")
5.5
>>> control.convert_json_to_python("aaa")
Traceback (most recent call last):
silf.backend.commons.api.exceptions.ValidationError: Pole "Enter a number" powinno przyjmować wartość liczbową. Nie udało się skonwerować: aaa
do wartości liczbowej.
>>> control.convert_json_to_python(5)
5.0
>>> control.convert_json_to_python(11)
Traceback (most recent call last):
silf.backend.commons.api.exceptions.ValidationError: Wartość w polu "Enter a number" powinna być mniejsza niż 10 a wynosi 11.0.
>>> control.convert_json_to_python(-1)
Traceback (most recent call last):
silf.backend.commons.api.exceptions.ValidationError: Wartość w polu "Enter a number" powinna być większa niż 1 a wynosi -1.0.
get_value_type()
to_json_dict()
class silf.backend.commons.api.stanza_content.control._controls.TimeControlMinSec(name, label, type='interval', style=None, default_value=None, live=False, validators=None, required=True, description=None, max_time=None, min_time=datetime.timedelta(0))

Bases: silf.backend.commons.api.stanza_content.control._control_api.Control

Control that renders to silf-proprietary input field that allows input time deltas in format MM:SS.

>>> control = TimeControlMinSec("foo", "Insert time",
...    min_time=datetime.timedelta(seconds=30),
...    max_time=datetime.timedelta(days=60))
>>> control.to_json_dict() ==  {'type': 'interval', 'validations': {'max_value': 5184000.0, 'min_value': 30.0}, 'name': 'foo', 'metadata': {'label': 'Insert time'}, 'live': False, 'order': None}
True
>>> control.convert_json_to_python(75)
datetime.timedelta(0, 75)
>>> control.convert_python_to_json(datetime.timedelta(days = 10))
864000.0
>>> control.convert_json_to_python("bar")
Traceback (most recent call last):
silf.backend.commons.api.exceptions.ValidationError: Podaj ilość sekund jako "Insert time". Nie udało się przekształcić tej wartości: "bar" na stałą ilczbową.
>>> control.convert_json_to_python(0)
Traceback (most recent call last):
silf.backend.commons.api.exceptions.ValidationError: Minimalny czas w polu "Insert time" wynosi 0:00:30. Ustawiono mniejszą wartość: 0:00:00.
>>> control.convert_python_to_json(datetime.timedelta(days = 60, seconds=1))
Traceback (most recent call last):
silf.backend.commons.api.exceptions.ValidationError: Maksymalny czas w polu "Insert time" wynosi 60 days, 0:00:00. Ustawiono większą wartość: 60 days, 0:00:01.
get_value_type()
to_json_dict()
class silf.backend.commons.api.stanza_content.control._controls.BooleanControl(name, label, type='boolean', style=None, default_value=None, live=False, validators=None, required=True, description=None)

Bases: silf.backend.commons.api.stanza_content.control._control_api.Control

get_value_type()
class silf.backend.commons.api.stanza_content.control._controls.ComboBoxControl(name, label, item_type=<class 'str'>, choices=None, **kwargs)

Bases: silf.backend.commons.api.stanza_content.control._control_api.Control

choices
post_construct_validate()
to_json_dict()

_suite Module

class silf.backend.commons.api.stanza_content.control._suite.ControlSuite(*args)

Bases: object

check_and_convert_live_settings(setting_suite: silf.backend.commons.api.stanza_content._misc.SettingSuite, old_settings: dict) → dict
Checks settings and returns parsed values as a dictionaty.
>>> from silf.backend.commons.api import SettingSuite, Setting
>>> c = ControlSuite(
...   NumberControl("foo", "Insert foo", max_value=10, live=True),
...   NumberControl("bar", "Insert bar", required=False)
... )
>>> old_settings = {'foo': 1.0, "bar":2.0}
>>> converted = c.check_and_convert_live_settings(SettingSuite(foo=Setting(5, False)), old_settings)
>>> converted == {'foo': 5.0, 'bar': 2.0}
True
>>> c.check_and_convert_live_settings(SettingSuite(bar=Setting(1, False)), old_settings)
Traceback (most recent call last):
silf.backend.commons.api.exceptions.ValidationError: Pole Insert bar zostało ustawione przez settings:update, ale nie zostało oznaczone jako live.
Parameters:
  • setting_suite
  • old_settings
Returns:

check_and_convert_settings(setting_suite)

Checks settings and returns parsed values as a dictionaty.

>>> from silf.backend.commons.api import SettingSuite, Setting
>>> c = ControlSuite(
...   NumberControl("foo", "Insert foo", max_value=10),
...   NumberControl("bar", "Insert bar", required=False)
... )
>>> i = SettingSuite(
...    foo = Setting(1, False),
...    bar = Setting(2, False)
... )
>>> c.check_and_convert_settings(i) == {'bar': 2, 'foo': 1}
True
>>> i = SettingSuite(
...    foo = Setting(11, False),
...    bar = Setting(2, False)
... )
>>> c.check_and_convert_settings(i)
Traceback (most recent call last):
silf.backend.commons.api.exceptions.ValidationError: Wartość w polu "Insert foo" powinna być mniejsza niż 10 a wynosi 11.0.
>>> i = SettingSuite(
...    bar = Setting(9, True)
... )
>>> c.check_settings(i)
check_settings(setting_suite, ignore_unset_settings=True) → dict

Checks settings received from json and converts them to python domain.

>>> c = ControlSuite(
...   NumberControl("foo", "Insert foo", max_value=10),
...   NumberControl("bar", "Insert bar")
... )
>>> from silf.backend.commons.api import SettingSuite, Setting
>>> i = SettingSuite(
...    foo = Setting(1, False),
...    bar = Setting(2, False)
... )
>>> c.check_settings(i)
>>> i = SettingSuite(
...    foo = Setting(11, True),
...    bar = Setting(2, False)
... )
>>> c.check_settings(i)
Traceback (most recent call last):
silf.backend.commons.api.exceptions.ValidationError: Wartość w polu "Insert foo" powinna być mniejsza niż 10 a wynosi 11.0.
>>> i = SettingSuite(
...    foo = Setting(9, True),
... )
>>> c.check_settings(i)
Parameters:
  • setting_suite (SettingSuite) – Settings read from json.
  • ignore_current_parameter (bool) – Whether this is called before applying settings (check_only``=False) or only to check them and display errors to user. If ``check_only is true this method might omit specific validations for fields that are not currently edited.
Raises:

ValidationError – If there is a validation error.

Returns:

None

controls = []
classmethod join(*args)
Parameters:args
Returns:
to_input_field_suite()

Converts this instance to misc.InputFieldSuite.