diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 5afa01a293..4edbf6f7f0 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -149,11 +149,9 @@ class PartParameterManager(models.Manager): part_id = kwargs['part'] template_id = kwargs['template'] - try: - params = self.filter(part=part_id, template=template_id) + params = self.filter(part=part_id, template=template_id) + if len(params) > 0: return params[0] - except: - pass return super(PartParameterManager, self).create(*args, **kwargs) diff --git a/InvenTree/project/models.py b/InvenTree/project/models.py index 2d7c110502..6cc8a01ce0 100644 --- a/InvenTree/project/models.py +++ b/InvenTree/project/models.py @@ -50,11 +50,9 @@ class ProjectPartManager(models.Manager): project_id = kwargs['project'] part_id = kwargs['part'] - try: - project_parts = self.filter(project=project_id, part=part_id) + project_parts = self.filter(project=project_id, part=part_id) + if len(project_parts) > 0: return project_parts[0] - except: - pass return super(ProjectPartManager, self).create(*args, **kwargs) diff --git a/InvenTree/track/models.py b/InvenTree/track/models.py index 389a0bea09..5236e7eaef 100644 --- a/InvenTree/track/models.py +++ b/InvenTree/track/models.py @@ -1,4 +1,5 @@ from __future__ import unicode_literals +from django.core.exceptions import ValidationError from django.utils.translation import ugettext as _ from django.db import models from django.contrib.auth.models import User @@ -7,12 +8,37 @@ from supplier.models import Customer from part.models import Part, PartRevision +class UniquePartManager(models.Manager): + """ Ensures UniqueParts are correctly handled + """ + + def create(self, *args, **kwargs): + + part_id = kwargs['part'] + sn = kwargs.get('serial', None) + + if not sn: + raise ValidationError(_("Serial number must be supplied")) + + if not isinstance(sn, int): + raise ValidationError(_("Serial number must be integer")) + + # Does a part already exists with this serial number? + parts = self.filter(part=part_id, serial=sn) + if len(parts) > 0: + raise ValidationError(_("Matching part and serial number found!")) + + return super(UniquePartManager, self).create(*args, **kwargs) + + class UniquePart(models.Model): """ A unique instance of a Part object. Used for tracking parts based on serial numbers, and tracking all events in the life of a part """ + objects = UniquePartManager() + part = models.ForeignKey(Part, on_delete=models.CASCADE) revision = models.ForeignKey(PartRevision, @@ -50,6 +76,17 @@ class UniquePart(models.Model): def __str__(self): return self.part.name + def save(self, *args, **kwargs): + + # Disallow saving a serial number that already exists + matches = UniquePart.objects.filter(serial=self.serial, part=self.part) + matches = matches.filter(~models.Q(id = self.id)) + + if len(matches) > 0: + raise ValidationError(_("Matching serial number already exists")) + + super(UniquePart, self).save(*args, **kwargs) + class PartTrackingInfo(models.Model): """ Single data-point in the life of a UniquePart