Add ability to filter stock by variants for a templated part

This commit is contained in:
Oliver Walters 2019-05-25 23:26:46 +10:00
parent 0e684071fa
commit 75d38489d7

View File

@ -11,7 +11,7 @@ from django.urls import reverse
from .models import StockLocation, StockItem
from .models import StockItemTracking
from part.models import PartCategory
from part.models import Part, PartCategory
from .serializers import StockItemSerializer, StockQuantitySerializer
from .serializers import LocationSerializer
@ -263,11 +263,28 @@ class StockList(generics.ListCreateAPIView):
we may wish to also request stock items from all child locations.
"""
# Start with all objects
stock_list = StockItem.objects.all()
# Does the client wish to filter by the Part ID?
part_id = self.request.query_params.get('part', None)
if part_id:
try:
part = Part.objects.get(pk=part_id)
# If the part is a Template part, select stock items for any "variant" parts under that template
if part.has_variants:
stock_list = stock_list.filter(part__in=[part.id for part in Part.objects.filter(variant_of=part_id)])
else:
stock_list = stock_list.filter(part=part_id)
except Part.DoesNotExist:
pass
# Does the client wish to filter by stock location?
loc_id = self.request.query_params.get('location', None)
# Start with all objects
stock_list = StockItem.objects.all()
if loc_id:
try:
@ -312,7 +329,6 @@ class StockList(generics.ListCreateAPIView):
]
filter_fields = [
'part',
'supplier_part',
'customer',
'belongs_to',