Parameter by name (#5055)

* Add method get_parameter

- Return a parameter for a part, on name

* Add unit test for new method

* Adds template tag to retrieve parameter based on name

* Update docs
This commit is contained in:
Oliver 2023-06-16 12:14:17 +10:00 committed by GitHub
parent 51cece9e07
commit 31ff3599eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 122 additions and 4 deletions

View File

@ -2176,6 +2176,16 @@ class Part(InvenTreeBarcodeMixin, InvenTreeNotesMixin, MetadataMixin, MPTTModel)
return quantity
def get_parameter(self, name):
"""Return the parameter with the given name.
If no matching parameter is found, return None.
"""
try:
return self.parameters.get(template__name=name)
except PartParameter.DoesNotExist:
return None
def get_parameters(self):
"""Return all parameters for this part, ordered by name."""
return self.parameters.order_by('template__name')
@ -3598,6 +3608,21 @@ class PartParameter(MetadataMixin, models.Model):
blank=True,
)
@property
def units(self):
"""Return the units associated with the template"""
return self.template.units
@property
def name(self):
"""Return the name of the template"""
return self.template.name
@property
def description(self):
"""Return the description of the template"""
return self.template.description
@classmethod
def create(cls, part, template, data, save=False):
"""Custom save method for the PartParameter class"""

View File

@ -63,6 +63,20 @@ class TestParams(TestCase):
self.assertEqual(len(p.metadata.keys()), 4)
def test_get_parameter(self):
"""Test the Part.get_parameter method"""
prt = Part.objects.get(pk=3)
# Check that we can get a parameter by name
for name in ['Length', 'Width', 'Thickness']:
param = prt.get_parameter(name)
self.assertEqual(param.template.name, name)
# Check that an incorrect name returns None
param = prt.get_parameter('Not a parameter')
self.assertIsNone(param)
class TestCategoryTemplates(TransactionTestCase):
"""Test class for PartCategoryParameterTemplate model"""

View File

@ -181,7 +181,7 @@ def encode_svg_image(filename):
@register.simple_tag()
def part_image(part):
def part_image(part: Part):
"""Return a fully-qualified path for a part image.
Arguments:
@ -200,6 +200,23 @@ def part_image(part):
return uploaded_image(img)
@register.simple_tag()
def part_parameter(part: Part, parameter_name: str):
"""Return a PartParameter object for the given part and parameter name
Arguments:
part: A Part object
parameter_name: The name of the parameter to retrieve
Returns:
A PartParameter object, or None if not found
"""
if type(part) is Part:
return part.get_parameter(parameter_name)
else:
return None
@register.simple_tag()
def company_image(company):
"""Return a fully-qualified path for a company image.

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

View File

@ -224,3 +224,31 @@ You can add asset images to the reports and labels by using the `{% raw %}{% ass
<img src="{% asset 'my_awesome_logo.png' %}"/>
{% endraw %}
```
## Part Parameters
If you need to load a part parameter for a particular Part, within the context of your template, you can use the `part_parameter` template tag.
The following example assumes that you have a report or label which contains a valid [Part](../part/part.md) instance:
```
{% raw %}
{% load report %}
{% part_parameter part "length" as length %}
Part: {{ part.name }}<br>
Length: {{ length.data }} [{{ length.units }}]
{% endraw %}
```
A [Part Parameter](../part/parameter.md) has the following available attributes:
| Attribute | Description |
| --- | --- |
| Name | The *name* of the parameter (e.g. "Length") |
| Description | The *description* of the parameter |
| Data | The *value* of the parameter (e.g. "123.4") |
| Units | The *units* of the parameter (e.g. "km") |
| Template | A reference to a [PartParameterTemplate](../part/parameter.md#parameter-templates) |

View File

@ -34,9 +34,9 @@ The following context variables are made available to the Part label template:
| qr_data | String data which can be rendered to a QR code |
| parameters | Map (Python dictionary) object containing the parameters associated with the part instance |
#### Parameters
#### Parameter Values
The part parameters can be accessed by parameter name lookup in the template, as follows:
The part parameter *values* can be accessed by parameter name lookup in the template, as follows:
```html
{% raw %}
@ -47,7 +47,8 @@ Length: {{ parameters.length }}
{% endraw %}
```
Note that for parameters which include a `space` character in their name, lookup using the "dot" notation won't work! In this case, try using the [key lookup](../helpers.md#key-access) method:
!!! warning "Spaces"
Note that for parameters which include a `space` character in their name, lookup using the "dot" notation won't work! In this case, try using the [key lookup](../helpers.md#key-access) method:
```html
{% raw %}
@ -55,3 +56,36 @@ Note that for parameters which include a `space` character in their name, lookup
Voltage Rating: {% getkey parameters "Voltage Rating" %}
{% endraw %}
```
#### Parameter Data
If you require access to the parameter data itself, and not just the "value" of a particular parameter, you can use the `part_parameter` [helper function](../helpers.md#part-parameters).
For example, the following label template can be used to generate a label which contains parameter data in addition to parameter units:
```html
{% raw %}
{% extends "label/label_base.html" %}
{% load report %}
{% block content %}
{% part_parameter part "Width" as width %}
{% part_parameter part "Length" as length %}
<div>
Part: {{ part.full_name }}<br>
Width: {{ width.data }} [{{ width.units }}]<br>
Length: {{ length.data }} [{{ length.units }}]
</div>
{% endblock content %}
{% endraw %}
```
The following label is produced:
{% with id="report-parameters", url="report/label_with_parameters.png", description="Label with parameters" %}
{% include 'img.html' %}
{% endwith %}