Refactor tractor

This commit is contained in:
Oliver Walters 2020-05-16 08:55:19 +10:00
parent ea88a03b5a
commit 10762fc1cf
2 changed files with 19 additions and 32 deletions

View File

@ -39,7 +39,7 @@ from InvenTree.models import InvenTreeTree, InvenTreeAttachment
from InvenTree.fields import InvenTreeURLField from InvenTree.fields import InvenTreeURLField
from InvenTree.helpers import decimal2string, normalize 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 build import models as BuildModels
from order import models as OrderModels from order import models as OrderModels
@ -241,7 +241,7 @@ class Part(MPTTModel):
class MPTTMeta: class MPTTMeta:
# For legacy reasons the 'variant_of' field is used to indicate the MPTT parent # 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): def save(self, *args, **kwargs):
""" """

View File

@ -149,9 +149,8 @@ class StockItem(MPTTModel):
- Adds a transaction note when the item is first created. - Adds a transaction note when the item is first created.
""" """
# Query to look for duplicate serial numbers self.validate_unique()
parts = PartModels.Part.objects.filter(tree_id=self.part.tree_id) self.clean()
stock = StockItem.objects.filter(part__in=parts, serial=self.serial)
if not self.pk: if not self.pk:
# StockItem has not yet been saved # StockItem has not yet been saved
@ -160,13 +159,6 @@ class StockItem(MPTTModel):
# StockItem has already been saved # StockItem has already been saved
add_note = False 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) user = kwargs.pop('user', None)
add_note = add_note and kwargs.pop('note', True) 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 return self.serial is not None and self.quantity == 1
def validate_unique(self, exclude=None): 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) super(StockItem, self).validate_unique(exclude)
# If the Part object is a variant (of a template part), if self.serial is not None:
# ensure that the serial number is unique # Query to look for duplicate serial numbers
# across all variants of the same template part parts = PartModels.Part.objects.filter(tree_id=self.part.tree_id)
stock = StockItem.objects.filter(part__in=parts, serial=self.serial)
print("validating...") # Exclude myself from the search
print(self.pk, self.serial) if self.pk is not None:
stock = stock.exclude(pk=self.pk)
try: if stock.exists():
if self.serial is not None: raise ValidationError({"serial": _("StockItem with this serial number already exists")})
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
def clean(self): def clean(self):
""" Validate the StockItem object (separate to field validation) """ Validate the StockItem object (separate to field validation)