mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-29 20:16:44 +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']
|
part_id = kwargs['part']
|
||||||
template_id = kwargs['template']
|
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]
|
return params[0]
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
return super(PartParameterManager, self).create(*args, **kwargs)
|
return super(PartParameterManager, self).create(*args, **kwargs)
|
||||||
|
|
||||||
|
@ -50,11 +50,9 @@ class ProjectPartManager(models.Manager):
|
|||||||
project_id = kwargs['project']
|
project_id = kwargs['project']
|
||||||
part_id = kwargs['part']
|
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]
|
return project_parts[0]
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
return super(ProjectPartManager, self).create(*args, **kwargs)
|
return super(ProjectPartManager, self).create(*args, **kwargs)
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
from django.core.exceptions import ValidationError
|
||||||
from django.utils.translation import ugettext as _
|
from django.utils.translation import ugettext as _
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
@ -7,12 +8,37 @@ from supplier.models import Customer
|
|||||||
from part.models import Part, PartRevision
|
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):
|
class UniquePart(models.Model):
|
||||||
""" A unique instance of a Part object.
|
""" A unique instance of a Part object.
|
||||||
Used for tracking parts based on serial numbers,
|
Used for tracking parts based on serial numbers,
|
||||||
and tracking all events in the life of a part
|
and tracking all events in the life of a part
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
objects = UniquePartManager()
|
||||||
|
|
||||||
part = models.ForeignKey(Part, on_delete=models.CASCADE)
|
part = models.ForeignKey(Part, on_delete=models.CASCADE)
|
||||||
|
|
||||||
revision = models.ForeignKey(PartRevision,
|
revision = models.ForeignKey(PartRevision,
|
||||||
@ -50,6 +76,17 @@ class UniquePart(models.Model):
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.part.name
|
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):
|
class PartTrackingInfo(models.Model):
|
||||||
""" Single data-point in the life of a UniquePart
|
""" Single data-point in the life of a UniquePart
|
||||||
|
Loading…
x
Reference in New Issue
Block a user