From ecfde468383ec336fcfd434ff3a9622290e0c195 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sat, 25 May 2019 21:57:59 +1000 Subject: [PATCH 1/3] Fix error in BomItem creation --- InvenTree/part/models.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 8383431caa..385bae7bf4 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -858,13 +858,19 @@ class BomItem(models.Model): """ # A part cannot refer to itself in its BOM - if self.part == self.sub_part: - raise ValidationError({'sub_part': _('Part cannot be added to its own Bill of Materials')}) - - # Test for simple recursion - for item in self.sub_part.bom_items.all(): - if self.part == item.sub_part: - raise ValidationError({'sub_part': _("Part '{p1}' is used in BOM for '{p2}' (recursive)".format(p1=str(self.part), p2=str(self.sub_part)))}) + try: + if self.sub_part is not None and self.part is not None: + if self.part == self.sub_part: + raise ValidationError({'sub_part': _('Part cannot be added to its own Bill of Materials')}) + + # Test for simple recursion + for item in self.sub_part.bom_items.all(): + if self.part == item.sub_part: + raise ValidationError({'sub_part': _("Part '{p1}' is used in BOM for '{p2}' (recursive)".format(p1=str(self.part), p2=str(self.sub_part)))}) + + except Part.DoesNotExist: + # A blank Part will be caught elsewhere + pass class Meta: verbose_name = "BOM Item" From 2760efac18c923b92497c55a7342f44bb7e5cdbd Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sat, 25 May 2019 22:01:30 +1000 Subject: [PATCH 2/3] Fix similar error for Build object --- InvenTree/build/models.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/InvenTree/build/models.py b/InvenTree/build/models.py index 92f04bdcc6..763686b765 100644 --- a/InvenTree/build/models.py +++ b/InvenTree/build/models.py @@ -368,15 +368,21 @@ class BuildItem(models.Model): errors = {} - if self.stock_item is not None and self.stock_item.part is not None: + try: 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=self.build.part.full_name))] - 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 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 + ))] + + except StockItem.DoesNotExist: + pass + + except Part.DoesNotExist: + pass if len(errors) > 0: raise ValidationError(errors) From 9f4105e2e48a24fb57a7cb8b5b2794f2bd6a725a Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sat, 25 May 2019 22:02:36 +1000 Subject: [PATCH 3/3] PEP fix --- InvenTree/build/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/InvenTree/build/models.py b/InvenTree/build/models.py index 763686b765..b5f5aa10eb 100644 --- a/InvenTree/build/models.py +++ b/InvenTree/build/models.py @@ -17,7 +17,7 @@ from django.db.models import Sum from django.core.validators import MinValueValidator from stock.models import StockItem -from part.models import BomItem +from part.models import Part, BomItem class Build(models.Model):