From fc6cad475adc63e974301e96e0090711549c6587 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 18 May 2020 19:11:43 +1000 Subject: [PATCH] Add validation for StockItemTestResult based on the matching PartTestTemplate --- InvenTree/part/forms.py | 5 ++++- InvenTree/stock/models.py | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/InvenTree/part/forms.py b/InvenTree/part/forms.py index a276d62c54..c86027118c 100644 --- a/InvenTree/part/forms.py +++ b/InvenTree/part/forms.py @@ -39,7 +39,10 @@ class EditPartTestTemplateForm(HelperForm): fields = [ 'part', 'test_name', - 'required' + 'description', + 'required', + 'requires_value', + 'requires_attachment', ] diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index c50a58bea9..bb3621a7ac 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -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,