Sanity checking for StockItem

- If a SupplierPart is selected, it must point to the same Part type as the Part field!
This commit is contained in:
Oliver 2018-04-28 00:06:39 +10:00
parent f49474ace5
commit 1df42b2397

View File

@ -1,7 +1,8 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.utils.translation import ugettext as _
from django.utils.translation import gettext_lazy as _
from django.core.exceptions import ValidationError
from django.db import models, transaction
from django.core.validators import MinValueValidator
@ -13,6 +14,8 @@ from datetime import datetime
from InvenTree.models import InvenTreeTree
from part.models import Part
class StockLocation(InvenTreeTree):
""" Organization tree for StockItem objects
@ -54,6 +57,26 @@ class StockItem(models.Model):
If a serial number is assigned, then StockItem cannot have a quantity other than 1
"""
def clean(self):
# The 'supplier_part' field must point to the same part!
try:
if self.supplier_part is not None:
if not self.supplier_part.part == self.part:
raise ValidationError({
'supplier_part': _(
"Part type ('{pf}') must be {pe}").format(
pf=str(self.supplier_part.part),
pe=str(self.part)
)
})
except Part.DoesNotExist:
# This gets thrown if self.supplier_part is null
# TODO - Find a test than can be perfomed...
pass
def get_absolute_url(self):
return '/stock/item/{id}/'.format(id=self.id)