diff --git a/src/ansys/dynamicreporting/core/utils/report_remote_server.py b/src/ansys/dynamicreporting/core/utils/report_remote_server.py index 8571b739c..6d74dc673 100755 --- a/src/ansys/dynamicreporting/core/utils/report_remote_server.py +++ b/src/ansys/dynamicreporting/core/utils/report_remote_server.py @@ -972,6 +972,111 @@ def get_pptx_from_report(self, report_guid, directory_name=None, query=None): else: raise Exception(f"The server returned an error code {resp.status_code}") + def get_templates_as_json(self, root_guid): + """ + Convert report templates rooted as root_guid to JSON + Return a python dictionary. + """ + templates_data = {} + templates = self.get_objects(objtype=report_objects.TemplateREST) + template_guid_id_map = {root_guid: 0} + _build_template_data(root_guid, templates_data, templates, template_guid_id_map) + return templates_data + + def _populate_template(self, attr, parent_template): + template = self.create_template( + name=attr["name"], parent=parent_template, report_type=attr["report_type"] + ) + template.set_params(attr["params"]) + if attr["sort_selection"] != "": + template.set_sort_selection(value=attr["sort_selection"]) + template.set_tags(attr["tags"]) + template.set_filter(filter_str=attr["item_filter"]) + + return template + + def _update_changes(self, id_str, id_template_map, templates_json): + children_id_strs = templates_json[id_str]["children"] + if not children_id_strs: + return + + for child_id_str in children_id_strs: + self._update_changes(child_id_str, id_template_map, templates_json) + self.put_objects(id_template_map[id_str]) + + def _build_templates_from_parent(self, id_str, id_template_map, templates_json): + children_id_strs = templates_json[id_str]["children"] + if not children_id_strs: + return + + child_templates = [] + for child_id_str in children_id_strs: + child_attr = templates_json[child_id_str] + child_template = self._populate_template(child_attr, id_template_map[id_str]) + child_templates.append(child_template) + id_template_map[child_id_str] = child_template + + self.put_objects(child_templates) + + i = 0 + for child_id_str in children_id_strs: + self._build_templates_from_parent(child_id_str, id_template_map, templates_json) + i += 1 + + def load_templates(self, templates_json): + """ + Load templates given a json-format data + """ + for template_id_str, template_attr in templates_json.items(): + if template_attr["parent"] is None: + root_id_str = template_id_str + break + + root_attr = templates_json[root_id_str] + root_template = self._populate_template(root_attr, None) + self.put_objects(root_template) + id_template_map = {} + id_template_map[root_id_str] = root_template + self._build_templates_from_parent(root_id_str, id_template_map, templates_json) + self._update_changes(root_id_str, id_template_map, templates_json) + + +def _build_template_data(guid, templates_data, templates, template_guid_id_map): + curr_template = None + for template in templates: + if template.guid == guid: + curr_template = template + + fields = ["name", "report_type", "date", "tags", "item_filter"] + curr_template_key = f"Template_{template_guid_id_map[curr_template.guid]}" + templates_data[curr_template_key] = {} + for field in fields: + value = getattr(curr_template, field, None) + if value is None: + continue + templates_data[curr_template_key][field] = value + + templates_data[curr_template_key]["params"] = curr_template.get_params() + templates_data[curr_template_key]["sort_selection"] = curr_template.get_sort_selection() + if curr_template.parent is None: + templates_data[curr_template_key]["parent"] = None + else: + templates_data[curr_template_key][ + "parent" + ] = f"Template_{template_guid_id_map[curr_template.parent]}" + + templates_data[curr_template_key]["children"] = [] + children_guids = curr_template.children + for child_guid in children_guids: + curr_size = len(template_guid_id_map) + template_guid_id_map[child_guid] = curr_size + templates_data[curr_template_key]["children"].append(f"Template_{curr_size}") + + if not children_guids: + return + for child_guid in children_guids: + _build_template_data(child_guid, templates_data, templates, template_guid_id_map) + def create_new_local_database( parent, diff --git a/tests/test_report_remote_server.py b/tests/test_report_remote_server.py index dbc08f045..e508f7b34 100755 --- a/tests/test_report_remote_server.py +++ b/tests/test_report_remote_server.py @@ -345,3 +345,130 @@ def test_acls_start(request, get_exec) -> bool: succ_three = not r.launch_local_database_server(parent=None, directory=db_dir, acls=True) r.delete_database(db_dir=db_dir) assert succ and succ_two and succ_three + + +@pytest.mark.ado_test +def test_get_templates_as_json(adr_service_create) -> bool: + server = adr_service_create.serverobj + + # Level 0 + template_01 = server.create_template(name="A", parent=None, report_type="Layout:basic") + server.put_objects(template_01) + + # Level 1 + template_02 = server.create_template(name="B", parent=template_01, report_type="Layout:basic") + template_04 = server.create_template(name="C", parent=template_01, report_type="Layout:basic") + server.put_objects([template_02, template_04]) + + # Level 2 + template_03 = server.create_template(name="D", parent=template_02, report_type="Layout:basic") + server.put_objects(template_03) + + # Updates the reports with change in children + server.put_objects(template_02) + server.put_objects(template_01) + + templates = server.get_objects(objtype=ro.TemplateREST) + for template in templates: + if template.master: + root_guid = template.guid + break + + templates_json = server.get_templates_as_json(root_guid) + assert len(templates_json) == 4 + assert templates_json["Template_0"]["name"] == "A" + assert templates_json["Template_0"]["report_type"] == "Layout:basic" + assert templates_json["Template_0"]["tags"] == "" + assert templates_json["Template_0"]["params"] == {} + assert templates_json["Template_0"]["sort_selection"] == "" + assert templates_json["Template_0"]["item_filter"] == "" + assert templates_json["Template_0"]["parent"] is None + assert templates_json["Template_0"]["children"] == ["Template_1", "Template_2"] + server.del_objects(templates) + + +@pytest.mark.ado_test +def test_load_templates(adr_service_create) -> bool: + server = adr_service_create.serverobj + templates_json = { + "Template_0": { + "name": "A", + "report_type": "Layout:basic", + "date": "2024-12-17T08:40:49.175728-05:00", + "tags": "", + "params": {}, + "property": {}, + "sort_fields": [], + "sort_selection": "", + "item_filter": "", + "filter_mode": "items", + "parent": None, + "children": ["Template_1", "Template_2"], + }, + "Template_1": { + "name": "B", + "report_type": "Layout:basic", + "date": "2024-12-17T08:40:49.413270-05:00", + "tags": "", + "params": {}, + "property": {}, + "sort_fields": [], + "sort_selection": "", + "item_filter": "", + "filter_mode": "items", + "parent": "Template_0", + "children": ["Template_3"], + }, + "Template_3": { + "name": "D", + "report_type": "Layout:basic", + "date": "2024-12-17T08:40:49.876721-05:00", + "tags": "", + "params": {}, + "property": {}, + "sort_fields": [], + "sort_selection": "", + "item_filter": "", + "filter_mode": "items", + "parent": "Template_1", + "children": [], + }, + "Template_2": { + "name": "C", + "report_type": "Layout:basic", + "date": "2024-12-17T08:40:49.413270-05:00", + "tags": "", + "params": {}, + "property": {}, + "sort_fields": [], + "sort_selection": "", + "item_filter": "", + "filter_mode": "items", + "parent": "Template_0", + "children": [], + }, + } + server.load_templates(templates_json) + templates = server.get_objects(objtype=ro.TemplateREST) + assert len(templates) == 4 + + template_guid_map = {} + for template in templates: + template_guid_map[template.guid] = template.name + + for template in templates: + if template.name == "A": + assert template.report_type == "Layout:basic" + assert template.tags == "" + assert template.get_params() == {} + assert template.get_property() == {} + assert template.get_sort_fields() == [] + assert template.get_sort_selection() == "" + assert template.item_filter == "" + assert template.get_filter_mode() == "items" + assert template.parent is None + children = [] + for child in template.children: + children.append(template_guid_map[child]) + assert children == ["B", "C"] + break