1
1
from .compound_file import CompoundFile
2
+ from uuid import uuid4
3
+ import operator
2
4
3
5
class Conversational :
4
6
def __init__ (self , project , name ):
@@ -30,9 +32,9 @@ def add_conversationa_attributes_if_doesnt_exist(self):
30
32
message_date_attribute = attribute
31
33
32
34
if message_author_attribute is None :
33
- new_message_author_attribute = self .project .attribute .new (default_schema )
35
+ message_author_attribute = self .project .attribute .new (default_schema )
34
36
self .project .attribute .update (
35
- new_message_author_attribute ,
37
+ message_author_attribute ,
36
38
prompt = "Author" ,
37
39
kind = "text" ,
38
40
name = "message_author" ,
@@ -41,9 +43,9 @@ def add_conversationa_attributes_if_doesnt_exist(self):
41
43
)
42
44
43
45
if message_time_attribute is None :
44
- new_message_time_attribute = self .project .attribute .new (default_schema )
46
+ message_time_attribute = self .project .attribute .new (default_schema )
45
47
self .project .attribute .update (
46
- new_message_time_attribute ,
48
+ message_time_attribute ,
47
49
prompt = "Time" ,
48
50
kind = "time" ,
49
51
name = "message_time" ,
@@ -52,16 +54,20 @@ def add_conversationa_attributes_if_doesnt_exist(self):
52
54
)
53
55
54
56
if message_date_attribute is None :
55
- new_message_date_attribute = self .project .attribute .new (default_schema )
57
+ message_date_attribute = self .project .attribute .new (default_schema )
56
58
self .project .attribute .update (
57
- new_message_date_attribute ,
59
+ message_date_attribute ,
58
60
prompt = "Date" ,
59
61
kind = "date" ,
60
62
name = "message_date" ,
61
63
is_global = True ,
62
64
global_type = 'file'
63
65
)
64
66
67
+ self .author_attribute = message_author_attribute
68
+ self .time_attribute = message_time_attribute
69
+ self .date_attribute = message_date_attribute
70
+
65
71
def add_message (self , message_file , author = None , time = None , date = None ):
66
72
message_meta = {
67
73
"author" : author ,
@@ -73,5 +79,34 @@ def add_message(self, message_file, author=None, time=None, date=None):
73
79
74
80
self .parent .add_child_from_local (path = message_file , ordinal = len (self .messgaes_meta ))
75
81
82
+ def _new_global_instance (self ):
83
+ return {
84
+ "creation_ref_id" : str (uuid4 ()),
85
+ "type" : "global" ,
86
+ "attribute_groups" : {}
87
+ }
88
+
89
+
76
90
def upload (self ):
77
- self .parent .upload ()
91
+ self .parent .upload ()
92
+ child_files = self .parent .fetch_child_files ()
93
+ child_files .sort (key = operator .attrgetter ('id' ))
94
+
95
+ for index in range (0 , len (child_files )):
96
+ global_instance_for_child = self ._new_global_instance ()
97
+
98
+ if self .messgaes_meta [index ]["author" ] is not None :
99
+ global_instance_for_child ["attribute_groups" ][self .author_attribute ["id" ]] = self .messgaes_meta [index ]["author" ]
100
+ if self .messgaes_meta [index ]["time" ] is not None :
101
+ global_instance_for_child ["attribute_groups" ][self .time_attribute ["id" ]] = self .messgaes_meta [index ]["time" ]
102
+ if self .messgaes_meta [index ]["date" ] is not None :
103
+ global_instance_for_child ["attribute_groups" ][self .date_attribute ["id" ]] = self .messgaes_meta [index ]["date" ]
104
+
105
+ payload = {
106
+ "instance_list" : [global_instance_for_child ],
107
+ "and_complete" : False ,
108
+ "child_file_save_id" : child_files [index ].id
109
+ }
110
+
111
+ response = self .project .session .post (url = self .project .host + f"/api/project/{ self .project .project_string_id } /file/{ child_files [index ].id } /annotation/update" , json = payload )
112
+ self .project .handle_errors (response )
0 commit comments