mirror of
https://github.com/inventree/InvenTree.git
synced 2025-07-05 21:20:56 +00:00
API improvements for Stock app
This commit is contained in:
@ -1,22 +1,55 @@
|
||||
from rest_framework import generics
|
||||
from rest_framework import generics, permissions
|
||||
import django_filters
|
||||
|
||||
from .models import StockLocation, StockItem
|
||||
|
||||
from .serializers import StockItemSerializer, LocationDetailSerializer
|
||||
|
||||
|
||||
class PartStockDetail(generics.ListCreateAPIView):
|
||||
""" Return a list of all stockitems for a given part
|
||||
"""
|
||||
class StockDetail(generics.RetrieveUpdateDestroyAPIView):
|
||||
|
||||
queryset = StockItem.objects.all()
|
||||
serializer_class = StockItemSerializer
|
||||
|
||||
|
||||
class StockFilter(django_filters.rest_framework.FilterSet):
|
||||
min_stock = django_filters.NumberFilter(name='quantity', lookup_expr='gte')
|
||||
max_stock = django_filters.NumberFilter(name='quantity', lookup_expr='lte')
|
||||
|
||||
class Meta:
|
||||
model = StockItem
|
||||
fields = ['quantity']
|
||||
|
||||
class StockList(generics.ListCreateAPIView):
|
||||
|
||||
serializer_class = StockItemSerializer
|
||||
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
|
||||
filter_backends = (django_filters.rest_framework.DjangoFilterBackend,)
|
||||
filter_class = StockFilter
|
||||
|
||||
def get_queryset(self):
|
||||
part_id = self.kwargs['part']
|
||||
return StockItem.objects.filter(part=part_id)
|
||||
items = StockItem.objects.all()
|
||||
|
||||
# Specify a particular part
|
||||
part_id = self.request.query_params.get('part', None)
|
||||
if part_id:
|
||||
items = items.filter(part=part_id)
|
||||
|
||||
class LocationDetail(generics.RetrieveAPIView):
|
||||
# Specify a particular location
|
||||
loc_id = self.request.query_params.get('location', None)
|
||||
|
||||
if loc_id:
|
||||
items = items.filter(location=loc_id)
|
||||
|
||||
return items
|
||||
|
||||
def create(self, request, *args, **kwargs):
|
||||
# If the PART parameter is passed in the URL, use that
|
||||
part_id = self.request.query_params.get('part', None)
|
||||
if part_id:
|
||||
request.data['part'] = part_id
|
||||
return super(StockList, self).create(request, *args, **kwargs)
|
||||
|
||||
class LocationDetail(generics.RetrieveUpdateDestroyAPIView):
|
||||
""" Return information on a specific stock location
|
||||
"""
|
||||
|
||||
@ -24,10 +57,27 @@ class LocationDetail(generics.RetrieveAPIView):
|
||||
serializer_class = LocationDetailSerializer
|
||||
|
||||
|
||||
class LocationList(generics.ListAPIView):
|
||||
class LocationList(generics.ListCreateAPIView):
|
||||
""" Return a list of top-level locations
|
||||
Locations are considered "top-level" if they do not have a parent
|
||||
"""
|
||||
|
||||
queryset = StockLocation.objects.filter(parent=None)
|
||||
def get_queryset(self):
|
||||
params = self.request.query_params
|
||||
|
||||
locations = StockLocation.objects.all()
|
||||
|
||||
parent_id = params.get('parent', None)
|
||||
|
||||
if parent_id and parent_id.lower() in ['none', 'false', 'null', 'top']:
|
||||
locations = locations.filter(parent=None)
|
||||
else:
|
||||
try:
|
||||
parent_id_num = int(parent_id)
|
||||
locations = locations.filter(parent=parent_id_num)
|
||||
except:
|
||||
pass
|
||||
|
||||
return locations
|
||||
|
||||
serializer_class = LocationDetailSerializer
|
||||
|
Reference in New Issue
Block a user