Skip to content

Commit 86259c5

Browse files
Template editor JSON exporting and importing (#207)
1 parent 699d4d0 commit 86259c5

File tree

2 files changed

+232
-0
lines changed

2 files changed

+232
-0
lines changed

src/ansys/dynamicreporting/core/utils/report_remote_server.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,111 @@ def get_pptx_from_report(self, report_guid, directory_name=None, query=None):
972972
else:
973973
raise Exception(f"The server returned an error code {resp.status_code}")
974974

975+
def get_templates_as_json(self, root_guid):
976+
"""
977+
Convert report templates rooted as root_guid to JSON
978+
Return a python dictionary.
979+
"""
980+
templates_data = {}
981+
templates = self.get_objects(objtype=report_objects.TemplateREST)
982+
template_guid_id_map = {root_guid: 0}
983+
_build_template_data(root_guid, templates_data, templates, template_guid_id_map)
984+
return templates_data
985+
986+
def _populate_template(self, attr, parent_template):
987+
template = self.create_template(
988+
name=attr["name"], parent=parent_template, report_type=attr["report_type"]
989+
)
990+
template.set_params(attr["params"])
991+
if attr["sort_selection"] != "":
992+
template.set_sort_selection(value=attr["sort_selection"])
993+
template.set_tags(attr["tags"])
994+
template.set_filter(filter_str=attr["item_filter"])
995+
996+
return template
997+
998+
def _update_changes(self, id_str, id_template_map, templates_json):
999+
children_id_strs = templates_json[id_str]["children"]
1000+
if not children_id_strs:
1001+
return
1002+
1003+
for child_id_str in children_id_strs:
1004+
self._update_changes(child_id_str, id_template_map, templates_json)
1005+
self.put_objects(id_template_map[id_str])
1006+
1007+
def _build_templates_from_parent(self, id_str, id_template_map, templates_json):
1008+
children_id_strs = templates_json[id_str]["children"]
1009+
if not children_id_strs:
1010+
return
1011+
1012+
child_templates = []
1013+
for child_id_str in children_id_strs:
1014+
child_attr = templates_json[child_id_str]
1015+
child_template = self._populate_template(child_attr, id_template_map[id_str])
1016+
child_templates.append(child_template)
1017+
id_template_map[child_id_str] = child_template
1018+
1019+
self.put_objects(child_templates)
1020+
1021+
i = 0
1022+
for child_id_str in children_id_strs:
1023+
self._build_templates_from_parent(child_id_str, id_template_map, templates_json)
1024+
i += 1
1025+
1026+
def load_templates(self, templates_json):
1027+
"""
1028+
Load templates given a json-format data
1029+
"""
1030+
for template_id_str, template_attr in templates_json.items():
1031+
if template_attr["parent"] is None:
1032+
root_id_str = template_id_str
1033+
break
1034+
1035+
root_attr = templates_json[root_id_str]
1036+
root_template = self._populate_template(root_attr, None)
1037+
self.put_objects(root_template)
1038+
id_template_map = {}
1039+
id_template_map[root_id_str] = root_template
1040+
self._build_templates_from_parent(root_id_str, id_template_map, templates_json)
1041+
self._update_changes(root_id_str, id_template_map, templates_json)
1042+
1043+
1044+
def _build_template_data(guid, templates_data, templates, template_guid_id_map):
1045+
curr_template = None
1046+
for template in templates:
1047+
if template.guid == guid:
1048+
curr_template = template
1049+
1050+
fields = ["name", "report_type", "date", "tags", "item_filter"]
1051+
curr_template_key = f"Template_{template_guid_id_map[curr_template.guid]}"
1052+
templates_data[curr_template_key] = {}
1053+
for field in fields:
1054+
value = getattr(curr_template, field, None)
1055+
if value is None:
1056+
continue
1057+
templates_data[curr_template_key][field] = value
1058+
1059+
templates_data[curr_template_key]["params"] = curr_template.get_params()
1060+
templates_data[curr_template_key]["sort_selection"] = curr_template.get_sort_selection()
1061+
if curr_template.parent is None:
1062+
templates_data[curr_template_key]["parent"] = None
1063+
else:
1064+
templates_data[curr_template_key][
1065+
"parent"
1066+
] = f"Template_{template_guid_id_map[curr_template.parent]}"
1067+
1068+
templates_data[curr_template_key]["children"] = []
1069+
children_guids = curr_template.children
1070+
for child_guid in children_guids:
1071+
curr_size = len(template_guid_id_map)
1072+
template_guid_id_map[child_guid] = curr_size
1073+
templates_data[curr_template_key]["children"].append(f"Template_{curr_size}")
1074+
1075+
if not children_guids:
1076+
return
1077+
for child_guid in children_guids:
1078+
_build_template_data(child_guid, templates_data, templates, template_guid_id_map)
1079+
9751080

9761081
def create_new_local_database(
9771082
parent,

tests/test_report_remote_server.py

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,3 +345,130 @@ def test_acls_start(request, get_exec) -> bool:
345345
succ_three = not r.launch_local_database_server(parent=None, directory=db_dir, acls=True)
346346
r.delete_database(db_dir=db_dir)
347347
assert succ and succ_two and succ_three
348+
349+
350+
@pytest.mark.ado_test
351+
def test_get_templates_as_json(adr_service_create) -> bool:
352+
server = adr_service_create.serverobj
353+
354+
# Level 0
355+
template_01 = server.create_template(name="A", parent=None, report_type="Layout:basic")
356+
server.put_objects(template_01)
357+
358+
# Level 1
359+
template_02 = server.create_template(name="B", parent=template_01, report_type="Layout:basic")
360+
template_04 = server.create_template(name="C", parent=template_01, report_type="Layout:basic")
361+
server.put_objects([template_02, template_04])
362+
363+
# Level 2
364+
template_03 = server.create_template(name="D", parent=template_02, report_type="Layout:basic")
365+
server.put_objects(template_03)
366+
367+
# Updates the reports with change in children
368+
server.put_objects(template_02)
369+
server.put_objects(template_01)
370+
371+
templates = server.get_objects(objtype=ro.TemplateREST)
372+
for template in templates:
373+
if template.master:
374+
root_guid = template.guid
375+
break
376+
377+
templates_json = server.get_templates_as_json(root_guid)
378+
assert len(templates_json) == 4
379+
assert templates_json["Template_0"]["name"] == "A"
380+
assert templates_json["Template_0"]["report_type"] == "Layout:basic"
381+
assert templates_json["Template_0"]["tags"] == ""
382+
assert templates_json["Template_0"]["params"] == {}
383+
assert templates_json["Template_0"]["sort_selection"] == ""
384+
assert templates_json["Template_0"]["item_filter"] == ""
385+
assert templates_json["Template_0"]["parent"] is None
386+
assert templates_json["Template_0"]["children"] == ["Template_1", "Template_2"]
387+
server.del_objects(templates)
388+
389+
390+
@pytest.mark.ado_test
391+
def test_load_templates(adr_service_create) -> bool:
392+
server = adr_service_create.serverobj
393+
templates_json = {
394+
"Template_0": {
395+
"name": "A",
396+
"report_type": "Layout:basic",
397+
"date": "2024-12-17T08:40:49.175728-05:00",
398+
"tags": "",
399+
"params": {},
400+
"property": {},
401+
"sort_fields": [],
402+
"sort_selection": "",
403+
"item_filter": "",
404+
"filter_mode": "items",
405+
"parent": None,
406+
"children": ["Template_1", "Template_2"],
407+
},
408+
"Template_1": {
409+
"name": "B",
410+
"report_type": "Layout:basic",
411+
"date": "2024-12-17T08:40:49.413270-05:00",
412+
"tags": "",
413+
"params": {},
414+
"property": {},
415+
"sort_fields": [],
416+
"sort_selection": "",
417+
"item_filter": "",
418+
"filter_mode": "items",
419+
"parent": "Template_0",
420+
"children": ["Template_3"],
421+
},
422+
"Template_3": {
423+
"name": "D",
424+
"report_type": "Layout:basic",
425+
"date": "2024-12-17T08:40:49.876721-05:00",
426+
"tags": "",
427+
"params": {},
428+
"property": {},
429+
"sort_fields": [],
430+
"sort_selection": "",
431+
"item_filter": "",
432+
"filter_mode": "items",
433+
"parent": "Template_1",
434+
"children": [],
435+
},
436+
"Template_2": {
437+
"name": "C",
438+
"report_type": "Layout:basic",
439+
"date": "2024-12-17T08:40:49.413270-05:00",
440+
"tags": "",
441+
"params": {},
442+
"property": {},
443+
"sort_fields": [],
444+
"sort_selection": "",
445+
"item_filter": "",
446+
"filter_mode": "items",
447+
"parent": "Template_0",
448+
"children": [],
449+
},
450+
}
451+
server.load_templates(templates_json)
452+
templates = server.get_objects(objtype=ro.TemplateREST)
453+
assert len(templates) == 4
454+
455+
template_guid_map = {}
456+
for template in templates:
457+
template_guid_map[template.guid] = template.name
458+
459+
for template in templates:
460+
if template.name == "A":
461+
assert template.report_type == "Layout:basic"
462+
assert template.tags == ""
463+
assert template.get_params() == {}
464+
assert template.get_property() == {}
465+
assert template.get_sort_fields() == []
466+
assert template.get_sort_selection() == ""
467+
assert template.item_filter == ""
468+
assert template.get_filter_mode() == "items"
469+
assert template.parent is None
470+
children = []
471+
for child in template.children:
472+
children.append(template_guid_map[child])
473+
assert children == ["B", "C"]
474+
break

0 commit comments

Comments
 (0)