Add validation for StockItemTestResult based on the matching PartTestTemplate

This commit is contained in:
Oliver Walters 2020-05-18 19:11:43 +10:00
parent e8c402ecd9
commit fc6cad475a
2 changed files with 26 additions and 1 deletions

View File

@ -39,7 +39,10 @@ class EditPartTestTemplateForm(HelperForm):
fields = [
'part',
'test_name',
'required'
'description',
'required',
'requires_value',
'requires_attachment',
]

View File

@ -1115,6 +1115,28 @@ class StockItemTestResult(models.Model):
})
except (StockItem.DoesNotExist, StockItemAttachment.DoesNotExist):
pass
# If this test result corresponds to a template, check the requirements of the template
key = helpers.generateTestKey(self.test)
templates = self.stock_item.part.getTestTemplates()
for template in templates:
if key == template.key:
if template.requires_value:
if not self.value:
raise ValidationError({
"value": _("Value must be provided for this test"),
})
if template.requires_attachment:
if not self.attachment:
raise ValidationError({
"attachment": _("Attachment must be uploaded for this test"),
})
break
stock_item = models.ForeignKey(
StockItem,