Skip to content

Commit b7ec8a4

Browse files
Reorder a template among its siblings in the Server ADR (#367)
User story in ADO: https://tfs.ansys.com:8443/tfs/ANSYS_Development/Portfolio/_boards/board/t/Nexus/Stories/?workitem=1234450 This PR fulfills the functionality of getting and setting orders of a target template in the server object level. Higher level wrapper in `Report` can be in another PR after this PR is approved. Testing is missing for now in case there will be some design changes. --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 59aac63 commit b7ec8a4

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ class TemplateEditorJSONLoadingError(RuntimeError):
5757
"""Raised for errors when loading a JSON file for the template editor"""
5858

5959

60+
class TemplateDoesNotExist(RuntimeError):
61+
"""Raised when the template is not found"""
62+
63+
64+
class TemplateReorderOutOfBounds(RuntimeError):
65+
"""Raised when a template is reordered to be out of the size of the children"""
66+
67+
6068
def raise_bad_request_error(response):
6169
"""
6270
Generates bad request error message and raises an exception.

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

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from ast import literal_eval
24
import base64
35
import copy
@@ -14,12 +16,11 @@
1416
import uuid
1517
import weakref
1618

17-
from PIL import Image
1819
import dateutil
1920
import dateutil.parser
2021
import pytz
2122

22-
from . import extremely_ugly_hacks, report_utils
23+
from . import exceptions, extremely_ugly_hacks, report_utils
2324
from .encoders import PayloaddataEncoder
2425

2526
try:
@@ -1624,6 +1625,45 @@ def get_html(self):
16241625
else:
16251626
raise ValueError(f"Error: HTML not supported on the report type {self.report_type}")
16261627

1628+
def reorder_child(self, target_child_template: str | TemplateREST, new_position: int) -> None:
1629+
"""
1630+
Reorder the template.guid in parent.children to the specified position.
1631+
1632+
Parameters
1633+
----------
1634+
target_child_template : str | TemplateREST
1635+
The child template to reorder. This can be either the GUID of the template (as a string)
1636+
or a TemplateREST object.
1637+
new_position : int
1638+
The new position in the parent's children list where the template should be placed.
1639+
1640+
Raises
1641+
------
1642+
TemplateReorderOutOfBoundError
1643+
If the specified position is out of bounds.
1644+
ValueError
1645+
If the target_child_template is not found in the parent's children list.
1646+
"""
1647+
children_size = len(self.children)
1648+
if new_position < 0 or new_position >= children_size:
1649+
raise exceptions.TemplateReorderOutOfBounds(
1650+
f"The specified position {new_position} is out of bounds. "
1651+
f"Valid range: [0, {len(self.children)})"
1652+
)
1653+
1654+
target_guid = (
1655+
target_child_template
1656+
if isinstance(target_child_template, str)
1657+
else target_child_template.guid
1658+
)
1659+
1660+
if target_guid not in self.children:
1661+
raise exceptions.TemplateDoesNotExist(
1662+
f"Template with GUID '{target_guid}' is not found in the parent's children list."
1663+
)
1664+
self.children.remove(target_guid)
1665+
self.children.insert(new_position, target_guid)
1666+
16271667

16281668
class LayoutREST(TemplateREST):
16291669
"""Representation of the common Layout Template."""

tests/test_report_objects.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,39 @@ def test_templaterest_fields() -> None:
392392
assert succ_a and succ_four and succ_five
393393

394394

395+
@pytest.mark.ado_test
396+
def test_templaterest_set_order() -> None:
397+
# Create a TemplateREST object
398+
template = ro.TemplateREST()
399+
template.children = ["guid1", "guid2", "guid3"]
400+
401+
# Test reordering a child template to a valid position
402+
template.reorder_child("guid2", 0)
403+
assert template.children == ["guid2", "guid1", "guid3"]
404+
405+
# Test reordering a child template to another valid position
406+
template.reorder_child("guid2", 2)
407+
assert template.children == ["guid1", "guid3", "guid2"]
408+
409+
# Test reordering using a TemplateREST object instead of a string
410+
child_template = ro.TemplateREST()
411+
child_template.guid = "guid3"
412+
template.reorder_child(child_template, 0)
413+
assert template.children == ["guid3", "guid1", "guid2"]
414+
415+
# Test invalid position (out of bounds)
416+
try:
417+
template.reorder_child("guid1", 5)
418+
except ro.exceptions.TemplateReorderOutOfBounds as e:
419+
assert "out of bounds" in str(e)
420+
421+
# Test invalid GUID (not in children)
422+
try:
423+
template.reorder_child("invalid_guid", 1)
424+
except ro.exceptions.TemplateDoesNotExist as e:
425+
assert "not found in the parent's children list" in str(e)
426+
427+
395428
@pytest.mark.ado_test
396429
def test_templaterest_filter() -> None:
397430
a = ro.TemplateREST()

0 commit comments

Comments
 (0)