From c59eee735953d881278175234ef6669a96f5d77c Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Thu, 6 Jul 2023 11:11:27 +1000 Subject: [PATCH] Param fix (#5183) (#5184) * Handle AttributeError in convert_physical_value * Added new unit test (cherry picked from commit 9abcc0ec34cdcaade7047965da45ff47be14b294) Co-authored-by: Oliver --- InvenTree/InvenTree/conversion.py | 2 +- InvenTree/InvenTree/tests.py | 17 +++++++++++++++++ InvenTree/templates/js/translated/part.js | 1 + 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/conversion.py b/InvenTree/InvenTree/conversion.py index 8637ae382b..67e18dc0e7 100644 --- a/InvenTree/InvenTree/conversion.py +++ b/InvenTree/InvenTree/conversion.py @@ -91,7 +91,7 @@ def convert_physical_value(value: str, unit: str = None): # At this point we *should* have a valid pint value # To double check, look at the maginitude float(val.magnitude) - except (TypeError, ValueError): + except (TypeError, ValueError, AttributeError): error = _('Provided value is not a valid number') except (pint.errors.UndefinedUnitError, pint.errors.DefinitionSyntaxError): error = _('Provided value has an invalid unit') diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index 83c340e7e5..a68f7c4739 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -56,6 +56,23 @@ class ConversionTest(TestCase): q = InvenTree.conversion.convert_physical_value(val).to_base_units() self.assertEqual(q.magnitude, expected) + def test_invalid_values(self): + """Test conversion of invalid inputs""" + + inputs = [ + '-', + ';;', + '-x', + '?', + '--', + '+', + '++', + ] + + for val in inputs: + with self.assertRaises(ValidationError): + InvenTree.conversion.convert_physical_value(val) + class ValidatorTest(TestCase): """Simple tests for custom field validators.""" diff --git a/InvenTree/templates/js/translated/part.js b/InvenTree/templates/js/translated/part.js index ebce845a7c..bba41ab980 100644 --- a/InvenTree/templates/js/translated/part.js +++ b/InvenTree/templates/js/translated/part.js @@ -1404,6 +1404,7 @@ function createPartParameter(part_id, options={}) { function editPartParameter(param_id, options={}) { options.fields = partParameterFields(); options.title = '{% trans "Edit Parameter" %}'; + options.focus = 'data'; options.processBeforeUpload = function(data) { // Convert data to string