Allow backup_value to be specified to the 'getkey' report helper (#8719)

This commit is contained in:
Oliver authored and GitHub committed 2024-12-19 09:24:01 +11:00
1 parent 17fd7f32a4
commit 53792693f5
1 file changed
+3 -4
@@ -103,7 +103,7 @@ def getindex(container: list, index: int) -> Any:
@register.simple_tag() @register.simple_tag()
def getkey(container: dict, key: str) -> Any: def getkey(container: dict, key: str, backup_value: Optional[any] = None) -> Any:
"""Perform key lookup in the provided dict object. """Perform key lookup in the provided dict object.
This function is provided to get around template rendering limitations. This function is provided to get around template rendering limitations.
@@ -112,14 +112,13 @@ def getkey(container: dict, key: str) -> Any:
Arguments: Arguments:
container: A python dict object container: A python dict object
key: The 'key' to be found within the dict key: The 'key' to be found within the dict
backup_value: A backup value to return if the key is not found
""" """
if type(container) is not dict: if type(container) is not dict:
logger.warning('getkey() called with non-dict object') logger.warning('getkey() called with non-dict object')
return None return None
if key in container: return container.get(key, backup_value)
return container[key]
return None
@register.simple_tag() @register.simple_tag()