Skip to content

Commit

Permalink
Create many annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
MariusWirtz committed Aug 30, 2022
1 parent 3492c8f commit 0af3ede
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 20 deletions.
28 changes: 26 additions & 2 deletions TM1py/Objects/Annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Iterable, Dict, List

from TM1py.Objects.TM1Object import TM1Object
from TM1py.Utils import format_url


class Annotation(TM1Object):
Expand Down Expand Up @@ -54,6 +55,10 @@ def from_json(cls, annotation_as_json: str) -> 'Annotation':

@property
def body(self) -> str:
return json.dumps(self._construct_body())

@property
def body_as_dict(self) -> Dict:
return self._construct_body()

@property
Expand Down Expand Up @@ -106,7 +111,7 @@ def move(self, dimension_order: Iterable[str], dimension: str, target_element: s
if not source_element or self._dimensional_context[i] == source_element:
self._dimensional_context[i] = target_element

def _construct_body(self) -> str:
def _construct_body(self) -> Dict:
""" construct the ODATA conform JSON represenation for the Annotation entity.
:return: string, the valid JSON
Expand All @@ -125,4 +130,23 @@ def _construct_body(self) -> str:
body['commentType'] = self._comment_type
body['commentValue'] = self._comment_value
body['objectName'] = self._object_name
return json.dumps(body, ensure_ascii=False)
return body

def construct_body_for_post(self, cube_dimensions) -> Dict:
body = collections.OrderedDict()
body["Text"] = self.text
body["ApplicationContext"] = [{
"Facet@odata.bind": "ApplicationContextFacets('}Cubes')",
"Value": self.object_name}]
body["DimensionalContext@odata.bind"] = []

for dimension, element in zip(cube_dimensions, self.dimensional_context):
coordinates = format_url("Dimensions('{}')/Hierarchies('{}')/Members('{}')", dimension, dimension, element)
body["DimensionalContext@odata.bind"].append(coordinates)

body['objectName'] = self.object_name
body['commentValue'] = self.comment_value
body['commentType'] = 'ANNOTATION'
body['commentLocation'] = ','.join(self.dimensional_context)

return body
45 changes: 27 additions & 18 deletions TM1py/Services/AnnotationService.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
# -*- coding: utf-8 -*-

import collections
import json
from typing import List
from typing import List, Iterable

from requests import Response

from TM1py.Objects.Annotation import Annotation
from TM1py.Services.ObjectService import ObjectService
from TM1py.Services.RestService import RestService
from TM1py.Utils import format_url
from TM1py.Utils import format_url, CaseAndSpaceInsensitiveDict


class AnnotationService(ObjectService):
Expand Down Expand Up @@ -39,25 +38,35 @@ def create(self, annotation: Annotation, **kwargs) -> Response:
"""
url = "/api/v1/Annotations"

payload = collections.OrderedDict()
payload["Text"] = annotation.text
payload["ApplicationContext"] = [{
"Facet@odata.bind": "ApplicationContextFacets('}Cubes')",
"Value": annotation.object_name}]
payload["DimensionalContext@odata.bind"] = []
from TM1py import CubeService
cube_dimensions = CubeService(self._rest).get_dimension_names(
cube_name=annotation.object_name,
skip_sandbox_dimension=True)
for dimension, element in zip(cube_dimensions, annotation.dimensional_context):
coordinates = format_url("Dimensions('{}')/Hierarchies('{}')/Members('{}')", dimension, dimension, element)
payload["DimensionalContext@odata.bind"].append(coordinates)
payload['objectName'] = annotation.object_name
payload['commentValue'] = annotation.comment_value
payload['commentType'] = 'ANNOTATION'
payload['commentLocation'] = ','.join(annotation.dimensional_context)

response = self._rest.POST(url, json.dumps(payload, ensure_ascii=False), **kwargs)

response = self._rest.POST(url, json.dumps(annotation.construct_body_for_post(cube_dimensions)), **kwargs)
return response

def create_many(self, annotations: Iterable[Annotation], **kwargs) -> Response:
""" create an Annotation
:param annotations: instances of TM1py.Annotation
"""
payload = list()
cube_dimensions = CaseAndSpaceInsensitiveDict()

for annotation in annotations:
dimension_names = cube_dimensions.get(annotation.object_name, None)
if not dimension_names:
from TM1py import CubeService
dimension_names = CubeService(self._rest).get_dimension_names(
cube_name=annotation.object_name,
skip_sandbox_dimension=True)

cube_dimensions[annotation.object_name] = dimension_names
payload.append(annotation.construct_body_for_post(dimension_names))

response = self._rest.POST("/api/v1/Annotations", json.dumps(payload), **kwargs)

return response

def get(self, annotation_id: str, **kwargs) -> Annotation:
Expand Down
21 changes: 21 additions & 0 deletions Tests/AnnotationService_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,27 @@ def test_create(self):
new_annotation = self.tm1.cubes.annotations.get(annotation_id)
self.assertEqual(new_annotation.comment_value, random_text)

def test_create_many(self):
"""
Check that an annotation can be created on the server
Check that created annotation has the correct comment_value
"""
pre_annotation_count = len(self.tm1.cubes.annotations.get_all(self.cube_name))

annotations = list()
for _ in range(5):
random_intersection = self.tm1.cubes.get_random_intersection(self.cube_name, False)
random_text = "".join([random.choice(string.printable) for _ in range(100)])

annotations.append(Annotation(
comment_value=random_text,
object_name=self.cube_name,
dimensional_context=random_intersection))

self.tm1.cubes.annotations.create_many(annotations)
all_annotations = self.tm1.cubes.annotations.get_all(self.cube_name)
self.assertEqual(len(all_annotations), pre_annotation_count + 5)

def test_get(self):
"""
Check that get returns the test annotation from its id
Expand Down

0 comments on commit 0af3ede

Please sign in to comment.