From 4d2ed8fcba368ded19258530cd4887f6c9c03fb9 Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 8 Apr 2026 14:14:44 +1000 Subject: [PATCH] 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 --- docs/docs/report/helpers.md | 29 +++++++++++- .../InvenTree/report/templatetags/report.py | 44 ++++++++++++++++--- 2 files changed, 66 insertions(+), 7 deletions(-) diff --git a/docs/docs/report/helpers.md b/docs/docs/report/helpers.md index 79fde22b2f..6688a14a4a 100644 --- a/docs/docs/report/helpers.md +++ b/docs/docs/report/helpers.md @@ -547,14 +547,18 @@ You can add asset images to the reports and labels by using the `{% raw %}{% ass ## Parameters -If you need to load a parameter value for a particular model instance, within the context of your template, you can use the `parameter` template tag: +If you need to reference a parameter for a particular model instance, within the context of your template, you can use the `parameter` template tag: + +### parameter + +This returns a [Parameter](../concepts/parameters.md) object which contains the value of the parameter, as well as any associated metadata (e.g. units, description, etc). ::: report.templatetags.report.parameter options: show_docstring_description: false show_source: False -### Example +#### Example The following example assumes that you have a report or label which contains a valid [Part](../part/index.md) instance: @@ -580,6 +584,27 @@ A [Parameter](../concepts/parameters.md) has the following available attributes: | Units | The *units* of the parameter (e.g. "km") | | Template | A reference to a [ParameterTemplate](../concepts/parameters.md#parameter-templates) | +### parameter_value + +To access just the value of a parameter, use the `parameter_value` template tag: + +::: report.templatetags.report.parameter_value + options: + show_docstring_description: false + show_source: False + +#### Example + +``` +{% raw %} +{% load report %} + +{% parameter_value part "length" as length_value %} +Part: {{ part.name }}
+Length: {{ length_value }} +{% endraw %} +``` + ## Rendering Markdown Some data fields (such as the *Notes* field available on many internal database models) support [markdown formatting](https://en.wikipedia.org/wiki/Markdown). To render markdown content in a custom report, there are template filters made available through the [django-markdownify](https://github.com/erwinmatijsen/django-markdownify) library. This library provides functionality for converting markdown content to HTML representation, allowing it to be then rendered to PDF by the InvenTree report generation pipeline. diff --git a/src/backend/InvenTree/report/templatetags/report.py b/src/backend/InvenTree/report/templatetags/report.py index 748ac65c7a..f923aa2551 100644 --- a/src/backend/InvenTree/report/templatetags/report.py +++ b/src/backend/InvenTree/report/templatetags/report.py @@ -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()