diff --git a/InvenTree/InvenTree/conversion.py b/InvenTree/InvenTree/conversion.py index e154dc76f8..af6ba9f872 100644 --- a/InvenTree/InvenTree/conversion.py +++ b/InvenTree/InvenTree/conversion.py @@ -165,6 +165,13 @@ def convert_physical_value(value: str, unit: str = None, strip_units=True): value = str(value).strip() if value else '' unit = str(unit).strip() if unit else '' + # Handle imperial length measurements + if value.count("'") == 1 and value.endswith("'"): + value = value.replace("'", ' feet') + + if value.count('"') == 1 and value.endswith('"'): + value = value.replace('"', ' inches') + # Error on blank values if not value: raise ValidationError(_('No value provided')) diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index 8a3d41b528..1a8fb3ed1c 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -138,6 +138,24 @@ class ConversionTest(TestCase): q = InvenTree.conversion.convert_physical_value(val, 'W', strip_units=False) self.assertAlmostEqual(float(q.magnitude), expected, places=2) + def test_imperial_lengths(self): + """Test support of imperial length measurements.""" + tests = [ + ('1 inch', 'mm', 25.4), + ('1 "', 'mm', 25.4), + ('2 "', 'inches', 2), + ('3 feet', 'inches', 36), + ("3'", 'inches', 36), + ("7 '", 'feet', 7), + ] + + for val, unit, expected in tests: + output = InvenTree.conversion.convert_physical_value( + val, unit, strip_units=True + ) + + self.assertAlmostEqual(output, expected, 3) + def test_dimensionless_units(self): """Tests for 'dimensionless' unit quantities.""" # Test some dimensionless units diff --git a/docs/docs/concepts/units.md b/docs/docs/concepts/units.md index b15d4e40f1..7b26ae445f 100644 --- a/docs/docs/concepts/units.md +++ b/docs/docs/concepts/units.md @@ -34,6 +34,15 @@ Scientific notation is also supported, and can be used to represent very large o !!! tip "Case Sensitive" Support for scientific notation is case sensitive. For example, `1E3` is a valid value, but `1e3` is not. +### Feet and Inches + +Shorthand notation is supported for feet and inches. For example, the following values would all be considered *valid*: + +- `3'`: `3 feet` +- `6"` : `6 inches` + +However, note that compound measurements (e.g. `3'6"`) are not supported. + ### Case Sensitivity The pint library is case sensitive, and units must be specified in the correct case. For example, `kg` is a valid unit, but `KG` is not. In particular, you need to pay close attention when using SI prefixes (e.g. `k` for kilo, `M` for mega, `n` for nano, etc).