33 labeling_task_label ,
44 labeling_task ,
55 general ,
6+ knowledge_base ,
7+ information_source ,
8+ record_label_association ,
69)
710from controller .knowledge_base .util import create_knowledge_base_if_not_existing
811from submodules .model .enums import LabelingTaskType
12+ from submodules .model import enums
13+ from util import notification
14+
15+
16+ from typing import List , Any , Dict
17+ import re
918
1019
1120def get_label (project_id : str , label_id : str ) -> LabelingTaskLabel :
@@ -24,6 +33,33 @@ def update_label_hotkey(project_id: str, label_id: str, hotkey: str) -> None:
2433 general .commit ()
2534
2635
36+ def update_label_name (project_id : str , label_id : str , new_name : str ) -> None :
37+ label = get_label (project_id , label_id )
38+ label .name = new_name
39+ general .commit ()
40+
41+
42+ def handle_label_rename_warning (project_id : str , warning_data : Dict [str , str ]) -> None :
43+ if warning_data ["key" ] == enums .CommentCategory .KNOWLEDGE_BASE .value :
44+ knowledge_base_item = knowledge_base .get_by_name (
45+ project_id , warning_data ["old" ]
46+ )
47+ knowledge_base_item .name = warning_data ["new" ]
48+ general .commit ()
49+
50+ notification .send_organization_update (
51+ project_id , f"knowledge_base_updated:{ str (knowledge_base_item .id )} "
52+ )
53+ elif warning_data ["key" ] == enums .CommentCategory .HEURISTIC .value :
54+ information_source_item = information_source .get (project_id , warning_data ["id" ])
55+ information_source_item .source_code = warning_data ["new" ]
56+ general .commit ()
57+
58+ notification .send_organization_update (
59+ project_id , f"information_source_updated:{ str (information_source_item .id )} "
60+ )
61+
62+
2763def create_label (
2864 project_id : str , name : str , labeling_task_id : str , label_color : str
2965) -> LabelingTaskLabel :
@@ -39,3 +75,81 @@ def create_label(
3975
4076def delete_label (project_id : str , label_id : str ) -> None :
4177 labeling_task_label .delete (project_id , label_id , with_commit = True )
78+
79+
80+ def check_rename_label (project_id : str , label_id : str , new_name : str ) -> List [Any ]:
81+ label_item = labeling_task_label .get (project_id , label_id )
82+ if not label_item or label_item .name == new_name :
83+ return []
84+ return __get_change_dict (project_id , label_item , new_name )
85+
86+
87+ def __get_change_dict (
88+ project_id : str , label : LabelingTaskLabel , new_name : str
89+ ) -> Dict [str , str ]:
90+ return_values = {
91+ "errors" : __check_errors_label_rename (project_id , label , new_name ),
92+ "warnings" : __check_warnings_label_rename (project_id , label , new_name ),
93+ "infos" : [],
94+ }
95+ __check_label_rename_knowledge_base (project_id , label , new_name , return_values )
96+ if len (return_values ["errors" ]) == 0 and len (return_values ["warnings" ]) == 0 :
97+ return_values ["infos" ].append (__get_msg_dict ("No issues detected" ))
98+ return return_values
99+
100+
101+ def __get_msg_dict (msg : str ) -> Dict [str , str ]:
102+ return {"msg" : msg }
103+
104+
105+ def __check_errors_label_rename (
106+ project_id : str , label : LabelingTaskLabel , new_name : str
107+ ) -> List [Dict [str , Any ]]:
108+ append_me = []
109+ existing_with_name = labeling_task_label .get_by_name (
110+ project_id , str (label .labeling_task_id ), new_name
111+ )
112+ if existing_with_name :
113+ append_me .append (__get_msg_dict ("Label with name already exists" ))
114+ return append_me
115+
116+
117+ def __check_warnings_label_rename (
118+ project_id : str , label : LabelingTaskLabel , new_name : str
119+ ) -> List [Dict [str , Any ]]:
120+ append_me = []
121+
122+ information_sources = information_source .get_all (project_id )
123+ for information_source_item in information_sources :
124+ current_code = information_source_item .source_code
125+ new_code = re .sub (r"\b%s\b" % label .name , new_name , current_code )
126+ if current_code != new_code :
127+ entry = __get_msg_dict (
128+ "Information source with matching word was detected."
129+ )
130+ entry ["key" ] = enums .CommentCategory .HEURISTIC .value
131+ entry ["id" ] = str (information_source_item .id )
132+ entry ["old" ] = current_code
133+ entry ["new" ] = new_code
134+ entry ["old_name" ] = label .name
135+ entry ["new_name" ] = new_name
136+ append_me .append (entry )
137+
138+ return append_me
139+
140+
141+ def __check_label_rename_knowledge_base (
142+ project_id : str , label : LabelingTaskLabel , new_name : str , append_to
143+ ) -> None :
144+ knowledge_base_item = knowledge_base .get_by_name (project_id , label .name )
145+ if knowledge_base_item :
146+ entry = __get_msg_dict ("Lookup list with same name as label exists." )
147+ entry ["key" ] = enums .CommentCategory .KNOWLEDGE_BASE .value
148+ entry ["old" ] = knowledge_base_item .name
149+ entry ["new" ] = new_name
150+ knowledge_base_item_new = knowledge_base .get_by_name (project_id , new_name )
151+ if knowledge_base_item_new :
152+ entry ["msg" ] += "\n \t New label name however, already exists as lookup list."
153+ append_to ["errors" ].append (entry )
154+ else :
155+ append_to ["warnings" ].append (entry )
0 commit comments