mirror of
https://github.com/inventree/InvenTree.git
synced 2026-04-13 14:58:47 +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:
@@ -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 }}<br>
|
||||
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.
|
||||
|
||||
@@ -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