Skip to content

Commit d90e526

Browse files
zhang-yuanruiCopilotviseshrp
authored
Support reordering a child template in the Serverless ADR (#377)
This PR is mainly to support reordering a child template in serverless ADR. It also moves 2 customized exceptions to a common file for both server and serverless. --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Visesh Rajendraprasad <viseshrp@users.noreply.github.com>
1 parent be56a71 commit d90e526

File tree

5 files changed

+95
-11
lines changed

5 files changed

+95
-11
lines changed

src/ansys/dynamicreporting/core/exceptions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ class MissingReportError(ADRException):
8686
detail = "Can not find the corresponding report."
8787

8888

89+
class TemplateDoesNotExist(ADRException):
90+
"""Raised when the template is not found"""
91+
92+
93+
class TemplateReorderOutOfBounds(ADRException):
94+
"""Raised when a template is reordered to be out of the size of the children"""
95+
96+
8997
"""Serverless exceptions."""
9098

9199

src/ansys/dynamicreporting/core/serverless/template.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
from datetime import datetime
33
import json
44
import os
5-
from typing import Optional, Tuple
65
import uuid
76

87
from django.template.loader import render_to_string
98
from django.utils import timezone
109

1110
from ..constants import JSON_ATTR_KEYS
12-
from ..exceptions import ADRException
11+
from ..exceptions import ADRException, TemplateDoesNotExist, TemplateReorderOutOfBounds
1312
from .base import BaseModel, StrEnum
1413

1514

@@ -408,6 +407,40 @@ def to_json(self, filename: str) -> None:
408407
# Make the file read-only
409408
os.chmod(filename, 0o444)
410409

410+
def reorder_child(self, target_child_template: "Template", new_position: int) -> None:
411+
"""
412+
Reorder the template.guid in parent.children to the specified position.
413+
414+
Parameters
415+
----------
416+
target_child_template : str | TemplateREST
417+
The child template to reorder. This can be either the GUID of the template (as a string)
418+
or a TemplateREST object.
419+
new_position : int
420+
The new position in the parent's children list where the template should be placed.
421+
422+
Raises
423+
------
424+
TemplateReorderOutOfBound
425+
If the specified position is out of bounds.
426+
TemplateDoesNotExist
427+
If the target_child_template is not found in the parent's children list.
428+
"""
429+
children_size = len(self.children)
430+
if new_position < 0 or new_position >= children_size:
431+
raise TemplateReorderOutOfBounds(
432+
f"The specified position {new_position} is out of bounds. "
433+
f"Valid range: [0, {len(self.children)})"
434+
)
435+
436+
target_guid = target_child_template.guid
437+
if target_child_template not in self.children:
438+
raise TemplateDoesNotExist(
439+
f"Template with GUID '{target_guid}' is not found in the parent's children list."
440+
)
441+
self.children.remove(target_child_template)
442+
self.children.insert(new_position, target_child_template)
443+
411444

412445
class Layout(Template):
413446
def get_column_count(self):

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
import dateutil.parser
2121
import pytz
2222

23-
from . import exceptions, extremely_ugly_hacks, report_utils
23+
from . import extremely_ugly_hacks, report_utils
24+
from ..exceptions import TemplateDoesNotExist, TemplateReorderOutOfBounds
2425
from .encoders import PayloaddataEncoder
2526

2627
try:
@@ -1639,14 +1640,14 @@ def reorder_child(self, target_child_template: str | TemplateREST, new_position:
16391640
16401641
Raises
16411642
------
1642-
TemplateReorderOutOfBoundError
1643+
TemplateReorderOutOfBound
16431644
If the specified position is out of bounds.
1644-
ValueError
1645+
TemplateDoesNotExist
16451646
If the target_child_template is not found in the parent's children list.
16461647
"""
16471648
children_size = len(self.children)
16481649
if new_position < 0 or new_position >= children_size:
1649-
raise exceptions.TemplateReorderOutOfBounds(
1650+
raise TemplateReorderOutOfBounds(
16501651
f"The specified position {new_position} is out of bounds. "
16511652
f"Valid range: [0, {len(self.children)})"
16521653
)
@@ -1658,7 +1659,7 @@ def reorder_child(self, target_child_template: str | TemplateREST, new_position:
16581659
)
16591660

16601661
if target_guid not in self.children:
1661-
raise exceptions.TemplateDoesNotExist(
1662+
raise TemplateDoesNotExist(
16621663
f"Template with GUID '{target_guid}' is not found in the parent's children list."
16631664
)
16641665
self.children.remove(target_guid)

tests/serverless/test_template.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
import pytest
44

5-
from ansys.dynamicreporting.core.exceptions import ADRException
5+
from ansys.dynamicreporting.core.exceptions import (
6+
ADRException,
7+
TemplateDoesNotExist,
8+
TemplateReorderOutOfBounds,
9+
)
610

711

812
@pytest.mark.ado_test
@@ -1115,3 +1119,40 @@ def test_to_json_non_root_template(adr_serverless):
11151119
# Attempt to call to_json on the child template and expect an ADRException
11161120
with pytest.raises(ADRException, match="Only root templates can be dumped to JSON files."):
11171121
child_template.to_json("dummy_path.json")
1122+
1123+
1124+
@pytest.mark.ado_test
1125+
def test_template_reorder_child(adr_serverless):
1126+
from ansys.dynamicreporting.core.serverless import PanelLayout
1127+
1128+
# Create a parent template
1129+
parent_template = PanelLayout.create(name="ParentTemplate")
1130+
1131+
# Create child templates
1132+
child1 = PanelLayout.create(name="Child1", parent=parent_template)
1133+
child2 = PanelLayout.create(name="Child2", parent=parent_template)
1134+
child3 = PanelLayout.create(name="Child3", parent=parent_template)
1135+
1136+
# Add children to the parent template
1137+
parent_template.children = [child1, child2, child3]
1138+
1139+
# Reorder child2 to the first position
1140+
parent_template.reorder_child(child2, 0)
1141+
assert [child.name for child in parent_template.children] == ["Child2", "Child1", "Child3"]
1142+
1143+
# Reorder child3 to the second position
1144+
parent_template.reorder_child(child3, 1)
1145+
assert [child.name for child in parent_template.children] == ["Child2", "Child3", "Child1"]
1146+
1147+
# Reorder child1 to the last position
1148+
parent_template.reorder_child(child1, 2)
1149+
assert [child.name for child in parent_template.children] == ["Child2", "Child3", "Child1"]
1150+
1151+
# Test invalid position (out of bounds)
1152+
with pytest.raises(TemplateReorderOutOfBounds):
1153+
parent_template.reorder_child(child1, 5)
1154+
1155+
# Test invalid child (not in children)
1156+
invalid_child = PanelLayout.create(name="InvalidChild")
1157+
with pytest.raises(TemplateDoesNotExist):
1158+
parent_template.reorder_child(invalid_child, 1)

tests/test_report_objects.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import pytest
66

7+
from ansys.dynamicreporting.core.exceptions import TemplateDoesNotExist, TemplateReorderOutOfBounds
78
from ansys.dynamicreporting.core.utils import report_objects as ro
89

910

@@ -393,7 +394,7 @@ def test_templaterest_fields() -> None:
393394

394395

395396
@pytest.mark.ado_test
396-
def test_templaterest_set_order() -> None:
397+
def test_templaterest_reorder_child() -> None:
397398
# Create a TemplateREST object
398399
template = ro.TemplateREST()
399400
template.children = ["guid1", "guid2", "guid3"]
@@ -415,13 +416,13 @@ def test_templaterest_set_order() -> None:
415416
# Test invalid position (out of bounds)
416417
try:
417418
template.reorder_child("guid1", 5)
418-
except ro.exceptions.TemplateReorderOutOfBounds as e:
419+
except TemplateReorderOutOfBounds as e:
419420
assert "out of bounds" in str(e)
420421

421422
# Test invalid GUID (not in children)
422423
try:
423424
template.reorder_child("invalid_guid", 1)
424-
except ro.exceptions.TemplateDoesNotExist as e:
425+
except TemplateDoesNotExist as e:
425426
assert "not found in the parent's children list" in str(e)
426427

427428

0 commit comments

Comments
 (0)