mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Prevent duplication of unique parts
This commit is contained in:
parent
6f31e3447c
commit
5b49cff09a
@ -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)
|
||||
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user