Part list API adjustments

This commit is contained in:
Oliver Walters 2020-05-02 14:03:17 +10:00
parent 7fca496de8
commit cc11df917e
2 changed files with 13 additions and 13 deletions

View File

@ -6,7 +6,7 @@ Provides a JSON API for the Part app
from __future__ import unicode_literals
from django_filters.rest_framework import DjangoFilterBackend
from django.http import JsonResponse
from django.db.models import Q, F, Count
from rest_framework import status
@ -264,7 +264,15 @@ class PartList(generics.ListCreateAPIView):
part['category_detail'] = detail
return Response(data)
"""
Determine the response type based on the request.
a) For HTTP requests (e.g. via the browseable API) return a DRF response
b) For AJAX requests, simply return a JSON rendered response.
"""
if request.is_ajax():
return JsonResponse(data, safe=False)
else:
return Response(data)
def create(self, request, *args, **kwargs):
""" Override the default 'create' behaviour:
@ -288,6 +296,7 @@ class PartList(generics.ListCreateAPIView):
queryset = super().get_queryset(*args, **kwargs)
queryset = part_serializers.PartSerializer.prefetch_queryset(queryset)
queryset = part_serializers.PartSerializer.annotate_queryset(queryset)
return queryset

View File

@ -130,12 +130,7 @@ class PartSerializer(InvenTreeModelSerializer):
# Annotate the number total stock count
queryset = queryset.annotate(
in_stock=Coalesce(Sum('stock_items__quantity', filter=stock_filter, distinct=True), Decimal(0))
)
# Annotate the number of parts "on order"
# Total "on order" parts = "Quantity" - "Received" for each active purchase order
queryset = queryset.annotate(
in_stock=Coalesce(Sum('stock_items__quantity', filter=stock_filter, distinct=True), Decimal(0)),
ordering=Coalesce(Sum(
'supplier_parts__purchase_order_line_items__quantity',
filter=order_filter,
@ -144,11 +139,7 @@ class PartSerializer(InvenTreeModelSerializer):
'supplier_parts__purchase_order_line_items__received',
filter=order_filter,
distinct=True
), Decimal(0))
)
# Annotate number of parts being build
queryset = queryset.annotate(
), Decimal(0)),
building=Coalesce(
Sum('builds__quantity', filter=build_filter, distinct=True), Decimal(0)
)