InvenTree/InvenTree/part/models.py

153 lines
4.3 KiB
Python
Raw Normal View History

from __future__ import unicode_literals
2017-03-29 12:19:53 +00:00
from django.utils.translation import ugettext as _
from django.db import models
from django.db.models import Sum
2017-04-16 07:05:02 +00:00
from django.core.validators import MinValueValidator
from InvenTree.models import InvenTreeTree
#from django.db.models.signals.pre_delete
#from django.dispatch import receiver
2017-03-28 12:17:56 +00:00
2017-03-28 12:31:41 +00:00
class PartCategory(InvenTreeTree):
2017-03-27 11:55:21 +00:00
""" PartCategory provides hierarchical organization of Part objects.
"""
2017-03-29 12:19:53 +00:00
class Meta:
verbose_name = "Part Category"
verbose_name_plural = "Part Categories"
2017-03-29 12:19:53 +00:00
2017-04-10 23:41:03 +00:00
@property
def parts(self):
2017-04-11 13:07:02 +00:00
return self.part_set.all()
2017-04-10 23:41:03 +00:00
"""
@receiver(pre_delete, sender=PartCategory)
def reset_tag(sender, **kwargs):
cat = kwargs['instance']
for book in books.filter(tag=tag):
book.tag = book.author.tags.first()
book.save()
"""
2017-03-29 12:19:53 +00:00
class Part(models.Model):
""" Represents an abstract part
Parts can be "stocked" in multiple warehouses,
and can be combined to form other parts
"""
2017-03-29 12:19:53 +00:00
2017-04-01 02:31:48 +00:00
# Short name of the part
name = models.CharField(max_length=100)
2017-04-01 02:31:48 +00:00
# Longer description of the part (optional)
description = models.CharField(max_length=250, blank=True)
2017-04-01 02:31:48 +00:00
# Internal Part Number (optional)
IPN = models.CharField(max_length=100, blank=True)
2017-04-01 02:31:48 +00:00
# Provide a URL for an external link
URL = models.URLField(blank=True)
2017-04-01 02:31:48 +00:00
# Part category - all parts must be assigned to a category
category = models.ForeignKey(PartCategory, related_name='parts',
null=True, blank=True,
on_delete=models.SET_NULL)
2017-04-01 02:31:48 +00:00
# Minimum "allowed" stock level
2017-04-16 07:05:02 +00:00
minimum_stock = models.PositiveIntegerField(default=0, validators=[MinValueValidator(0)])
2017-04-01 02:31:48 +00:00
# Units of quantity for this part. Default is "pcs"
2017-03-28 06:49:01 +00:00
units = models.CharField(max_length=20, default="pcs", blank=True)
2017-04-01 02:31:48 +00:00
# Is this part "trackable"?
# Trackable parts can have unique instances which are assigned serial numbers
# and can have their movements tracked
trackable = models.BooleanField(default=False)
2017-03-29 12:19:53 +00:00
def __str__(self):
if self.IPN:
return "{name} ({ipn})".format(
2017-03-28 12:31:41 +00:00
ipn=self.IPN,
name=self.name)
else:
return self.name
2017-03-29 12:19:53 +00:00
class Meta:
verbose_name = "Part"
verbose_name_plural = "Parts"
unique_together = (("name", "category"),)
2017-03-29 12:19:53 +00:00
@property
2017-03-28 11:27:46 +00:00
def stock(self):
""" Return the total stock quantity for this part.
Part may be stored in multiple locations
"""
2017-03-29 12:19:53 +00:00
stocks = self.locations.all()
if len(stocks) == 0:
return 0
2017-03-29 12:19:53 +00:00
result = stocks.aggregate(total=Sum('quantity'))
return result['total']
2017-03-29 12:19:53 +00:00
@property
def bomItemCount(self):
return self.bom_items.all().count()
@property
def usedInCount(self):
return self.used_in.all().count()
"""
@property
def projects(self):
" Return a list of unique projects that this part is associated with.
2017-04-01 02:31:48 +00:00
A part may be used in zero or more projects.
"
2017-03-29 12:19:53 +00:00
project_ids = set()
project_parts = self.projectpart_set.all()
2017-03-29 12:19:53 +00:00
projects = []
2017-03-29 12:19:53 +00:00
for pp in project_parts:
if pp.project.id not in project_ids:
project_ids.add(pp.project.id)
projects.append(pp.project)
2017-03-29 12:19:53 +00:00
return projects
"""
2017-03-29 04:12:14 +00:00
2017-03-29 04:45:50 +00:00
class BomItem(models.Model):
""" A BomItem links a part to its component items.
A part can have a BOM (bill of materials) which defines
which parts are required (and in what quatity) to make it
"""
# A link to the parent part
# Each part will get a reverse lookup field 'bom_items'
part = models.ForeignKey(Part, on_delete=models.CASCADE, related_name='bom_items')
# A link to the child item (sub-part)
# Each part will get a reverse lookup field 'used_in'
sub_part = models.ForeignKey(Part, on_delete=models.CASCADE, related_name='used_in')
# Quantity required
quantity = models.PositiveIntegerField(default=1, validators=[MinValueValidator(0)])
class Meta:
verbose_name = "BOM Item"
# Prevent duplication of parent/child rows
unique_together = ('part', 'sub_part')
def __str__(self):
return "{par} -> {child} ({n})".format(
par=self.part.name,
child=self.sub_part.name,
n=self.quantity)