Skip to content

Commit 5ebde43

Browse files
committed
Fix when using different enum than original
1 parent a59f7ec commit 5ebde43

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

infrahub_sdk/template/filters.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,36 @@ def value_to_enum_name(value: Any, enum_path: str | None = None) -> str:
1717
This filter takes a raw value and converts it to the corresponding enum member name by dynamically importing the
1818
enum class.
1919
20-
For example, `{{ decision__value | value_to_enum_name("infrahub.permissions.constants.PermissionDecisionFlag") }}`
20+
For example, `{{ decision__value | value_to_enum_name("infrahub.core.constants.PermissionDecision") }}`
2121
will return: `"ALLOW_ALL"` for value `6`.
2222
"""
23-
if isinstance(value, Enum):
23+
if isinstance(value, Enum) and not enum_path:
2424
return value.name
2525

26+
raw_value = value.value if isinstance(value, Enum) else value
27+
2628
if not enum_path:
27-
return str(value)
29+
return str(raw_value)
2830

2931
try:
3032
module_path, class_name = enum_path.rsplit(".", 1)
3133
module = import_module(module_path)
3234
enum_type = getattr(module, class_name)
3335
except (ValueError, ImportError, AttributeError):
34-
return str(value)
36+
return str(raw_value)
3537

3638
# Verify that we have a class and that this class is a valid Enum
3739
if not (isinstance(enum_type, type) and issubclass(enum_type, Enum)):
38-
return str(value)
40+
return str(raw_value)
3941

4042
try:
41-
enum_member = enum_type(value)
43+
enum_member = enum_type(raw_value)
4244
if enum_member.name is not None:
4345
return enum_member.name
4446
except (ValueError, TypeError):
4547
pass
4648

47-
return str(value)
49+
return str(raw_value)
4850

4951

5052
INFRAHUB_FILTERS = {"value_to_enum_name": value_to_enum_name}

0 commit comments

Comments
 (0)