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.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):
"""

View File

@ -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)