Skip to content

utils

webaction_fields_to_attribute_defs(fields)

Helper function to convert fields definition from webactions form.

Convert form fields to attribute definitions to be able to display them using attribute definitions.

Parameters:

Name Type Description Default
fields list[dict[str, Any]]

Fields from webaction form.

required

Returns:

Type Description
list[AbstractAttrDef]

list[AbstractAttrDef]: Converted attribute definitions.

Source code in client/ayon_core/pipeline/actions/utils.py
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
def webaction_fields_to_attribute_defs(
    fields: list[dict[str, Any]]
) -> list[AbstractAttrDef]:
    """Helper function to convert fields definition from webactions form.

    Convert form fields to attribute definitions to be able to display them
        using attribute definitions.

    Args:
        fields (list[dict[str, Any]]): Fields from webaction form.

    Returns:
        list[AbstractAttrDef]: Converted attribute definitions.

    """
    attr_defs = []
    for field in fields:
        field_type = field["type"]
        attr_def = None
        if field_type == "label":
            label = field.get("value")
            if label is None:
                label = field.get("text")
            attr_def = UILabelDef(
                label, key=uuid.uuid4().hex
            )
        elif field_type == "boolean":
            value = field["value"]
            if isinstance(value, str):
                value = value.lower() == "true"

            attr_def = BoolDef(
                field["name"],
                default=value,
                label=field.get("label"),
            )
        elif field_type == "text":
            attr_def = TextDef(
                field["name"],
                default=field.get("value"),
                label=field.get("label"),
                placeholder=field.get("placeholder"),
                multiline=field.get("multiline", False),
                regex=field.get("regex"),
                # syntax=field["syntax"],
            )
        elif field_type in ("integer", "float"):
            value = field.get("value")
            if isinstance(value, str):
                if field_type == "integer":
                    value = int(value)
                else:
                    value = float(value)
            attr_def = NumberDef(
                field["name"],
                default=value,
                label=field.get("label"),
                decimals=0 if field_type == "integer" else 5,
                # placeholder=field.get("placeholder"),
                minimum=field.get("min"),
                maximum=field.get("max"),
            )
        elif field_type in ("select", "multiselect"):
            attr_def = EnumDef(
                field["name"],
                items=field["options"],
                default=field.get("value"),
                label=field.get("label"),
                multiselection=field_type == "multiselect",
            )
        elif field_type == "hidden":
            attr_def = HiddenDef(
                field["name"],
                default=field.get("value"),
            )

        if attr_def is None:
            print(f"Unknown config field type: {field_type}")
            attr_def = UILabelDef(
                f"Unknown field type '{field_type}",
                key=uuid.uuid4().hex
            )
        attr_defs.append(attr_def)
    return attr_defs