From 10762fc1cf085ec969f0f4702fc7e814ee0facef Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sat, 16 May 2020 08:55:19 +1000 Subject: [PATCH] Refactor tractor --- InvenTree/part/models.py | 4 ++-- InvenTree/stock/models.py | 47 ++++++++++++++------------------------- 2 files changed, 19 insertions(+), 32 deletions(-) diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 528d932cf0..0fdf764961 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -39,7 +39,7 @@ from InvenTree.models import InvenTreeTree, InvenTreeAttachment from InvenTree.fields import InvenTreeURLField from InvenTree.helpers import decimal2string, normalize -from InvenTree.status_codes import BuildStatus, StockStatus, PurchaseOrderStatus +from InvenTree.status_codes import BuildStatus, PurchaseOrderStatus from build import models as BuildModels from order import models as OrderModels @@ -241,7 +241,7 @@ class Part(MPTTModel): class MPTTMeta: # For legacy reasons the 'variant_of' field is used to indicate the MPTT parent - parent_attr='variant_of' + parent_attr = 'variant_of' def save(self, *args, **kwargs): """ diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index e7e4223f24..bf9c763ae7 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -149,9 +149,8 @@ class StockItem(MPTTModel): - Adds a transaction note when the item is first created. """ - # Query to look for duplicate serial numbers - parts = PartModels.Part.objects.filter(tree_id=self.part.tree_id) - stock = StockItem.objects.filter(part__in=parts, serial=self.serial) + self.validate_unique() + self.clean() if not self.pk: # StockItem has not yet been saved @@ -160,13 +159,6 @@ class StockItem(MPTTModel): # StockItem has already been saved add_note = False - stock = stock.exclude(pk=self.pk) - - if self.serial is not None: - # Check for presence of stock with same serial number - if stock.exists(): - raise ValidationError({"serial": _("StockItem with this serial number already exists")}) - user = kwargs.pop('user', None) add_note = add_note and kwargs.pop('note', True) @@ -193,30 +185,25 @@ class StockItem(MPTTModel): return self.serial is not None and self.quantity == 1 def validate_unique(self, exclude=None): + """ + Test that this StockItem is "unique". + If the StockItem is serialized, the same serial number. + cannot exist for the same part (or part tree). + """ + super(StockItem, self).validate_unique(exclude) - # If the Part object is a variant (of a template part), - # ensure that the serial number is unique - # across all variants of the same template part + if self.serial is not None: + # Query to look for duplicate serial numbers + parts = PartModels.Part.objects.filter(tree_id=self.part.tree_id) + stock = StockItem.objects.filter(part__in=parts, serial=self.serial) - print("validating...") - print(self.pk, self.serial) + # Exclude myself from the search + if self.pk is not None: + stock = stock.exclude(pk=self.pk) - try: - if self.serial is not None: - - parts = PartModels.Part.objects.filter(tree_id=self.part.tree_id) - stock = StockItem.objects.filter( - part__in=parts, - serial=self.serial, - ).exclude(pk=self.pk) - - if stock.exists(): - raise ValidationError({ - 'serial': _('A stock item with this serial number already exists for this part'), - }) - except PartModels.Part.DoesNotExist: - pass + if stock.exists(): + raise ValidationError({"serial": _("StockItem with this serial number already exists")}) def clean(self): """ Validate the StockItem object (separate to field validation)