File size: 6,349 Bytes
6c6354c
7f48a2b
6c6354c
7f48a2b
d41bf71
 
 
 
3a4c52b
d41bf71
 
 
 
 
 
6fbe8fc
d41bf71
6fbe8fc
d41bf71
 
 
 
 
 
3a4c52b
 
 
 
6fbe8fc
3a4c52b
6fbe8fc
 
3a4c52b
 
 
d41bf71
 
 
 
6c6354c
d41bf71
 
7f48a2b
6c6354c
 
 
7f48a2b
d41bf71
6c6354c
7f48a2b
 
3a4c52b
 
 
 
 
 
 
 
 
 
 
 
 
6c6354c
 
 
 
 
 
 
 
 
 
 
6fbe8fc
6c6354c
 
 
d41bf71
7f48a2b
 
 
 
 
 
 
 
d41bf71
7f48a2b
 
 
 
 
 
f43a395
7f48a2b
d41bf71
7f48a2b
d41bf71
7f48a2b
d41bf71
 
6c6354c
 
6fbe8fc
d41bf71
 
7f48a2b
 
6c6354c
d41bf71
 
7f48a2b
 
 
 
 
 
 
 
 
 
6c6354c
d41bf71
 
 
 
7f48a2b
 
 
 
 
 
 
 
 
 
 
 
 
6c6354c
7f48a2b
 
1be54f0
7f48a2b
1be54f0
 
 
 
 
 
7f48a2b
 
6c6354c
7f48a2b
6c6354c
 
7f48a2b
 
6c6354c
 
 
 
 
 
6fbe8fc
6c6354c
 
 
 
 
 
6fbe8fc
6c6354c
 
 
7f48a2b
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
from typing import Any, Dict, List, Optional, Literal
import re
from ..tool_utils import *

class PlainEnumValueModel(BaseModel):
    system_name: str = Field(
        description="Enum value system name. RU: Системное имя значения",
    )
    russian_name: Optional[str] = Field(
        description="Enum value Russian name. RU: Русское название значения",
    )
    english_name: Optional[str] = Field(
        default=None,
        description="Enum value English name. RU: Английское название значения",
    )
    german_name: Optional[str] = Field(
        default=None,
        description="Enum value German name. RU: Немецкое название значения",
    )
    color: Optional[str] = Field(
        default=None,
        description="Enum value display color via hex code. RU: Цвет отображения значения"
    )

    @model_validator(mode='after')
    def check_at_least_one_name(cls, values):
        russian = values.russian_name
        english = values.english_name
        german = values.german_name

        if not any([russian, english, german]):
            raise ValueError("At least one of 'russian_name', 'english_name', or 'german_name' must be provided.")

        return values

    @field_validator('color')
    def validate_hex_color(cls, v):
        if v is None:
            return v
        if not re.match(r'^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$', str(v)):
            raise ValueError('Color must be a valid hex color code, e.g. #RRGGBB or #RGB')
        return v

class EditOrCreateEnumAttributeSchema(CommonAttributeFields):
    display_format: Literal["Text", "Indicator", "Badge"] = Field(
        description="Attribute display format. RU: Формат отображения."
    )
    enum_values: List[PlainEnumValueModel] = Field(
        description="Attribute enum values. RU: Варианты значений атрибута"
    )

    @field_validator("display_format", "enum_values", mode="before")
    def non_empty_str(cls, v: Any) -> Any:
        """
        Validate that string fields are not empty.
        
        This field validator is automatically applied to the name, system_name, 
        application_system_name, and template_system_name fields in all schemas
        that inherit from CommonAttributeFields, ensuring consistent validation.
        """
        if isinstance(v, str) and v.strip() == "":
            raise ValueError("must be a non-empty string")
        return v

def convert_plain_to_enum_value(plain: PlainEnumValueModel, attr_system_name: str) -> Dict[str, Any]:
    """Build API-ready variant payload from a plain enum value."""
    return {
        "alias": {
            "type": "Variant",
            "owner": attr_system_name,
            "alias": plain.system_name,
        },
        "name": {
            "ru": plain.russian_name,
            "en": plain.english_name,
            "de": plain.german_name,
        },
        "color": plain.color,
    }

@tool("edit_or_create_enum_attribute", return_direct=False, args_schema=EditOrCreateEnumAttributeSchema)
def edit_or_create_enum_attribute(
    operation: str,
    name: str,
    system_name: str,
    application_system_name: str,
    template_system_name: str,
    display_format: str,
    enum_values: List[PlainEnumValueModel],
    description: Optional[str] = None,
    write_changes_to_the_log: Optional[bool] = False,
    calculate_value: Optional[bool] = False,
    expression_for_calculation: Optional[str] = None,
) -> Dict[str, Any]:
    r"""
    Edit or Create a enum attribute (Список значений).
    
    IMPORTANT: When providing `enum_values`, you MUST follow this exact structure (see schema description for example):

    Example `enum_values`:
    [
      {
        "system_name": "status_active",
        "russian_name": "Активен",
        "english_name": "Active",
        "german_name": "Aktiv",
        "color": "#4CAF50"
      }
    ]

    - `russian_name` is REQUIRED.
    - `system_name` is the system name of the enum value.
    - `color` must be valid hex (e.g. #RRGGBB or #RGB) or omitted.

    Returns:
        dict: {
            "success": bool - True if the attribute was created or edited successfully
            "status_code": int - HTTP response status code  
            "raw_response": dict|str|None - Raw response for auditing or payload body (sanitized)
            "error": str|None - Error message if operation failed
        }
    """

    convert_enum_values: List[Dict[str, Any]] = [
        convert_plain_to_enum_value(plain_enum_value, system_name)
        for plain_enum_value in enum_values
    ]

    request_body: Dict[str, Any] = {
        "globalAlias": {
            "owner": template_system_name,
            "type": "Undefined",
            "alias": system_name
        },
        "type": "Enum",
        "format": display_format,
        "name": name,
        "description": description,
        "isTracked": write_changes_to_the_log,
        "isCalculated": calculate_value if expression_for_calculation != None else False,
        "expression": expression_for_calculation,
        "variants": convert_enum_values
    }

    endpoint = f"{ATTRIBUTE_ENDPOINT}/{application_system_name}"

    return execute_edit_or_create_operation(
        request_body=request_body,
        operation=operation,
        endpoint=endpoint,
        result_model=AttributeResult
    )

if __name__ == "__main__":
    results = edit_or_create_enum_attribute.invoke({
        "operation": "create",
        "name": "Status",
        "system_name": "Status",
        "application_system_name": "AItestAndApi",
        "template_system_name": "Test",
        "display_format": "Text",
        "enum_values": [
            {
                "system_name": "status_active",
                "russian_name": "Активен",
                "english_name": "Active",
                "german_name": "Aktiv",
                "color": "#4CAF50"
            },
            {
                "system_name": "status_inactive",
                "russian_name": "Неактивен",
                "english_name": "Inactive",
                "german_name": "Inaktiv",
                "color": "#9E9E9E"
            }
        ]
    })
    print(results)