11from .compound_file import CompoundFile
2+ from uuid import uuid4
3+ import operator
24
35class Conversational :
46 def __init__ (self , project , name ):
@@ -30,9 +32,9 @@ def add_conversationa_attributes_if_doesnt_exist(self):
3032 message_date_attribute = attribute
3133
3234 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 )
3436 self .project .attribute .update (
35- new_message_author_attribute ,
37+ message_author_attribute ,
3638 prompt = "Author" ,
3739 kind = "text" ,
3840 name = "message_author" ,
@@ -41,9 +43,9 @@ def add_conversationa_attributes_if_doesnt_exist(self):
4143 )
4244
4345 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 )
4547 self .project .attribute .update (
46- new_message_time_attribute ,
48+ message_time_attribute ,
4749 prompt = "Time" ,
4850 kind = "time" ,
4951 name = "message_time" ,
@@ -52,16 +54,20 @@ def add_conversationa_attributes_if_doesnt_exist(self):
5254 )
5355
5456 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 )
5658 self .project .attribute .update (
57- new_message_date_attribute ,
59+ message_date_attribute ,
5860 prompt = "Date" ,
5961 kind = "date" ,
6062 name = "message_date" ,
6163 is_global = True ,
6264 global_type = 'file'
6365 )
6466
67+ self .author_attribute = message_author_attribute
68+ self .time_attribute = message_time_attribute
69+ self .date_attribute = message_date_attribute
70+
6571 def add_message (self , message_file , author = None , time = None , date = None ):
6672 message_meta = {
6773 "author" : author ,
@@ -73,5 +79,34 @@ def add_message(self, message_file, author=None, time=None, date=None):
7379
7480 self .parent .add_child_from_local (path = message_file , ordinal = len (self .messgaes_meta ))
7581
82+ def _new_global_instance (self ):
83+ return {
84+ "creation_ref_id" : str (uuid4 ()),
85+ "type" : "global" ,
86+ "attribute_groups" : {}
87+ }
88+
89+
7690 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