Include quantity currently being build in Part API

This commit is contained in:
Oliver Walters 2020-03-26 17:43:02 +11:00
parent 41bbbdcd43
commit 6a78f6d451
2 changed files with 12 additions and 5 deletions

View File

@ -25,7 +25,7 @@ from .models import PartParameter, PartParameterTemplate
from . import serializers as part_serializers
from InvenTree.status_codes import OrderStatus, StockStatus
from InvenTree.status_codes import OrderStatus, StockStatus, BuildStatus
from InvenTree.views import TreeSerializer
from InvenTree.helpers import str2bool
@ -162,6 +162,9 @@ class PartList(generics.ListCreateAPIView):
# "on_order" items should only sum orders which are currently outstanding
order_filter = Q(supplier_parts__purchase_order_line_items__order__status__in=OrderStatus.OPEN)
# "building" should only reference builds which are active
build_filter = Q(builds__status__in=BuildStatus.ACTIVE_CODES)
# Set of fields we wish to serialize
data = queryset.values(
'pk',
@ -183,11 +186,10 @@ class PartList(generics.ListCreateAPIView):
).annotate(
# Quantity of items which are "in stock"
in_stock=Sum('stock_items__quantity', filter=stock_filter),
on_order=Sum('supplier_parts__purchase_order_line_items__quantity', filter=order_filter)
on_order=Sum('supplier_parts__purchase_order_line_items__quantity', filter=order_filter),
building=Sum('builds__quantity', filter=build_filter),
)
# TODO - Annotate total being built
# Reduce the number of lookups we need to do for the part categories
categories = {}

View File

@ -569,7 +569,12 @@ class Part(models.Model):
""" Return the current number of parts currently being built
"""
return sum([b.quantity for b in self.active_builds])
quantity = self.active_builds.aggregate(quantity=Sum('quantity'))['quantity']
if quantity is None:
quantity = 0
return quantity
@property
def build_allocation(self):