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
""" """
super(BuildItem, self).clean()
errors = {}
if self.stock_item is not None and self.stock_item.part is not None:
if self.stock_item.part not in self.build.part.required_parts(): if self.stock_item.part not in self.build.part.required_parts():
print('stock_item:', self.stock_item.part) errors['stock_item'] = _("Selected stock item not found in BOM for part '{p}'".format(p=str(self.build.part)))
for p in self.build.part.bom_items.all():
print('bom_part:', p) if self.stock_item is not None and self.quantity > self.stock_item.quantity:
raise ValidationError( errors['quantity'] = _("Allocated quantity ({n}) must not exceed available quantity ({q})".format(
{'stock_item': _("Selected stock item not found in BOM for part '{p}'".format(p=str(self.build.part)))} n=self.quantity,
) q=self.stock_item.quantity
))
if len(errors) > 0:
raise ValidationError(errors)
build = models.ForeignKey( build = models.ForeignKey(
Build, Build,