Merge pull request #829 from SchrodingersGat/bom-validation

Integer value required for trackable bom item
This commit is contained in:
Oliver 2020-05-24 20:26:18 +10:00 committed by GitHub
commit 4292a32ab9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 0 deletions

View File

@ -1339,6 +1339,11 @@ class BomItem(models.Model):
checksum: Validation checksum for the particular BOM line item
"""
def save(self, *args, **kwargs):
self.clean()
super().save(*args, **kwargs)
def get_absolute_url(self):
return reverse('bom-item-detail', kwargs={'pk': self.id})
@ -1431,6 +1436,16 @@ class BomItem(models.Model):
- A part cannot refer to a part which refers to it
"""
# If the sub_part is 'trackable' then the 'quantity' field must be an integer
try:
if self.sub_part.trackable:
if not self.quantity == int(self.quantity):
raise ValidationError({
"quantity": _("Quantity must be integer value for trackable parts")
})
except Part.DoesNotExist:
pass
# A part cannot refer to itself in its BOM
try:
if self.sub_part is not None and self.part is not None:

View File

@ -44,6 +44,20 @@ class BomItemTest(TestCase):
item = BomItem.objects.create(part=self.bob, sub_part=self.bob, quantity=7)
item.clean()
def test_integer_quantity(self):
"""
Test integer validation for BomItem
"""
p = Part.objects.create(name="test", description="d", component=True, trackable=True)
# Creation of a BOMItem with a non-integer quantity of a trackable Part should fail
with self.assertRaises(django_exceptions.ValidationError):
BomItem.objects.create(part=self.bob, sub_part=p, quantity=21.7)
# But with an integer quantity, should be fine
BomItem.objects.create(part=self.bob, sub_part=p, quantity=21)
def test_overage(self):
""" Test that BOM line overages are calculated correctly """