Skip to content

Commit aed3d1c

Browse files
authored
Merge pull request #4413 from opsmill/dga-20240921-fix-4382
Fix rendering of display label for attribute with enum
2 parents 15e92ab + acb6d82 commit aed3d1c

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

backend/infrahub/core/node/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
from enum import Enum
34
from typing import TYPE_CHECKING, Any, Optional, Union
45

56
from infrahub_sdk import UUIDT
@@ -590,7 +591,11 @@ async def render_display_label(self, db: Optional[InfrahubDatabase] = None) -> s
590591
raise ValidationError("Only Attribute can be used in Display Label")
591592

592593
attr = getattr(self, item_elements[0])
593-
display_elements.append(getattr(attr, item_elements[1]))
594+
attr_value = getattr(attr, item_elements[1])
595+
if isinstance(attr_value, Enum):
596+
display_elements.append(attr_value.value)
597+
else:
598+
display_elements.append(attr_value)
594599

595600
if not display_elements or all(de is None for de in display_elements):
596601
return ""

backend/tests/unit/core/test_node.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ async def test_render_display_label(db: InfrahubDatabase, default_branch: Branch
196196
{"name": "firstname", "kind": "Text"},
197197
{"name": "lastname", "kind": "Text"},
198198
{"name": "age", "kind": "Number"},
199+
{"name": "color", "kind": "Text", "enum": ["blue", "red"], "default_value": "red"},
200+
{"name": "height", "kind": "Number", "enum": [170, 180], "default_value": 170},
199201
],
200202
}
201203

@@ -224,6 +226,24 @@ async def test_render_display_label(db: InfrahubDatabase, default_branch: Branch
224226
await obj.new(db=db, firstname="John", lastname="Doe", age=99)
225227
assert await obj.render_display_label(db=db) == f"TestDisplay(ID: {obj.id})[NEW]"
226228

229+
# Display Labels with an ENUM String
230+
schema_01["display_labels"] = ["firstname__value", "color__value"]
231+
node_schema = NodeSchema(**schema_01)
232+
registry.schema.set(name=node_schema.kind, schema=node_schema)
233+
234+
obj = await Node.init(db=db, schema=node_schema)
235+
await obj.new(db=db, firstname="John", lastname="Doe", age=99, color="red")
236+
assert await obj.render_display_label(db=db) == "John red"
237+
238+
# Display Labels with an ENUM Number
239+
schema_01["display_labels"] = ["firstname__value", "height__value"]
240+
node_schema = NodeSchema(**schema_01)
241+
registry.schema.set(name=node_schema.kind, schema=node_schema)
242+
243+
obj = await Node.init(db=db, schema=node_schema)
244+
await obj.new(db=db, firstname="John", lastname="Doe", age=99, height=180)
245+
assert await obj.render_display_label(db=db) == "John 180"
246+
227247

228248
async def test_get_hfid(db: InfrahubDatabase, default_branch, animal_person_schema):
229249
person_schema = animal_person_schema.get(name="TestPerson")

changelog/4382.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Display label composed of an attribute of type Enum will now render correctly

0 commit comments

Comments
 (0)