mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-30 20:46:47 +00:00
Update the stock list API
- Custom data serialization is MUCH faster (~400ms compared to 3s) - Cache location queries - Flatten related field data - Update stock table javascript to match
This commit is contained in:
parent
f7d521ca97
commit
8c583750a2
@ -104,7 +104,6 @@ MIDDLEWARE = [
|
|||||||
'django.contrib.messages.middleware.MessageMiddleware',
|
'django.contrib.messages.middleware.MessageMiddleware',
|
||||||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||||
'InvenTree.middleware.AuthRequiredMiddleware',
|
'InvenTree.middleware.AuthRequiredMiddleware',
|
||||||
'InvenTree.middleware.QueryCountMiddleware',
|
|
||||||
]
|
]
|
||||||
|
|
||||||
if DEBUG:
|
if DEBUG:
|
||||||
|
@ -388,13 +388,22 @@ function loadStockTable(table, options) {
|
|||||||
groupByField: options.groupByField || 'part',
|
groupByField: options.groupByField || 'part',
|
||||||
groupByFormatter: function(field, id, data) {
|
groupByFormatter: function(field, id, data) {
|
||||||
|
|
||||||
|
var row = data[0];
|
||||||
|
|
||||||
if (field == 'Part') {
|
if (field == 'Part') {
|
||||||
return imageHoverIcon(data[0].part_detail.image_url) +
|
|
||||||
data[0].part_detail.full_name +
|
var name = row.part__IPN;
|
||||||
' <i>(' + data.length + ' items)</i>';
|
|
||||||
|
if (name) {
|
||||||
|
name += ' | ';
|
||||||
|
}
|
||||||
|
|
||||||
|
name += row.part__name;
|
||||||
|
|
||||||
|
return imageHoverIcon(row.part__image) + name + ' <i>(' + data.length + ' items)</i>';
|
||||||
}
|
}
|
||||||
else if (field == 'Description') {
|
else if (field == 'Description') {
|
||||||
return data[0].part_detail.description;
|
return row.part__description;
|
||||||
}
|
}
|
||||||
else if (field == 'Stock') {
|
else if (field == 'Stock') {
|
||||||
var stock = 0;
|
var stock = 0;
|
||||||
@ -419,7 +428,8 @@ function loadStockTable(table, options) {
|
|||||||
if (locations.length > 1) {
|
if (locations.length > 1) {
|
||||||
return "In " + locations.length + " locations";
|
return "In " + locations.length + " locations";
|
||||||
} else {
|
} else {
|
||||||
return renderLink(data[0].location_detail.pathstring, data[0].location_detail.url);
|
// A single location!
|
||||||
|
return renderLink(row.location__path, '/stock/location/' + row.location + '/')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -438,15 +448,24 @@ function loadStockTable(table, options) {
|
|||||||
visible: false,
|
visible: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
field: 'part_detail',
|
field: 'part__name',
|
||||||
title: 'Part',
|
title: 'Part',
|
||||||
sortable: true,
|
sortable: true,
|
||||||
formatter: function(value, row, index, field) {
|
formatter: function(value, row, index, field) {
|
||||||
return imageHoverIcon(value.image_url) + renderLink(value.full_name, value.url + 'stock/');
|
|
||||||
|
var name = row.part__IPN;
|
||||||
|
|
||||||
|
if (name) {
|
||||||
|
name += ' | ';
|
||||||
|
}
|
||||||
|
|
||||||
|
name += row.part__name;
|
||||||
|
|
||||||
|
return imageHoverIcon(row.part__image) + renderLink(name, '/part/' + row.part + '/stock/');
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
field: 'part_detail.description',
|
field: 'part__description',
|
||||||
title: 'Description',
|
title: 'Description',
|
||||||
sortable: true,
|
sortable: true,
|
||||||
},
|
},
|
||||||
@ -463,19 +482,19 @@ function loadStockTable(table, options) {
|
|||||||
val = '# ' + row.serial;
|
val = '# ' + row.serial;
|
||||||
}
|
}
|
||||||
|
|
||||||
var text = renderLink(val, row.url);
|
var text = renderLink(val, '/stock/item/' + row.pk + '/');
|
||||||
|
|
||||||
text = text + "<span class='badge'>" + row.status_text + "</span>";
|
text = text + "<span class='badge'>" + row.status_text + "</span>";
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
field: 'location_detail',
|
field: 'location__path',
|
||||||
title: 'Location',
|
title: 'Location',
|
||||||
sortable: true,
|
sortable: true,
|
||||||
formatter: function(value, row, index, field) {
|
formatter: function(value, row, index, field) {
|
||||||
if (value) {
|
if (value) {
|
||||||
return renderLink(value.pathstring, value.url);
|
return renderLink(value, '/stock/location/' + row.location + '/');
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return '<i>No stock location set</i>';
|
return '<i>No stock location set</i>';
|
||||||
|
@ -5,6 +5,7 @@ JSON API for the Stock app
|
|||||||
from django_filters.rest_framework import FilterSet, DjangoFilterBackend
|
from django_filters.rest_framework import FilterSet, DjangoFilterBackend
|
||||||
from django_filters import NumberFilter
|
from django_filters import NumberFilter
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
from django.conf.urls import url, include
|
from django.conf.urls import url, include
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
|
||||||
@ -22,6 +23,8 @@ from .serializers import StockTrackingSerializer
|
|||||||
from InvenTree.views import TreeSerializer
|
from InvenTree.views import TreeSerializer
|
||||||
from InvenTree.helpers import str2bool
|
from InvenTree.helpers import str2bool
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
from rest_framework.serializers import ValidationError
|
from rest_framework.serializers import ValidationError
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
@ -264,47 +267,43 @@ class StockList(generics.ListCreateAPIView):
|
|||||||
|
|
||||||
queryset = self.filter_queryset(self.get_queryset())
|
queryset = self.filter_queryset(self.get_queryset())
|
||||||
|
|
||||||
if str2bool(self.request.GET.get('aggregate', None)):
|
# Instead of using the DRF serializer to LIST,
|
||||||
# Aggregate stock by part type
|
# we will serialize the objects manually.
|
||||||
queryset = queryset.values(
|
# This is significantly faster
|
||||||
'part',
|
|
||||||
'part__name',
|
|
||||||
'part__image',
|
|
||||||
'location',
|
|
||||||
'location__name').annotate(
|
|
||||||
stock=Sum('quantity'),
|
|
||||||
items=Count('part'))
|
|
||||||
|
|
||||||
for result in queryset:
|
data = queryset.values(
|
||||||
# If there is only 1 stock item (which will be a lot of the time),
|
'pk',
|
||||||
# Add that data to the dict
|
'quantity',
|
||||||
if result['items'] == 1:
|
'serial',
|
||||||
items = StockItem.objects.filter(
|
'batch',
|
||||||
part=result['part'],
|
'status',
|
||||||
location=result['location']
|
'notes',
|
||||||
)
|
'location',
|
||||||
|
'location__name',
|
||||||
|
'part',
|
||||||
|
'part__IPN',
|
||||||
|
'part__name',
|
||||||
|
'part__description',
|
||||||
|
'part__image',
|
||||||
|
'part__category',
|
||||||
|
'part__category__name'
|
||||||
|
)
|
||||||
|
|
||||||
if items.count() == 1:
|
# Reduce the number of lookups we need to do for categories
|
||||||
result.pop('items')
|
# Cache location lookups for this query
|
||||||
|
locations = {}
|
||||||
|
|
||||||
item = items[0]
|
for item in data:
|
||||||
|
item['part__image'] = os.path.join(settings.MEDIA_URL, item['part__image'])
|
||||||
|
|
||||||
# Add in some extra information specific to this StockItem
|
loc_id = item['location']
|
||||||
result['pk'] = item.id
|
|
||||||
result['serial'] = item.serial
|
|
||||||
result['batch'] = item.batch
|
|
||||||
result['notes'] = item.notes
|
|
||||||
|
|
||||||
return Response(queryset)
|
if loc_id not in locations:
|
||||||
|
locations[loc_id] = StockLocation.objects.get(pk=loc_id).pathstring
|
||||||
|
|
||||||
|
item['location__path'] = locations[loc_id]
|
||||||
|
|
||||||
page = self.paginate_queryset(queryset)
|
return Response(data)
|
||||||
if page is not None:
|
|
||||||
serializer = self.get_serializer(page, many=True)
|
|
||||||
return self.get_paginated_response(serializer.data)
|
|
||||||
|
|
||||||
serializer = self.get_serializer(queryset, many=True)
|
|
||||||
return Response(serializer.data)
|
|
||||||
|
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
"""
|
"""
|
||||||
@ -366,6 +365,8 @@ class StockList(generics.ListCreateAPIView):
|
|||||||
'location'
|
'location'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
stock_list = stock_list.order_by('part__name')
|
||||||
|
|
||||||
return stock_list
|
return stock_list
|
||||||
|
|
||||||
serializer_class = StockItemSerializer
|
serializer_class = StockItemSerializer
|
||||||
|
Loading…
x
Reference in New Issue
Block a user