From a5ee59dddcc5ef38ba1525618d5eb7fce278fe55 Mon Sep 17 00:00:00 2001 From: Visesh Rajendraprasad Date: Thu, 6 Nov 2025 16:06:52 -0500 Subject: [PATCH] fix copies of template subtrees (#441) Co-authored-by: viseshrp <11642379+viseshrp@users.noreply.github.com> --- .../dynamicreporting/core/serverless/adr.py | 2 ++ .../core/serverless/template.py | 14 ++++++++++--- tests/serverless/test_template.py | 21 +++++++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/ansys/dynamicreporting/core/serverless/adr.py b/src/ansys/dynamicreporting/core/serverless/adr.py index 1517a4c4a..2f870ab55 100644 --- a/src/ansys/dynamicreporting/core/serverless/adr.py +++ b/src/ansys/dynamicreporting/core/serverless/adr.py @@ -1101,6 +1101,8 @@ def _copy_template(self, template: Template, **kwargs) -> Template: new_child = self._copy_template(child, **kwargs) new_children.append(new_child) out_template.children = new_children + out_template.update_children_order() + out_template.save(**kwargs) return out_template def copy_objects( diff --git a/src/ansys/dynamicreporting/core/serverless/template.py b/src/ansys/dynamicreporting/core/serverless/template.py index 53fffb2fe..d9cf15f8b 100644 --- a/src/ansys/dynamicreporting/core/serverless/template.py +++ b/src/ansys/dynamicreporting/core/serverless/template.py @@ -135,7 +135,6 @@ def save(self, **kwargs): raise self.parent.__class__.NotSaved( extra_detail="Failed to save template because its parent is not saved" ) - children_order = [] for child in self.children: if not isinstance(child, Template): raise TypeError( @@ -145,8 +144,7 @@ def save(self, **kwargs): raise child.__class__.NotSaved( extra_detail="Failed to save template because its children are not saved" ) - children_order.append(child.guid) - self._children_order = ",".join(children_order) + self.update_children_order() self._master = self.parent is None # set properties prop_dict = {} @@ -221,7 +219,17 @@ def find(cls, query="", **kwargs): query_string = f"A|t_types|cont|{cls.report_type};{query}" # noqa: E702 return super().find(query=query_string, **kwargs) + def update_children_order(self) -> None: + """ + Update the children_order string based on the current order of the children list. + """ + children_guids = [str(child.guid) for child in self.children] + self._children_order = ",".join(children_guids) + def reorder_children(self) -> None: + """ + Reorder the children list based on the children_order string. + """ guid_to_child = {child.guid: child for child in self.children} sorted_guids = self.children_order.lower().split(",") # return the children based on the order of guids in children_order diff --git a/tests/serverless/test_template.py b/tests/serverless/test_template.py index 833dbaef6..6f1981b72 100644 --- a/tests/serverless/test_template.py +++ b/tests/serverless/test_template.py @@ -677,6 +677,27 @@ def test_template_reorder_children(adr_serverless): assert [child.name for child in parent.children] == ["child2", "child3", "child1"] +@pytest.mark.ado_test +def test_template_update_children_order(adr_serverless): + from ansys.dynamicreporting.core.serverless import BasicLayout, PanelLayout + + # Create parent template + parent = PanelLayout.create(name="test_template_update_children_order", tags="dp=dp227") + + # Create child templates + child1 = BasicLayout.create(name="child1", parent=parent) + child2 = BasicLayout.create(name="child2", parent=parent) + child3 = BasicLayout.create(name="child3", parent=parent) + + # Manually set the parent's children + parent.children = [child1, child2, child3] + parent._children_order = "" # Clear order + parent.update_children_order() + + # check order + assert parent.children_order == f"{child1.guid},{child2.guid},{child3.guid}" + + @pytest.mark.ado_test def test_layout_set_get_column_count(adr_serverless): from ansys.dynamicreporting.core.serverless import PanelLayout