Allow files to be uploaded alongside a test report

- Attach the file to the stock item
- Link the uploaded attachment to the test report
This commit is contained in:
Oliver Walters 2020-05-16 22:53:23 +10:00
parent 247cfcc514
commit 45556058d2
3 changed files with 77 additions and 3 deletions

View File

@ -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.

View File

@ -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 """

View File

@ -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)