mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-18 04:55:44 +00:00
Parameter by name (#5055)
* Add method get_parameter - Return a parameter for a part, on name * Add unit test for new method * Adds template tag to retrieve parameter based on name * Update docs
This commit is contained in:
@ -2176,6 +2176,16 @@ class Part(InvenTreeBarcodeMixin, InvenTreeNotesMixin, MetadataMixin, MPTTModel)
|
||||
|
||||
return quantity
|
||||
|
||||
def get_parameter(self, name):
|
||||
"""Return the parameter with the given name.
|
||||
|
||||
If no matching parameter is found, return None.
|
||||
"""
|
||||
try:
|
||||
return self.parameters.get(template__name=name)
|
||||
except PartParameter.DoesNotExist:
|
||||
return None
|
||||
|
||||
def get_parameters(self):
|
||||
"""Return all parameters for this part, ordered by name."""
|
||||
return self.parameters.order_by('template__name')
|
||||
@ -3598,6 +3608,21 @@ class PartParameter(MetadataMixin, models.Model):
|
||||
blank=True,
|
||||
)
|
||||
|
||||
@property
|
||||
def units(self):
|
||||
"""Return the units associated with the template"""
|
||||
return self.template.units
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the template"""
|
||||
return self.template.name
|
||||
|
||||
@property
|
||||
def description(self):
|
||||
"""Return the description of the template"""
|
||||
return self.template.description
|
||||
|
||||
@classmethod
|
||||
def create(cls, part, template, data, save=False):
|
||||
"""Custom save method for the PartParameter class"""
|
||||
|
@ -63,6 +63,20 @@ class TestParams(TestCase):
|
||||
|
||||
self.assertEqual(len(p.metadata.keys()), 4)
|
||||
|
||||
def test_get_parameter(self):
|
||||
"""Test the Part.get_parameter method"""
|
||||
|
||||
prt = Part.objects.get(pk=3)
|
||||
|
||||
# Check that we can get a parameter by name
|
||||
for name in ['Length', 'Width', 'Thickness']:
|
||||
param = prt.get_parameter(name)
|
||||
self.assertEqual(param.template.name, name)
|
||||
|
||||
# Check that an incorrect name returns None
|
||||
param = prt.get_parameter('Not a parameter')
|
||||
self.assertIsNone(param)
|
||||
|
||||
|
||||
class TestCategoryTemplates(TransactionTestCase):
|
||||
"""Test class for PartCategoryParameterTemplate model"""
|
||||
|
Reference in New Issue
Block a user