diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 83c2c63de5..51f0b4d460 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -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""" diff --git a/InvenTree/part/test_param.py b/InvenTree/part/test_param.py index 91537cbfa3..63a3b4f8d2 100644 --- a/InvenTree/part/test_param.py +++ b/InvenTree/part/test_param.py @@ -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""" diff --git a/InvenTree/report/templatetags/report.py b/InvenTree/report/templatetags/report.py index 5f90ff807f..cb8c8f2105 100644 --- a/InvenTree/report/templatetags/report.py +++ b/InvenTree/report/templatetags/report.py @@ -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. diff --git a/docs/docs/assets/images/report/label_with_parameters.png b/docs/docs/assets/images/report/label_with_parameters.png new file mode 100644 index 0000000000..b127849f1f Binary files /dev/null and b/docs/docs/assets/images/report/label_with_parameters.png differ diff --git a/docs/docs/report/helpers.md b/docs/docs/report/helpers.md index e2253f2737..6ed679a44b 100644 --- a/docs/docs/report/helpers.md +++ b/docs/docs/report/helpers.md @@ -224,3 +224,31 @@ You can add asset images to the reports and labels by using the `{% raw %}{% ass {% 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 }}
+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) | diff --git a/docs/docs/report/labels/part_labels.md b/docs/docs/report/labels/part_labels.md index fe5d5cdefb..7e9e5606b8 100644 --- a/docs/docs/report/labels/part_labels.md +++ b/docs/docs/report/labels/part_labels.md @@ -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 %} + +
+ Part: {{ part.full_name }}
+ Width: {{ width.data }} [{{ width.units }}]
+ Length: {{ length.data }} [{{ length.units }}] +
+ +{% 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 %}