2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-05-01 13:06:45 +00:00

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
This commit is contained in:
Oliver 2024-09-05 14:47:22 +10:00 committed by GitHub
parent e3205184be
commit 9b272bd60f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 37 additions and 21 deletions

View File

@ -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

View File

@ -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'

View File

@ -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):

View File

@ -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
)

View File

@ -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

View File

@ -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" %}
</tbody>
</table>

View File

@ -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'
]}
/>
)