From 9b272bd60fcdc0b1c66489cd30162ed442632725 Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 5 Sep 2024 14:47:22 +1000 Subject: [PATCH] Add option for controlling test result uploads (#8076) * Fix typo * Add new setting * Gate creation of new test result template * Refactor test result template validation * Update API unit testing --- docs/docs/settings/global.md | 1 + src/backend/InvenTree/common/models.py | 8 +++++ src/backend/InvenTree/stock/models.py | 33 +++++++++---------- src/backend/InvenTree/stock/serializers.py | 4 +-- src/backend/InvenTree/stock/test_api.py | 8 +++++ .../templates/InvenTree/settings/stock.html | 1 + .../pages/Index/Settings/SystemSettings.tsx | 3 +- 7 files changed, 37 insertions(+), 21 deletions(-) diff --git a/docs/docs/settings/global.md b/docs/docs/settings/global.md index 7de1186167..d2ecaf44e4 100644 --- a/docs/docs/settings/global.md +++ b/docs/docs/settings/global.md @@ -188,6 +188,7 @@ Configuration of stock item options {{ globalsetting("STOCK_ENFORCE_BOM_INSTALLATION") }} {{ globalsetting("STOCK_ALLOW_OUT_OF_STOCK_TRANSFER") }} {{ globalsetting("TEST_STATION_DATA") }} +{{ globalsetting("TEST_UPLOAD_CREATE_TEMPLATE") }} ### Build Orders diff --git a/src/backend/InvenTree/common/models.py b/src/backend/InvenTree/common/models.py index ac13e8d3c6..24e718bd5e 100644 --- a/src/backend/InvenTree/common/models.py +++ b/src/backend/InvenTree/common/models.py @@ -2160,6 +2160,14 @@ class InvenTreeSetting(BaseInvenTreeSetting): 'default': False, 'validator': bool, }, + 'TEST_UPLOAD_CREATE_TEMPLATE': { + 'name': _('Create Template on Upload'), + 'description': _( + 'Create a new test template when uploading test data which does not match an existing template' + ), + 'default': True, + 'validator': bool, + }, } typ = 'inventree' diff --git a/src/backend/InvenTree/stock/models.py b/src/backend/InvenTree/stock/models.py index c2ea88f192..d849588b81 100644 --- a/src/backend/InvenTree/stock/models.py +++ b/src/backend/InvenTree/stock/models.py @@ -2429,28 +2429,25 @@ class StockItemTestResult(InvenTree.models.InvenTreeMetadataModel): super().clean() # If this test result corresponds to a template, check the requirements of the template - template = self.template + try: + template = self.template + except PartModels.PartTestTemplate.DoesNotExist: + template = None - if template is None: - # Fallback if there is no matching template - for template in self.stock_item.part.getTestTemplates(): - if self.key == template.key: - break + if not template: + raise ValidationError({'template': _('Test template does not exist')}) - if template: - if template.requires_value and not self.value: - raise ValidationError({ - 'value': _('Value must be provided for this test') - }) + if template.requires_value and not self.value: + raise ValidationError({'value': _('Value must be provided for this test')}) - if template.requires_attachment and not self.attachment: - raise ValidationError({ - 'attachment': _('Attachment must be uploaded for this test') - }) + if template.requires_attachment and not self.attachment: + raise ValidationError({ + 'attachment': _('Attachment must be uploaded for this test') + }) - if choices := template.get_choices(): - if self.value not in choices: - raise ValidationError({'value': _('Invalid value for this test')}) + if choices := template.get_choices(): + if self.value not in choices: + raise ValidationError({'value': _('Invalid value for this test')}) @property def key(self): diff --git a/src/backend/InvenTree/stock/serializers.py b/src/backend/InvenTree/stock/serializers.py index 4b31b64a33..3fc8bd477a 100644 --- a/src/backend/InvenTree/stock/serializers.py +++ b/src/backend/InvenTree/stock/serializers.py @@ -268,13 +268,13 @@ class StockItemTestResultSerializer( ).first(): data['template'] = template - else: + elif get_global_setting('TEST_UPLOAD_CREATE_TEMPLATE', False): logger.debug( "No matching test template found for '%s' - creating a new template", test_name, ) - # Create a new test template based on the provided dasta + # Create a new test template based on the provided data data['template'] = part_models.PartTestTemplate.objects.create( part=stock_item.part, test_name=test_name ) diff --git a/src/backend/InvenTree/stock/test_api.py b/src/backend/InvenTree/stock/test_api.py index c49c4b3319..9cce95ec4b 100644 --- a/src/backend/InvenTree/stock/test_api.py +++ b/src/backend/InvenTree/stock/test_api.py @@ -1716,6 +1716,14 @@ class StockTestResultTest(StockAPITestCase): 'notes': 'I guess there was just too much pressure?', } + # First, test with TEST_UPLOAD_CREATE_TEMPLATE set to False + InvenTreeSetting.set_setting('TEST_UPLOAD_CREATE_TEMPLATE', False, self.user) + + response = self.post(url, data, expected_code=400) + + # Again, with the setting enabled + InvenTreeSetting.set_setting('TEST_UPLOAD_CREATE_TEMPLATE', True, self.user) + response = self.post(url, data, expected_code=201) # Check that a new test template has been created diff --git a/src/backend/InvenTree/templates/InvenTree/settings/stock.html b/src/backend/InvenTree/templates/InvenTree/settings/stock.html index 89dd5e95b9..fbc51ffe60 100644 --- a/src/backend/InvenTree/templates/InvenTree/settings/stock.html +++ b/src/backend/InvenTree/templates/InvenTree/settings/stock.html @@ -25,6 +25,7 @@ {% include "InvenTree/settings/setting.html" with key="STOCK_ENFORCE_BOM_INSTALLATION" icon="fa-check-circle" %} {% include "InvenTree/settings/setting.html" with key="STOCK_ALLOW_OUT_OF_STOCK_TRANSFER" icon="fa-dolly" %} {% include "InvenTree/settings/setting.html" with key="TEST_STATION_DATA" icon="fa-network-wired" %} + {% include "InvenTree/settings/setting.html" with key="TEST_UPLOAD_CREATE_TEMPLATE" %} diff --git a/src/frontend/src/pages/Index/Settings/SystemSettings.tsx b/src/frontend/src/pages/Index/Settings/SystemSettings.tsx index fe4d7a49e4..fa42dd2109 100644 --- a/src/frontend/src/pages/Index/Settings/SystemSettings.tsx +++ b/src/frontend/src/pages/Index/Settings/SystemSettings.tsx @@ -221,7 +221,8 @@ export default function SystemSettings() { 'STOCK_SHOW_INSTALLED_ITEMS', 'STOCK_ENFORCE_BOM_INSTALLATION', 'STOCK_ALLOW_OUT_OF_STOCK_TRANSFER', - 'TEST_STATION_DATA' + 'TEST_STATION_DATA', + 'TEST_UPLOAD_CREATE_TEMPLATE' ]} /> )