mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-04 07:05:41 +00:00 
			
		
		
		
	Fix for % (percent) unit (missing unary operator "%") (#5527)
				
					
				
			* Fix `%` (percent) unit conversions * Add tests for percent unit * Fix formatting
This commit is contained in:
		@@ -106,7 +106,7 @@ def convert_physical_value(value: str, unit: str = None, strip_units=True):
 | 
				
			|||||||
                val = ureg.Quantity(value, unit)
 | 
					                val = ureg.Quantity(value, unit)
 | 
				
			||||||
            else:
 | 
					            else:
 | 
				
			||||||
                # Convert to the provided unit (may raise an exception)
 | 
					                # Convert to the provided unit (may raise an exception)
 | 
				
			||||||
                val = val.to(unit)
 | 
					                val = val.to(ureg.Unit(unit))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # At this point we *should* have a valid pint value
 | 
					        # At this point we *should* have a valid pint value
 | 
				
			||||||
        # To double check, look at the maginitude
 | 
					        # To double check, look at the maginitude
 | 
				
			||||||
@@ -134,7 +134,7 @@ def convert_physical_value(value: str, unit: str = None, strip_units=True):
 | 
				
			|||||||
    # So, we ensure that it is converted to a floating point value
 | 
					    # So, we ensure that it is converted to a floating point value
 | 
				
			||||||
    # If we wish to return a "raw" value, some trickery is required
 | 
					    # If we wish to return a "raw" value, some trickery is required
 | 
				
			||||||
    if unit:
 | 
					    if unit:
 | 
				
			||||||
        magnitude = ureg.Quantity(val.to(unit)).magnitude
 | 
					        magnitude = ureg.Quantity(val.to(ureg.Unit(unit))).magnitude
 | 
				
			||||||
    else:
 | 
					    else:
 | 
				
			||||||
        magnitude = ureg.Quantity(val.to_base_units()).magnitude
 | 
					        magnitude = ureg.Quantity(val.to_base_units()).magnitude
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -144,7 +144,7 @@ class ParameterTests(TestCase):
 | 
				
			|||||||
        """Test validation of 'units' field for PartParameterTemplate"""
 | 
					        """Test validation of 'units' field for PartParameterTemplate"""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # Test that valid units pass
 | 
					        # Test that valid units pass
 | 
				
			||||||
        for unit in [None, '', 'mm', 'A', 'm^2', 'Pa', 'V', 'C', 'F', 'uF', 'mF', 'millifarad']:
 | 
					        for unit in [None, '', '%', 'mm', 'A', 'm^2', 'Pa', 'V', 'C', 'F', 'uF', 'mF', 'millifarad']:
 | 
				
			||||||
            tmp = PartParameterTemplate(name='test', units=unit)
 | 
					            tmp = PartParameterTemplate(name='test', units=unit)
 | 
				
			||||||
            tmp.full_clean()
 | 
					            tmp.full_clean()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -169,6 +169,15 @@ class ParameterTests(TestCase):
 | 
				
			|||||||
            param = PartParameter(part=prt, template=template, data=value)
 | 
					            param = PartParameter(part=prt, template=template, data=value)
 | 
				
			||||||
            param.full_clean()
 | 
					            param.full_clean()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # Test that percent unit is working
 | 
				
			||||||
 | 
					        template2 = PartParameterTemplate.objects.create(
 | 
				
			||||||
 | 
					            name='My Template 2',
 | 
				
			||||||
 | 
					            units='%',
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					        for value in ["1", "1%", "1 percent"]:
 | 
				
			||||||
 | 
					            param = PartParameter(part=prt, template=template2, data=value)
 | 
				
			||||||
 | 
					            param.full_clean()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        bad_values = ['3 Amps', '-3 zogs', '3.14F']
 | 
					        bad_values = ['3 Amps', '-3 zogs', '3.14F']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # Disable enforcing of part parameter units
 | 
					        # Disable enforcing of part parameter units
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user