2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-18 04:55:44 +00:00

Unit check option (#5175)

* Add option to control parameter units

* Check setting before validation

* Update part parameter settings page

* Update unit tests

* Update docs
This commit is contained in:
Oliver
2023-07-05 11:11:19 +10:00
committed by GitHub
parent cf0d30b11c
commit 3bea809823
6 changed files with 67 additions and 15 deletions

View File

@ -3532,6 +3532,16 @@ class PartParameter(MetadataMixin, models.Model):
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:

View File

@ -4,6 +4,7 @@ import django.core.exceptions as django_exceptions
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,
@ -168,11 +169,24 @@ class ParameterTests(TestCase):
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"""