mirror of
https://github.com/inventree/InvenTree.git
synced 2026-04-15 07:48:51 +00:00
Update parameter report helper (#11690)
* Update parameter report helper - Fallback to case insensitive lookup * Add default value in case parameter is not found * Add new report helper func
This commit is contained in:
@@ -473,10 +473,10 @@ def parameter(
|
||||
|
||||
Arguments:
|
||||
instance: A Model object
|
||||
parameter_name: The name of the parameter to retrieve
|
||||
parameter_name: The name of the parameter to retrieve (case insensitive)
|
||||
|
||||
Returns:
|
||||
A Parameter object, or None if not found
|
||||
A Parameter object, or the provided default value if not found
|
||||
"""
|
||||
if instance is None:
|
||||
raise ValueError('parameter tag requires a valid Model instance')
|
||||
@@ -484,12 +484,46 @@ def parameter(
|
||||
if not isinstance(instance, Model) or not hasattr(instance, 'parameters'):
|
||||
raise TypeError("parameter tag requires a Model with 'parameters' attribute")
|
||||
|
||||
return (
|
||||
instance.parameters
|
||||
# First try with exact match
|
||||
if (
|
||||
parameter := instance.parameters
|
||||
.prefetch_related('template')
|
||||
.filter(template__name=parameter_name)
|
||||
.first()
|
||||
)
|
||||
):
|
||||
return parameter
|
||||
|
||||
# Next, try with case-insensitive match
|
||||
if (
|
||||
parameter := instance.parameters
|
||||
.prefetch_related('template')
|
||||
.filter(template__name__iexact=parameter_name)
|
||||
.first()
|
||||
):
|
||||
return parameter
|
||||
|
||||
return None
|
||||
|
||||
|
||||
@register.simple_tag()
|
||||
def parameter_value(
|
||||
instance: Model, parameter_name: str, backup_value: Optional[Any] = None
|
||||
) -> str:
|
||||
"""Return the value of a Parameter for the given part and parameter name.
|
||||
|
||||
Arguments:
|
||||
instance: A Model object
|
||||
parameter_name: The name of the parameter to retrieve (case insensitive)
|
||||
backup_value: A backup value to return if the parameter is not found
|
||||
|
||||
Returns:
|
||||
The value of the Parameter, or the backup_value if not found
|
||||
"""
|
||||
if param := parameter(instance, parameter_name):
|
||||
return param.data
|
||||
|
||||
# If the matching parameter is not found, return the backup value
|
||||
return backup_value
|
||||
|
||||
|
||||
@register.simple_tag()
|
||||
|
||||
Reference in New Issue
Block a user