From 2b286c3b83c1eaec23c0bf180222a144851c5179 Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 7 May 2018 21:17:19 +1000 Subject: [PATCH] Bulk stocktake API - Pass list of pk/quantity dict objects --- InvenTree/stock/api.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/InvenTree/stock/api.py b/InvenTree/stock/api.py index 714259b5d1..183a209d1a 100644 --- a/InvenTree/stock/api.py +++ b/InvenTree/stock/api.py @@ -51,6 +51,43 @@ class StockFilter(FilterSet): fields = ['quantity', 'part', 'location'] +class StockStocktake(APIView): + + permission_classes = [ + permissions.IsAuthenticatedOrReadOnly, + ] + + def post(self, request, *args, **kwargs): + + data = request.data + + items = [] + + # Ensure each entry is valid + for entry in data: + if not 'pk' in entry: + raise ValidationError({'pk': 'Each entry must contain pk field'}) + if not 'quantity' in entry: + raise ValidationError({'quantity': 'Each entry must contain quantity field'}) + + item = {} + try: + item['item'] = StockItem.objects.get(pk=entry['pk']) + except StockItem.DoesNotExist: + raise ValidationError({'pk': 'No matching StockItem found for pk={pk}'.format(pk=entry['pk'])}) + try: + item['quantity'] = int(entry['quantity']) + except ValueError: + raise ValidationError({'quantity': 'Quantity must be an integer'}) + + items.append(item) + + for item in items: + item['item'].stocktake(item['quantity'], request.user) + + return Response({'success': 'success'}) + + class StockMove(APIView): permission_classes = [ @@ -221,6 +258,8 @@ stock_api_urls = [ url(r'location/(?P\d+)/', include(location_endpoints)), + url(r'stocktake/?', StockStocktake.as_view(), name='api-stock-stocktake'), + url(r'move/?', StockMove.as_view(), name='api-stock-move'), url(r'^tree/?', StockCategoryTree.as_view(), name='api-stock-tree'),