[bug] External build validation (#13399)

Check if external builds are enabled
This commit is contained in:
Oliver authored and GitHub committed 2026-11-27 11:03:32 +11:00
1 parent 3a8c73fe22
commit b0ffa90c05
2 files changed
+73

No files matched your search

+11
View File
@@ -244,6 +244,17 @@ class Build(
)
})
# A build order can only be marked as external if the feature is enabled.
# Existing external build orders remain valid if the feature is later disabled.
if (
self.external
and (self.pk is None or self.has_field_changed('external'))
and not get_global_setting('BUILDORDER_EXTERNAL_BUILDS', cache=False)
):
raise ValidationError({
'external': _('External build orders are not enabled')
})
if get_global_setting('BUILDORDER_REQUIRE_RESPONSIBLE'):
if not self.responsible:
raise ValidationError({
+62
View File
@@ -1333,6 +1333,68 @@ class ExternalBuildTest(InvenTreeAPITestCase):
str(err.exception.messages),
)
def test_external_setting(self):
"""External build orders can only be created when the feature is enabled."""
part = Part.objects.create(
name='Test part',
description='A test part',
assembly=True,
purchaseable=True,
)
set_global_setting('BUILDORDER_EXTERNAL_BUILDS', False)
# Cannot create a new external build order
build = Build(part=part, title='Test build order', quantity=10, external=True)
with self.assertRaises(ValidationError) as err:
build.clean()
self.assertIn('External build orders are not enabled', str(err.exception))
# ... including via the API
url = reverse('api-build-list')
data = {
'part': part.pk,
'quantity': 10,
'title': 'API build order',
'reference': 'BO-9901',
'external': True,
}
self.assignRole('build.add')
response = self.post(url, data, expected_code=400)
self.assertIn('External build orders are not enabled', str(response.data))
# Cannot convert an existing (internal) build order to external
build = Build.objects.create(
part=part, title='Internal build order', quantity=10, external=False
)
build.external = True
with self.assertRaises(ValidationError):
build.clean()
# Enable the feature - now external build orders can be created
set_global_setting('BUILDORDER_EXTERNAL_BUILDS', True)
build.clean()
build.save()
self.post(url, data, expected_code=201)
# Disable the feature again - existing external builds can still be edited
set_global_setting('BUILDORDER_EXTERNAL_BUILDS', False)
build.refresh_from_db()
self.assertTrue(build.external)
build.title = 'Updated title'
build.clean()
# ... and can be converted back to internal builds
build.external = False
build.clean()
def test_build_requirement(self):
"""Test the global 'BUILDORDER_EXTERNAL_REQUIRED' setting."""
# Create required test data