Skip to content

Commit

Permalink
Unit check option (#5175)
Browse files Browse the repository at this point in the history
* Add option to control parameter units

* Check setting before validation

* Update part parameter settings page

* Update unit tests

* Update docs
  • Loading branch information
SchrodingersGat committed Jul 5, 2023
1 parent cf0d30b commit 3bea809
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 14 deletions.
7 changes: 7 additions & 0 deletions InvenTree/common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1249,6 +1249,13 @@ def save(self, *args, **kwargs):
'default': '',
},

'PART_PARAMETER_ENFORCE_UNITS': {
'name': _('Enforce Parameter Units'),
'description': _('If units are provided, parameter values must match the specified units'),
'default': True,
'validator': bool,
},

'PRICING_DECIMAL_PLACES_MIN': {
'name': _('Minimum Pricing Decimal Places'),
'description': _('Minimum number of decimal places to display when rendering pricing data'),
Expand Down
10 changes: 10 additions & 0 deletions InvenTree/part/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3532,6 +3532,16 @@ def clean(self):

super().clean()

# Validate the parameter data against the template units
if InvenTreeSetting.get_setting('PART_PARAMETER_ENFORCE_UNITS', True, cache=False, create=False):
if self.template.units:
try:
InvenTree.conversion.convert_physical_value(self.data, self.template.units)
except ValidationError as e:
raise ValidationError({
'data': e.message
})

# Validate the parameter data against the template choices
if choices := self.template.get_choices():
if self.data not in choices:
Expand Down
16 changes: 15 additions & 1 deletion InvenTree/part/test_param.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.test import TestCase, TransactionTestCase
from django.urls import reverse

from common.models import InvenTreeSetting
from InvenTree.unit_test import InvenTreeAPITestCase

from .models import (Part, PartCategory, PartCategoryParameterTemplate,
Expand Down Expand Up @@ -168,11 +169,24 @@ def test_param_unit_validation(self):
param = PartParameter(part=prt, template=template, data=value)
param.full_clean()

bad_values = ['3 Amps', '-3 zogs', '3.14F']

# Disable enforcing of part parameter units
InvenTreeSetting.set_setting('PART_PARAMETER_ENFORCE_UNITS', False, change_user=None)

# Invalid units also pass, but will be converted to the template units
for value in ['3 Amps', '-3 zogs', '3.14F']:
for value in bad_values:
param = PartParameter(part=prt, template=template, data=value)
param.full_clean()

# Enable enforcing of part parameter units
InvenTreeSetting.set_setting('PART_PARAMETER_ENFORCE_UNITS', True, change_user=None)

for value in bad_values:
param = PartParameter(part=prt, template=template, data=value)
with self.assertRaises(django_exceptions.ValidationError):
param.full_clean()

def test_param_unit_conversion(self):
"""Test that parameters are correctly converted to template units"""

Expand Down
41 changes: 28 additions & 13 deletions InvenTree/templates/InvenTree/settings/part_parameters.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,37 @@
{% block label %}part-parameters{% endblock label %}

{% block heading %}
{% trans "Part Parameter Templates" %}
{% trans "Part Parameters" %}
{% endblock heading %}

{% block actions %}
<button class='btn btn-success' id='new-param'>
<span class='fas fa-plus-circle'></span> {% trans "New Parameter" %}
</button>
{% endblock actions %}
{% block panel_content %}

{% block content %}
<div id='param-buttons'>
<div class='btn-group' role='group'>
{% include "filter_list.html" with id="parameter-templates" %}
<table class='table table-striped table-condensed'>
<tbody>
{% include "InvenTree/settings/setting.html" with key="PART_PARAMETER_ENFORCE_UNITS" icon="fa-clipboard-check" %}
</tbody>
</table>

<div class='panel-heading'>
<div class='d-flex flex-wrap'>
<h4>{% trans "Part Parameter Templates" %}</h4>
{% include "spacer.html" %}
<div class='btn-group' role='group'>
<button class='btn btn-success' id='new-param'>
<span class='fas fa-plus-circle'></span> {% trans "New Parameter" %}
</button>
</div>
</div>
</div>
<table class='table table-striped table-condensed' id='param-table' data-toolbar='#param-buttons'>
</table>

{% endblock content %}
<div class='panel-content'>
<div id='param-buttons'>
<div class='btn-group' role='group'>
{% include "filter_list.html" with id="parameter-templates" %}
</div>
</div>
<table class='table table-striped table-condensed' id='param-table' data-toolbar='#param-buttons'>
</table>
</div>

{% endblock panel_content %}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions docs/docs/part/parameter.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ If a part parameter is created with a value which is incompatible with the units
{% include 'img.html' %}
{% endwith %}

This behaviour can be disabled if required, so that any parameter value is accepted:

{% with id="enforce_units", url="part/part_parameters_enforce.png", description="Enforce part parameters" %}
{% include 'img.html' %}
{% endwith %}

### Parameter Sorting

Parameter sorting takes unit conversion into account, meaning that values provided in different (but compatible) units are sorted correctly:
Expand Down

0 comments on commit 3bea809

Please sign in to comment.