diff --git a/InvenTree/stock/api.py b/InvenTree/stock/api.py index f0fb8a5d7a..f6218547d0 100644 --- a/InvenTree/stock/api.py +++ b/InvenTree/stock/api.py @@ -687,6 +687,36 @@ class StockItemTestResultList(generics.ListCreateAPIView): 'value', ] + def perform_create(self, serializer): + """ + Create a new test result object. + + Also, check if an attachment was uploaded alongside the test result, + and save it to the database if it were. + """ + + # Capture the user information + test_result = serializer.save() + test_result.user = self.request.user + + # Check if a file has been attached to the request + attachment_file = self.request.FILES.get('attachment', None) + + if attachment_file: + # Create a new attachment associated with the stock item + attachment = StockItemAttachment( + attachment=attachment_file, + stock_item=test_result.stock_item, + user=test_result.user + ) + + attachment.save() + + # Link the attachment back to the test result + test_result.attachment = attachment + + test_result.save() + class StockTrackingList(generics.ListCreateAPIView): """ API endpoint for list view of StockItemTracking objects. diff --git a/InvenTree/stock/serializers.py b/InvenTree/stock/serializers.py index 8459aefd2a..783ff70d4b 100644 --- a/InvenTree/stock/serializers.py +++ b/InvenTree/stock/serializers.py @@ -212,6 +212,7 @@ class StockItemAttachmentSerializer(InvenTreeModelSerializer): 'stock_item', 'attachment', 'comment', + 'date', 'user', 'user_detail', ] @@ -235,6 +236,14 @@ class StockItemTestResultSerializer(InvenTreeModelSerializer): 'date' ] + read_only_fields = [ + 'pk', + 'stock_item', + 'attachment', + 'user', + 'date', + ] + class StockTrackingSerializer(InvenTreeModelSerializer): """ Serializer for StockItemTracking model """ diff --git a/InvenTree/stock/test_api.py b/InvenTree/stock/test_api.py index afb24487f1..cd598e8538 100644 --- a/InvenTree/stock/test_api.py +++ b/InvenTree/stock/test_api.py @@ -181,9 +181,12 @@ class StocktakeTest(StockAPITestCase): class StockTestResultTest(StockAPITestCase): + def get_url(self): + return reverse('api-stock-test-result-list') + def test_list(self): - url = reverse('api-stock-test-result-list') + url = self.get_url() response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_200_OK) @@ -193,10 +196,39 @@ class StockTestResultTest(StockAPITestCase): self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertGreaterEqual(len(response.data), 4) + def test_post_fail(self): + # Attempt to post a new test result without specifying required data + + url = self.get_url() + + response = self.client.post( + url, + data={ + 'test': 'A test', + 'result': True, + }, + format='json' + ) + + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + + # This one should pass! + response = self.client.post( + url, + data={ + 'test': 'A test', + 'stock_item': 105, + 'result': True, + }, + format='json' + ) + + self.assertEqual(response.status_code, status.HTTP_201_CREATED) + def test_post(self): # Test creation of a new test result - url = reverse('api-stock-test-result-list') + url = self.get_url() response = self.client.get(url) n = len(response.data) @@ -220,4 +252,7 @@ class StockTestResultTest(StockAPITestCase): response = self.client.get(url, data={'test': 'Checked Steam Valve'}) self.assertEqual(len(response.data), 1) - self.assertEqual(response.data[0]['value'], '150kPa') + + test = response.data[0] + self.assertEqual(test['value'], '150kPa') + self.assertEqual(test['user'], 1)