2
0
mirror of https://github.com/inventree/InvenTree.git synced 2026-04-13 06:48:44 +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:
Oliver
2026-04-08 14:14:44 +10:00
committed by GitHub
parent 2753a437cc
commit 4d2ed8fcba
2 changed files with 66 additions and 7 deletions

View File

@@ -547,14 +547,18 @@ You can add asset images to the reports and labels by using the `{% raw %}{% ass
## Parameters ## 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 ::: report.templatetags.report.parameter
options: options:
show_docstring_description: false show_docstring_description: false
show_source: 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: 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") | | Units | The *units* of the parameter (e.g. "km") |
| Template | A reference to a [ParameterTemplate](../concepts/parameters.md#parameter-templates) | | 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 }}<br>
Length: {{ length_value }}
{% endraw %}
```
## Rendering Markdown ## 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. 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.

View File

@@ -473,10 +473,10 @@ def parameter(
Arguments: Arguments:
instance: A Model object 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: Returns:
A Parameter object, or None if not found A Parameter object, or the provided default value if not found
""" """
if instance is None: if instance is None:
raise ValueError('parameter tag requires a valid Model instance') 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'): if not isinstance(instance, Model) or not hasattr(instance, 'parameters'):
raise TypeError("parameter tag requires a Model with 'parameters' attribute") raise TypeError("parameter tag requires a Model with 'parameters' attribute")
return ( # First try with exact match
instance.parameters if (
parameter := instance.parameters
.prefetch_related('template') .prefetch_related('template')
.filter(template__name=parameter_name) .filter(template__name=parameter_name)
.first() .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() @register.simple_tag()