From 1df42b2397577268768ce54410d04c42661ab34a Mon Sep 17 00:00:00 2001 From: Oliver Date: Sat, 28 Apr 2018 00:06:39 +1000 Subject: [PATCH] Sanity checking for StockItem - If a SupplierPart is selected, it must point to the same Part type as the Part field! --- InvenTree/stock/models.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index f77550b609..1c765180c1 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -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)