From 7e5521edf24f989e0d0218efad061e5ef46fd58e Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 2 Dec 2026 11:02:05 +1100 Subject: [PATCH] prevent editing BomItemSubstitute against a locked assembly (#13431) --- src/backend/InvenTree/part/models.py | 45 ++++++- src/backend/InvenTree/part/test_api.py | 77 ++++++++++++ src/backend/InvenTree/part/test_bom_item.py | 124 ++++++++++++++++++++ 3 files changed, 245 insertions(+), 1 deletion(-) diff --git a/src/backend/InvenTree/part/models.py b/src/backend/InvenTree/part/models.py index adb1e93c8c..0203b25e55 100644 --- a/src/backend/InvenTree/part/models.py +++ b/src/backend/InvenTree/part/models.py @@ -3675,12 +3675,55 @@ class BomItemSubstitute(InvenTreeAuditMixin, InvenTreeMetadataModel): return ContentType.objects.get_for_model(Part), str(part_id) + def check_part_lock(self, bom_item: BomItem | None = None): + """Check if the assembly associated with the BomItem is locked. + + Arguments: + bom_item: The BomItem to check (defaults to self.bom_item) + + Raises: + ValidationError: If the assembly (or a variant assembly) is locked + """ + bom_item = bom_item or self.bom_item + + if bom_item: + bom_item.check_part_lock(bom_item.part) + def save(self, *args, **kwargs): - """Enforce a full_clean when saving the BomItemSubstitute model.""" + """Enforce a full_clean when saving the BomItemSubstitute model. + + Raises: + ValidationError: If the substitute is invalid, or the assembly is locked + """ self.full_clean() + self.check_part_lock() + + # If this substitute is being moved from a different BomItem, check that one too + if self.pk: + old_bom_item_id = ( + BomItemSubstitute.objects + .filter(pk=self.pk) + .values_list('bom_item', flat=True) + .first() + ) + + if old_bom_item_id and old_bom_item_id != self.bom_item_id: + if old_bom_item := BomItem.objects.filter(pk=old_bom_item_id).first(): + self.check_part_lock(old_bom_item) + super().save(*args, **kwargs) + def delete(self, *args, **kwargs): + """Prevent deletion of a BomItemSubstitute if the assembly is locked. + + Raises: + ValidationError: If the assembly is locked + """ + self.check_part_lock() + + return super().delete(*args, **kwargs) + def validate_unique(self, exclude=None): """Ensure that this BomItemSubstitute is "unique". diff --git a/src/backend/InvenTree/part/test_api.py b/src/backend/InvenTree/part/test_api.py index 3604a8f820..0de56401e4 100644 --- a/src/backend/InvenTree/part/test_api.py +++ b/src/backend/InvenTree/part/test_api.py @@ -3391,6 +3391,83 @@ class BomItemTest(InvenTreeAPITestCase): self.assertEqual(len(response.data), 2) + def test_substitutes_locked_assembly(self): + """Substitutes cannot be created, edited or deleted via the API for a locked assembly.""" + self.assignRole('bom.view') + self.assignRole('bom.add') + self.assignRole('bom.change') + self.assignRole('bom.delete') + + list_url = reverse('api-bom-substitute-list') + + assembly = Part.objects.create( + name='Locked API Assembly', description='An assembly', assembly=True + ) + sub_part = Part.objects.create( + name='API Sub Part', description='A sub-part', component=True + ) + alt_1 = Part.objects.create(name='API Alt 1', description='Alt', component=True) + alt_2 = Part.objects.create(name='API Alt 2', description='Alt', component=True) + + bom_item = BomItem.objects.create(part=assembly, sub_part=sub_part, quantity=1) + substitute = BomItemSubstitute.objects.create(bom_item=bom_item, part=alt_1) + + # Validate, then lock the assembly + assembly.validate_bom(self.user) + assembly.locked = True + assembly.save() + + assembly.refresh_from_db() + checksum = assembly.bom_checksum + self.assertTrue(assembly.is_bom_valid()) + + detail_url = reverse('api-bom-substitute-detail', kwargs={'pk': substitute.pk}) + + # Adding a new BOM item is blocked (control) + self.post( + reverse('api-bom-list'), + {'part': assembly.pk, 'sub_part': alt_2.pk, 'quantity': 1}, + expected_code=400, + ) + + # Adding a substitute is blocked + response = self.post( + list_url, {'bom_item': bom_item.pk, 'part': alt_2.pk}, expected_code=400 + ) + self.assertIn('assembly is locked', str(response.data)) + + # Editing a substitute is blocked + response = self.patch(detail_url, {'part': alt_2.pk}, expected_code=400) + self.assertIn('assembly is locked', str(response.data)) + + # Deleting a substitute is blocked + response = self.delete(detail_url, expected_code=400) + self.assertIn('assembly is locked', str(response.data)) + + # Nothing has changed + self.assertEqual( + list( + BomItemSubstitute.objects.filter(bom_item=bom_item).values_list( + 'part', flat=True + ) + ), + [alt_1.pk], + ) + + assembly.refresh_from_db() + self.assertTrue(assembly.locked) + self.assertEqual(assembly.bom_checksum, checksum) + self.assertTrue(assembly.is_bom_valid()) + + # Unlock the assembly - changes are now allowed + assembly.locked = False + assembly.save() + + self.post( + list_url, {'bom_item': bom_item.pk, 'part': alt_2.pk}, expected_code=201 + ) + self.delete(detail_url, expected_code=204) + def test_substitutes(self): """Tests for BomItem substitutes.""" url = reverse('api-bom-substitute-list') diff --git a/src/backend/InvenTree/part/test_bom_item.py b/src/backend/InvenTree/part/test_bom_item.py index 8eb6e2a0a0..f7cfe5a279 100644 --- a/src/backend/InvenTree/part/test_bom_item.py +++ b/src/backend/InvenTree/part/test_bom_item.py @@ -536,6 +536,130 @@ class BomItemTest(TestCase): bom_item2.quantity = 99 bom_item2.save() + def test_locked_assembly_substitutes(self): + """BomItemSubstitute objects cannot be created, edited or deleted for a locked assembly.""" + assembly = Part.objects.create( + name='Locked Assembly', description='An assembly part', assembly=True + ) + other_assembly = Part.objects.create( + name='Unlocked Assembly', description='An assembly part', assembly=True + ) + + sub_part = Part.objects.create( + name='Sub Part', description='A sub-part', component=True + ) + alt_1 = Part.objects.create(name='Alt 1', description='Alt', component=True) + alt_2 = Part.objects.create(name='Alt 2', description='Alt', component=True) + + bom_item = BomItem.objects.create(part=assembly, sub_part=sub_part, quantity=1) + other_bom_item = BomItem.objects.create( + part=other_assembly, sub_part=sub_part, quantity=1 + ) + + substitute = BomItemSubstitute.objects.create(bom_item=bom_item, part=alt_1) + other_substitute = BomItemSubstitute.objects.create( + bom_item=other_bom_item, part=alt_1 + ) + + # Lock the assembly + assembly.locked = True + assembly.save() + + n = BomItemSubstitute.objects.count() + + # Cannot create a new substitute + with self.assertRaises(django_exceptions.ValidationError): + BomItemSubstitute.objects.create(bom_item=bom_item, part=alt_2) + + # Cannot edit an existing substitute + with self.assertRaises(django_exceptions.ValidationError): + substitute.part = alt_2 + substitute.save() + + substitute.refresh_from_db() + + # Cannot move a substitute *away* from the locked assembly + with self.assertRaises(django_exceptions.ValidationError): + substitute.bom_item = other_bom_item + substitute.part = alt_2 + substitute.save() + + substitute.refresh_from_db() + + # Cannot move a substitute *onto* the locked assembly + with self.assertRaises(django_exceptions.ValidationError): + other_substitute.bom_item = bom_item + other_substitute.part = alt_2 + other_substitute.save() + + other_substitute.refresh_from_db() + + # Cannot delete a substitute + with self.assertRaises(django_exceptions.ValidationError): + substitute.delete() + + # Nothing has changed + self.assertEqual(BomItemSubstitute.objects.count(), n) + substitute.refresh_from_db() + self.assertEqual(substitute.bom_item, bom_item) + self.assertEqual(substitute.part, alt_1) + self.assertEqual(other_substitute.bom_item, other_bom_item) + + # Substitutes for the unlocked assembly are unaffected + other_substitute.part = alt_2 + other_substitute.save() + other_substitute.delete() + + # With locking disabled, substitutes can be modified + set_global_setting('PART_ENABLE_LOCKING', False) + + new_sub = BomItemSubstitute.objects.create(bom_item=bom_item, part=alt_2) + new_sub.delete() + + set_global_setting('PART_ENABLE_LOCKING', True) + + # Unlock the assembly - substitutes can be modified again + assembly.locked = False + assembly.save() + + new_sub = BomItemSubstitute.objects.create(bom_item=bom_item, part=alt_2) + substitute.delete() + new_sub.delete() + + self.assertEqual(BomItemSubstitute.objects.filter(bom_item=bom_item).count(), 0) + + def test_locked_variant_substitutes(self): + """Substitutes for an inherited BomItem cannot be modified if a variant assembly is locked.""" + template = Part.objects.create( + name='Template Assembly', + description='A template assembly', + assembly=True, + is_template=True, + ) + variant = Part.objects.create( + name='Variant Assembly', + description='A variant assembly', + assembly=True, + variant_of=template, + ) + + sub_part = Part.objects.create( + name='Sub Part', description='A sub-part', component=True + ) + alt = Part.objects.create(name='Alt', description='Alt', component=True) + + bom_item = BomItem.objects.create( + part=template, sub_part=sub_part, quantity=1, inherited=True + ) + + variant.locked = True + variant.save() + + with self.assertRaises(django_exceptions.ValidationError): + BomItemSubstitute.objects.create(bom_item=bom_item, part=alt) + + self.assertEqual(BomItemSubstitute.objects.filter(bom_item=bom_item).count(), 0) + def test_bom_hash_order_consistency(self): """Regression test for BOM checksum instability due to non-deterministic item ordering.