Annotate "in_stock" quantity to SupplierPart API (#3335)

* Annotate "in_stock" quantity to SupplierPart API

* Increment API version
This commit is contained in:
Oliver 2022-07-15 17:37:02 +10:00 committed by GitHub
parent 739489840b
commit 653dcd4526
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 1 deletions

View File

@ -2,11 +2,14 @@
# InvenTree API version
INVENTREE_API_VERSION = 64
INVENTREE_API_VERSION = 65
"""
Increment this API version number whenever there is a significant change to the API that any clients need to know about
v65 -> 2022-07-15 : https://github.com/inventree/InvenTree/pull/3335
- Annotates 'in_stock' quantity to the SupplierPart API
v64 -> 2022-07-08 : https://github.com/inventree/InvenTree/pull/3310
- Annotate 'on_order' quantity to BOM list API
- Allow BOM List API endpoint to be filtered by "on_order" parameter

View File

@ -263,6 +263,13 @@ class SupplierPartList(ListCreateDestroyAPIView):
queryset = SupplierPart.objects.all()
def get_queryset(self, *args, **kwargs):
"""Return annotated queryest object for the SupplierPart list"""
queryset = super().get_queryset(*args, **kwargs)
queryset = SupplierPartSerializer.annotate_queryset(queryset)
return queryset
def filter_queryset(self, queryset):
"""Custom filtering for the queryset."""
queryset = super().filter_queryset(queryset)

View File

@ -5,6 +5,7 @@ from django.utils.translation import gettext_lazy as _
from rest_framework import serializers
from sql_util.utils import SubqueryCount
import part.filters
from common.settings import currency_code_default, currency_code_mappings
from InvenTree.serializers import (InvenTreeAttachmentSerializer,
InvenTreeDecimalField,
@ -199,6 +200,9 @@ class ManufacturerPartParameterSerializer(InvenTreeModelSerializer):
class SupplierPartSerializer(InvenTreeModelSerializer):
"""Serializer for SupplierPart object."""
# Annotated field showing total in-stock quantity
in_stock = serializers.FloatField(read_only=True)
part_detail = PartBriefSerializer(source='part', many=False, read_only=True)
supplier_detail = CompanyBriefSerializer(source='supplier', many=False, read_only=True)
@ -249,6 +253,7 @@ class SupplierPartSerializer(InvenTreeModelSerializer):
'available',
'availability_updated',
'description',
'in_stock',
'link',
'manufacturer',
'manufacturer_detail',
@ -270,6 +275,20 @@ class SupplierPartSerializer(InvenTreeModelSerializer):
'availability_updated',
]
@staticmethod
def annotate_queryset(queryset):
"""Annotate the SupplierPart queryset with extra fields:
Fields:
in_stock: Current stock quantity for each SupplierPart
"""
queryset = queryset.annotate(
in_stock=part.filters.annotate_total_stock(reference='part__')
)
return queryset
def update(self, supplier_part, data):
"""Custom update functionality for the serializer"""

View File

@ -840,6 +840,7 @@ function loadSupplierPartTable(table, url, options) {
queryParams: filters,
name: 'supplierparts',
groupBy: false,
sortable: true,
formatNoMatches: function() {
return '{% trans "No supplier parts found" %}';
},
@ -957,6 +958,11 @@ function loadSupplierPartTable(table, url, options) {
title: '{% trans "Packaging" %}',
sortable: false,
},
{
field: 'in_stock',
title: '{% trans "In Stock" %}',
sortable: true,
},
{
field: 'available',
title: '{% trans "Available" %}',