Catch BuildItem errors in clean()

This commit is contained in:
Oliver Walters 2019-04-30 16:35:55 +10:00
parent fbd5a2a270
commit 6961d1ec68

View File

@ -147,15 +147,25 @@ class BuildItem(models.Model):
The following checks are performed: The following checks are performed:
- StockItem.part must be in the BOM of the Part object referenced by Build - StockItem.part must be in the BOM of the Part object referenced by Build
- Allocation quantity cannot exceed available quantity
""" """
if self.stock_item.part not in self.build.part.required_parts(): super(BuildItem, self).clean()
print('stock_item:', self.stock_item.part)
for p in self.build.part.bom_items.all(): errors = {}
print('bom_part:', p)
raise ValidationError( if self.stock_item is not None and self.stock_item.part is not None:
{'stock_item': _("Selected stock item not found in BOM for part '{p}'".format(p=str(self.build.part)))} if self.stock_item.part not in self.build.part.required_parts():
) errors['stock_item'] = _("Selected stock item not found in BOM for part '{p}'".format(p=str(self.build.part)))
if self.stock_item is not None and self.quantity > self.stock_item.quantity:
errors['quantity'] = _("Allocated quantity ({n}) must not exceed available quantity ({q})".format(
n=self.quantity,
q=self.stock_item.quantity
))
if len(errors) > 0:
raise ValidationError(errors)
build = models.ForeignKey( build = models.ForeignKey(
Build, Build,