22
33import collections
44import json
5- from typing import Iterable , Dict , List
5+ from typing import Dict , Iterable , List
66
77from TM1py .Objects .TM1Object import TM1Object
88from TM1py .Utils import format_url
99
1010
1111class Annotation (TM1Object ):
12- """ Abtraction of TM1 Annotation
12+ """Abtraction of TM1 Annotation
1313
14- :Notes:
15- - Class complete, functional and tested.
16- - doesn't cover Attachments though
14+ :Notes:
15+ - Class complete, functional and tested.
16+ - doesn't cover Attachments though
1717 """
1818
19- def __init__ (self , comment_value : str , object_name : str , dimensional_context : Iterable [str ],
20- comment_type : str = 'ANNOTATION' , annotation_id : str = None ,
21- text : str = '' , creator : str = None , created : str = None , last_updated_by : str = None ,
22- last_updated : str = None ):
19+ def __init__ (
20+ self ,
21+ comment_value : str ,
22+ object_name : str ,
23+ dimensional_context : Iterable [str ],
24+ comment_type : str = "ANNOTATION" ,
25+ annotation_id : str = None ,
26+ text : str = "" ,
27+ creator : str = None ,
28+ created : str = None ,
29+ last_updated_by : str = None ,
30+ last_updated : str = None ,
31+ ):
2332 self ._id = annotation_id
2433 self ._text = text
2534 self ._creator = creator
@@ -32,26 +41,35 @@ def __init__(self, comment_value: str, object_name: str, dimensional_context: It
3241 self ._object_name = object_name
3342
3443 @classmethod
35- def from_json (cls , annotation_as_json : str ) -> ' Annotation' :
36- """ Alternative constructor
44+ def from_json (cls , annotation_as_json : str ) -> " Annotation" :
45+ """Alternative constructor
3746
38- :param annotation_as_json: String, JSON
39- :return: instance of TM1py.Process
47+ :param annotation_as_json: String, JSON
48+ :return: instance of TM1py.Process
4049 """
4150 annotation_as_dict = json .loads (annotation_as_json )
42- annotation_id = annotation_as_dict ['ID' ]
43- text = annotation_as_dict ['Text' ]
44- creator = annotation_as_dict ['Creator' ]
45- created = annotation_as_dict ['Created' ]
46- last_updated_by = annotation_as_dict ['LastUpdatedBy' ]
47- last_updated = annotation_as_dict ['LastUpdated' ]
48- dimensional_context = [item ['Name' ] for item in annotation_as_dict ['DimensionalContext' ]]
49- comment_type = annotation_as_dict ['commentType' ]
50- comment_value = annotation_as_dict ['commentValue' ]
51- object_name = annotation_as_dict ['objectName' ]
52- return cls (comment_value = comment_value , object_name = object_name , dimensional_context = dimensional_context ,
53- comment_type = comment_type , annotation_id = annotation_id , text = text , creator = creator , created = created ,
54- last_updated_by = last_updated_by , last_updated = last_updated )
51+ annotation_id = annotation_as_dict ["ID" ]
52+ text = annotation_as_dict ["Text" ]
53+ creator = annotation_as_dict ["Creator" ]
54+ created = annotation_as_dict ["Created" ]
55+ last_updated_by = annotation_as_dict ["LastUpdatedBy" ]
56+ last_updated = annotation_as_dict ["LastUpdated" ]
57+ dimensional_context = [item ["Name" ] for item in annotation_as_dict ["DimensionalContext" ]]
58+ comment_type = annotation_as_dict ["commentType" ]
59+ comment_value = annotation_as_dict ["commentValue" ]
60+ object_name = annotation_as_dict ["objectName" ]
61+ return cls (
62+ comment_value = comment_value ,
63+ object_name = object_name ,
64+ dimensional_context = dimensional_context ,
65+ comment_type = comment_type ,
66+ annotation_id = annotation_id ,
67+ text = text ,
68+ creator = creator ,
69+ created = created ,
70+ last_updated_by = last_updated_by ,
71+ last_updated = last_updated ,
72+ )
5573
5674 @property
5775 def body (self ) -> str :
@@ -98,55 +116,55 @@ def id(self) -> str:
98116 return self ._id
99117
100118 def move (self , dimension_order : Iterable [str ], dimension : str , target_element : str , source_element : str = None ):
101- """ Move annotation on given dimension from source_element to target_element
102-
103- :param dimension_order: List, order of the dimensions in the cube
104- :param dimension: dimension name
105- :param target_element: target element name
106- :param source_element: source element name
107- :return:
119+ """Move annotation on given dimension from source_element to target_element
120+
121+ :param dimension_order: List, order of the dimensions in the cube
122+ :param dimension: dimension name
123+ :param target_element: target element name
124+ :param source_element: source element name
125+ :return:
108126 """
109127 for i , dimension_name in enumerate (dimension_order ):
110128 if dimension_name .lower () == dimension .lower ():
111129 if not source_element or self ._dimensional_context [i ] == source_element :
112130 self ._dimensional_context [i ] = target_element
113131
114132 def _construct_body (self ) -> Dict :
115- """ construct the ODATA conform JSON represenation for the Annotation entity.
133+ """construct the ODATA conform JSON represenation for the Annotation entity.
116134
117- :return: string, the valid JSON
135+ :return: string, the valid JSON
118136 """
119- dimensional_context = [{' Name' : element } for element in self ._dimensional_context ]
137+ dimensional_context = [{" Name" : element } for element in self ._dimensional_context ]
120138 body = collections .OrderedDict ()
121- body ['ID' ] = self ._id
122- body [' Text' ] = self ._text
123- body [' Creator' ] = self ._creator
124- body [' Created' ] = self ._created
125- body [' LastUpdatedBy' ] = self ._last_updated_by
126- body [' LastUpdated' ] = self ._last_updated
127- body [' DimensionalContext' ] = dimensional_context
128- comment_locations = ',' .join (self ._dimensional_context )
129- body [' commentLocation' ] = comment_locations [1 :]
130- body [' commentType' ] = self ._comment_type
131- body [' commentValue' ] = self ._comment_value
132- body [' objectName' ] = self ._object_name
139+ body ["ID" ] = self ._id
140+ body [" Text" ] = self ._text
141+ body [" Creator" ] = self ._creator
142+ body [" Created" ] = self ._created
143+ body [" LastUpdatedBy" ] = self ._last_updated_by
144+ body [" LastUpdated" ] = self ._last_updated
145+ body [" DimensionalContext" ] = dimensional_context
146+ comment_locations = "," .join (self ._dimensional_context )
147+ body [" commentLocation" ] = comment_locations [1 :]
148+ body [" commentType" ] = self ._comment_type
149+ body [" commentValue" ] = self ._comment_value
150+ body [" objectName" ] = self ._object_name
133151 return body
134152
135153 def construct_body_for_post (self , cube_dimensions ) -> Dict :
136154 body = collections .OrderedDict ()
137155 body ["Text" ] = self .text
138- body ["ApplicationContext" ] = [{
139- "[email protected] " :
"ApplicationContextFacets('}Cubes')" ,
140- "Value" : self . object_name } ]
156+ body ["ApplicationContext" ] = [
157+ { "[email protected] " :
"ApplicationContextFacets('}Cubes')" ,
"Value" : self . object_name } 158+ ]
141159142160
143161 for dimension , element in zip (cube_dimensions , self .dimensional_context ):
144162 coordinates = format_url ("Dimensions('{}')/Hierarchies('{}')/Members('{}')" , dimension , dimension , element )
145163 body [
"[email protected] " ].
append (
coordinates )
146164
147- body [' objectName' ] = self .object_name
148- body [' commentValue' ] = self .comment_value
149- body [' commentType' ] = ' ANNOTATION'
150- body [' commentLocation' ] = ',' .join (self .dimensional_context )
165+ body [" objectName" ] = self .object_name
166+ body [" commentValue" ] = self .comment_value
167+ body [" commentType" ] = " ANNOTATION"
168+ body [" commentLocation" ] = "," .join (self .dimensional_context )
151169
152170 return body
0 commit comments