File size: 4,793 Bytes
70cbb67
e3f664d
 
 
 
 
8a15af8
e3f664d
 
43da826
 
e3f664d
8a15af8
 
e3f664d
8a15af8
e3f664d
8a15af8
 
e3f664d
 
 
8a15af8
 
e3f664d
 
3a4c52b
 
 
 
179904f
3a4c52b
 
 
 
 
 
 
 
e3f664d
 
 
 
 
 
 
8a15af8
e3f664d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a15af8
e3f664d
 
 
 
8a15af8
f43a395
179904f
8abafca
179904f
8abafca
 
8a15af8
 
 
 
 
 
 
e3f664d
 
 
 
 
 
 
 
d2d7f20
e3f664d
 
 
8a15af8
e3f664d
 
 
 
 
1be54f0
e3f664d
1be54f0
 
 
 
 
 
e3f664d
 
8a15af8
e3f664d
8a15af8
 
e3f664d
 
8a15af8
 
 
e3f664d
 
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
from ..tool_utils import *

ALLOWED_EXTENSIONS_LIST = ['TXT', 'PNG', 'JPG', 'CSV', 'XLSX', 'DOCX', 'PPTX', 'VSDX', 'MSG', 'ZIP', 'BMP', 'EMF', 'DWG', 'BPMN', 'LOG', 'RAR', 'TAR', 'TAR.GZ(TGZ)', 'GZ', 'BZ2', 'TAR.BZ2', 'ENV', 'UNL', 'EML', 'SQL', 'ISO', 'CONF', 'ICO']

ALLOWED_EXTENSIONS = Literal[tuple(ALLOWED_EXTENSIONS_LIST)]
ALLOWED_EXTENSIONS_SET = set(ALLOWED_EXTENSIONS_LIST)
class EditOrCreateDocumentAttributeSchema(CommonAttributeFields):
    display_format: Literal[
        "Attachment",
        "SignedDocument",
        "InlineDocument"
    ] = Field(
        description="Attribute display format. "
                    "RU: 'Формат отображения'."
    )
    use_to_search_records: bool = Field(
        default=False,
        description="Set to `True` to allow the users to search the records by this attribute's value. "
                    "RU: Использовать для поиска записей",
    )
    file_extensions_filter: Optional[List[Literal[ALLOWED_EXTENSIONS]]] = Field(
        default=None,
        description="Filter for file extensions that the attribute can store. "
                    "RU: Фильтр расширений файлов"
    )

    @field_validator("display_format", 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

    @field_validator("file_extensions_filter", mode="before")
    def normalize_file_extensions_filter(cls, v: Any) -> Optional[List[str]]:
        if v is None:
            return v
        if isinstance(v, str):
            v = [x.strip() for x in v.split(",") if x.strip()]
        if not isinstance(v, list):
            raise ValueError("file_extensions_filter must be a list or comma-separated strings")    
        normalized = [str(ext).strip().lower() for ext in v]

        invalid = set(normalized) - ALLOWED_EXTENSIONS_SET
        if invalid:
            raise ValueError(f"Invalid file extensions: {sorted(invalid)}. " f"Allowed: {sorted(ALLOWED_EXTENSIONS_SET)}")
        return normalized

@tool("edit_or_create_document_attribute", return_direct=False, args_schema=EditOrCreateDocumentAttributeSchema)
def edit_or_create_document_attribute(
    operation: str,
    name: str,
    system_name: str,
    application_system_name: str,
    template_system_name: str,
    display_format: str,
    description: Optional[str] = None,
    use_to_search_records: Optional[bool] = False,
    write_changes_to_the_log: Optional[bool] = False,
    store_multiple_values: Optional[bool] = False,
    file_extensions_filter: Optional[List[str]] = None
) -> Dict[str, Any]:
    """
    Edit or Create a document attribute (Документа).

    Document attribute stores file attachments with configurable file format filters.

    Can also store signed documents with digital signatures.

    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
        }
    """

    request_body: Dict[str, Any] = {
        "globalAlias": {
            "owner": template_system_name,
            "type": "Undefined",
            "alias": system_name
        },
        "type": "Document",
        "format": display_format,
        "name": name,
        "description": description,
        "isIndexed": use_to_search_records,
        "isTracked": write_changes_to_the_log,
        "isMultiValue": store_multiple_values,
        "fileFormat": file_extensions_filter
    }

    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_document_attribute.invoke({
        "operation": "create",
        "name": "Contract Document",
        "system_name": "ContractDocument",
        "application_system_name": "AItestAndApi",
        "template_system_name": "Test",
        "display_format": "Attachment",
        "description": "Contract document attachment",
        "use_to_search_records": False
    })
    print(results)