diff --git a/InvenTree/stock/api.py b/InvenTree/stock/api.py index 9d2860e41d..e287441382 100644 --- a/InvenTree/stock/api.py +++ b/InvenTree/stock/api.py @@ -12,39 +12,39 @@ from django.conf.urls import url, include from django.http import JsonResponse from django.db.models import Q from django.db import transaction +from django.utils.translation import ugettext_lazy as _ + +from django_filters.rest_framework import DjangoFilterBackend +from django_filters import rest_framework as rest_filters from rest_framework import status from rest_framework.serializers import ValidationError from rest_framework.response import Response from rest_framework import generics, filters -from django_filters.rest_framework import DjangoFilterBackend -from django_filters import rest_framework as rest_filters - -from .models import StockLocation, StockItem -from .models import StockItemTracking -from .models import StockItemAttachment -from .models import StockItemTestResult - -from part.models import BomItem, Part, PartCategory -from part.serializers import PartBriefSerializer +import common.settings +import common.models from company.models import Company, SupplierPart from company.serializers import CompanySerializer, SupplierPartSerializer +from InvenTree.helpers import str2bool, isNull, extract_serial_numbers +from InvenTree.api import AttachmentMixin +from InvenTree.filters import InvenTreeOrderingFilter + from order.models import PurchaseOrder from order.models import SalesOrder, SalesOrderAllocation from order.serializers import POSerializer -import common.settings -import common.models +from part.models import BomItem, Part, PartCategory +from part.serializers import PartBriefSerializer +from stock.models import StockLocation, StockItem +from stock.models import StockItemTracking +from stock.models import StockItemAttachment +from stock.models import StockItemTestResult import stock.serializers as StockSerializers -from InvenTree.helpers import str2bool, isNull, extract_serial_numbers -from InvenTree.api import AttachmentMixin -from InvenTree.filters import InvenTreeOrderingFilter - class StockDetail(generics.RetrieveUpdateDestroyAPIView): """ API detail endpoint for Stock object @@ -411,7 +411,12 @@ class StockList(generics.ListCreateAPIView): # Check if a set of serial numbers was provided serial_numbers = data.get('serial_numbers', '') - quantity = data['quantity'] + quantity = data.get('quantity', None) + + if quantity is None: + raise ValidationError({ + 'quantity': _('Quantity is required'), + }) notes = data.get('notes', '') diff --git a/InvenTree/stock/test_api.py b/InvenTree/stock/test_api.py index d07c35aaf7..422f9f11ab 100644 --- a/InvenTree/stock/test_api.py +++ b/InvenTree/stock/test_api.py @@ -364,24 +364,22 @@ class StockItemTest(StockAPITestCase): 'part': 1, 'location': 1, }, - expected_code=201, + expected_code=400 ) - # Item should have been created with default quantity - self.assertEqual(response.data['quantity'], 1) + self.assertIn('Quantity is required', str(response.data)) # POST with quantity and part and location - response = self.client.post( + response = self.post( self.list_url, data={ 'part': 1, 'location': 1, 'quantity': 10, - } + }, + expected_code=201 ) - self.assertEqual(response.status_code, status.HTTP_201_CREATED) - def test_default_expiry(self): """ Test that the "default_expiry" functionality works via the API. diff --git a/InvenTree/stock/test_views.py b/InvenTree/stock/test_views.py index e210a6ac95..d3019ee541 100644 --- a/InvenTree/stock/test_views.py +++ b/InvenTree/stock/test_views.py @@ -60,70 +60,6 @@ class StockListTest(StockViewTestCase): self.assertEqual(response.status_code, 200) -class StockItemTest(StockViewTestCase): - """" Tests for StockItem views """ - - def test_qr_code(self): - # QR code for a valid item - response = self.client.get(reverse('stock-item-qr', args=(1,)), HTTP_X_REQUESTED_WITH='XMLHttpRequest') - self.assertEqual(response.status_code, 200) - - # QR code for an invalid item - response = self.client.get(reverse('stock-item-qr', args=(9999,)), HTTP_X_REQUESTED_WITH='XMLHttpRequest') - self.assertEqual(response.status_code, 200) - - def test_create_item(self): - """ - Test creation of StockItem - """ - - url = reverse('stock-item-create') - - response = self.client.get(url, {'part': 1}, HTTP_X_REQUESTED_WITH='XMLHttpRequest') - self.assertEqual(response.status_code, 200) - - response = self.client.get(url, {'part': 999}, HTTP_X_REQUESTED_WITH='XMLHttpRequest') - self.assertEqual(response.status_code, 200) - - # Copy from a valid item, valid location - response = self.client.get(url, {'location': 1, 'copy': 1}, HTTP_X_REQUESTED_WITH='XMLHttpRequest') - self.assertEqual(response.status_code, 200) - - # Copy from an invalid item, invalid location - response = self.client.get(url, {'location': 999, 'copy': 9999}, HTTP_X_REQUESTED_WITH='XMLHttpRequest') - self.assertEqual(response.status_code, 200) - - def test_create_stock_with_expiry(self): - """ - Test creation of stock item of a part with an expiry date. - The initial value for the "expiry_date" field should be pre-filled, - and should be in the future! - """ - - # First, ensure that the expiry date feature is enabled! - InvenTreeSetting.set_setting('STOCK_ENABLE_EXPIRY', True, self.user) - - url = reverse('stock-item-create') - - response = self.client.get(url, {'part': 25}, HTTP_X_REQUESTED_WITH='XMLHttpRequest') - - self.assertEqual(response.status_code, 200) - - # We are expecting 10 days in the future - expiry = datetime.now().date() + timedelta(10) - - expected = f'name=\\\\"expiry_date\\\\" value=\\\\"{expiry.isoformat()}\\\\"' - - self.assertIn(expected, str(response.content)) - - # Now check with a part which does *not* have a default expiry period - response = self.client.get(url, {'part': 1}, HTTP_X_REQUESTED_WITH='XMLHttpRequest') - - expected = 'name=\\\\"expiry_date\\\\" placeholder=\\\\"\\\\"' - - self.assertIn(expected, str(response.content)) - - class StockOwnershipTest(StockViewTestCase): """ Tests for stock ownership views """ diff --git a/InvenTree/stock/urls.py b/InvenTree/stock/urls.py index 7c35aebcaf..b28104f388 100644 --- a/InvenTree/stock/urls.py +++ b/InvenTree/stock/urls.py @@ -45,8 +45,6 @@ stock_urls = [ # Stock location url(r'^location/', include(location_urls)), - url(r'^item/new/?', views.StockItemCreate.as_view(), name='stock-item-create'), - url(r'^item/uninstall/', views.StockItemUninstall.as_view(), name='stock-item-uninstall'), url(r'^track/', include(stock_tracking_urls)),