Merge pull request #818 from SchrodingersGat/test-features

Enable attachments to be uploaded via the API
This commit is contained in:
Oliver 2020-05-19 17:03:05 +10:00 committed by GitHub
commit 6b73c9e020
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1102,20 +1102,16 @@ class StockItemTestResult(models.Model):
date: Date the test result was recorded date: Date the test result was recorded
""" """
def save(self, *args, **kwargs):
super().clean()
super().validate_unique()
super().save(*args, **kwargs)
def clean(self): def clean(self):
super().clean() super().clean()
# If an attachment is linked to this result, the attachment must also point to the item
try:
if self.attachment:
if not self.attachment.stock_item == self.stock_item:
raise ValidationError({
'attachment': _("Test result attachment must be linked to the same StockItem"),
})
except (StockItem.DoesNotExist, StockItemAttachment.DoesNotExist):
pass
# If this test result corresponds to a template, check the requirements of the template # If this test result corresponds to a template, check the requirements of the template
key = helpers.generateTestKey(self.test) key = helpers.generateTestKey(self.test)
@ -1130,14 +1126,27 @@ class StockItemTestResult(models.Model):
"value": _("Value must be provided for this test"), "value": _("Value must be provided for this test"),
}) })
"""
TODO: Re-introduce this at a later stage, it is buggy when uplaoding an attachment via the API
if template.requires_attachment: if template.requires_attachment:
if not self.attachment: if not self.attachment:
raise ValidationError({ raise ValidationError({
"attachment": _("Attachment must be uploaded for this test"), "attachment": _("Attachment must be uploaded for this test"),
}) })
"""
break break
# If an attachment is linked to this result, the attachment must also point to the item
try:
if self.attachment:
if not self.attachment.stock_item == self.stock_item:
raise ValidationError({
'attachment': _("Test result attachment must be linked to the same StockItem"),
})
except (StockItem.DoesNotExist, StockItemAttachment.DoesNotExist):
pass
stock_item = models.ForeignKey( stock_item = models.ForeignKey(
StockItem, StockItem,
on_delete=models.CASCADE, on_delete=models.CASCADE,