diff --git a/InvenTree/stock/templates/stock/item_attachments.html b/InvenTree/stock/templates/stock/item_attachments.html
index 453ab7f09b..3861bebe6a 100644
--- a/InvenTree/stock/templates/stock/item_attachments.html
+++ b/InvenTree/stock/templates/stock/item_attachments.html
@@ -7,8 +7,8 @@
{% include "stock/tabs.html" with tab='attachments' %}
-
{% trans "Stock Item Attachments" %}
+
{% include "attachment_table.html" with attachments=item.attachments.all %}
diff --git a/InvenTree/stock/templates/stock/item_notes.html b/InvenTree/stock/templates/stock/item_notes.html
index 31e818ef71..9dd21331b4 100644
--- a/InvenTree/stock/templates/stock/item_notes.html
+++ b/InvenTree/stock/templates/stock/item_notes.html
@@ -10,6 +10,7 @@
{% include "stock/tabs.html" with tab="notes" %}
{% if editing %}
+
{% trans "Stock Item Notes" %}
diff --git a/InvenTree/stock/templates/stock/item_tests.html b/InvenTree/stock/templates/stock/item_tests.html
new file mode 100644
index 0000000000..6621637f1f
--- /dev/null
+++ b/InvenTree/stock/templates/stock/item_tests.html
@@ -0,0 +1,76 @@
+{% extends "stock/item_base.html" %}
+
+{% load static %}
+{% load i18n %}
+
+{% block details %}
+
+{% include "stock/tabs.html" with tab='tests' %}
+
+
{% trans "Test Results" %}
+
+
+
+
+
+
+{% endblock %}
+
+{% block js_ready %}
+{{ block.super }}
+
+loadStockTestResultsTable(
+ $("#test-result-table"), {
+ params: {
+ stock_item: {{ item.id }},
+ user_detail: true,
+ attachment_detail: true,
+ },
+ }
+);
+
+function reloadTable() {
+ $("#test-result-table").bootstrapTable("refresh");
+}
+
+$("#add-test-result").click(function() {
+ launchModalForm(
+ "{% url 'stock-item-test-create' %}", {
+ data: {
+ stock_item: {{ item.id }},
+ },
+ success: reloadTable,
+ }
+ );
+});
+
+$("#test-result-table").on('click', '.button-test-edit', function() {
+ var button = $(this);
+
+ var url = `/stock/item/test/${button.attr('pk')}/edit/`;
+
+ launchModalForm(url, {
+ success: reloadTable,
+ });
+});
+
+$("#test-result-table").on('click', '.button-test-delete', function() {
+ var button = $(this);
+
+ var url = `/stock/item/test/${button.attr('pk')}/delete/`;
+
+ launchModalForm(url, {
+ success: reloadTable,
+ });
+});
+
+{% endblock %}
\ No newline at end of file
diff --git a/InvenTree/stock/templates/stock/tabs.html b/InvenTree/stock/templates/stock/tabs.html
index fddd2f095f..5ec75ddc28 100644
--- a/InvenTree/stock/templates/stock/tabs.html
+++ b/InvenTree/stock/templates/stock/tabs.html
@@ -1,15 +1,20 @@
{% load i18n %}
\ No newline at end of file
diff --git a/InvenTree/stock/test_api.py b/InvenTree/stock/test_api.py
index fe49547cee..cd598e8538 100644
--- a/InvenTree/stock/test_api.py
+++ b/InvenTree/stock/test_api.py
@@ -6,11 +6,17 @@ from django.contrib.auth import get_user_model
from .models import StockLocation
-class StockLocationTest(APITestCase):
- """
- Series of API tests for the StockLocation API
- """
- list_url = reverse('api-location-list')
+class StockAPITestCase(APITestCase):
+
+ fixtures = [
+ 'category',
+ 'part',
+ 'company',
+ 'location',
+ 'supplier_part',
+ 'stock',
+ 'stock_tests',
+ ]
def setUp(self):
# Create a user for auth
@@ -18,6 +24,21 @@ class StockLocationTest(APITestCase):
User.objects.create_user('testuser', 'test@testing.com', 'password')
self.client.login(username='testuser', password='password')
+ def doPost(self, url, data={}):
+ response = self.client.post(url, data=data, format='json')
+
+ return response
+
+
+class StockLocationTest(StockAPITestCase):
+ """
+ Series of API tests for the StockLocation API
+ """
+ list_url = reverse('api-location-list')
+
+ def setUp(self):
+ super().setUp()
+
# Add some stock locations
StockLocation.objects.create(name='top', description='top category')
@@ -38,7 +59,7 @@ class StockLocationTest(APITestCase):
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
-class StockItemTest(APITestCase):
+class StockItemTest(StockAPITestCase):
"""
Series of API tests for the StockItem API
"""
@@ -49,11 +70,7 @@ class StockItemTest(APITestCase):
return reverse('api-stock-detail', kwargs={'pk': pk})
def setUp(self):
- # Create a user for auth
- User = get_user_model()
- User.objects.create_user('testuser', 'test@testing.com', 'password')
- self.client.login(username='testuser', password='password')
-
+ super().setUp()
# Create some stock locations
top = StockLocation.objects.create(name='A', description='top')
@@ -65,30 +82,11 @@ class StockItemTest(APITestCase):
self.assertEqual(response.status_code, status.HTTP_200_OK)
-class StocktakeTest(APITestCase):
+class StocktakeTest(StockAPITestCase):
"""
Series of tests for the Stocktake API
"""
- fixtures = [
- 'category',
- 'part',
- 'company',
- 'location',
- 'supplier_part',
- 'stock',
- ]
-
- def setUp(self):
- User = get_user_model()
- User.objects.create_user('testuser', 'test@testing.com', 'password')
- self.client.login(username='testuser', password='password')
-
- def doPost(self, url, data={}):
- response = self.client.post(url, data=data, format='json')
-
- return response
-
def test_action(self):
"""
Test each stocktake action endpoint,
@@ -179,3 +177,82 @@ class StocktakeTest(APITestCase):
response = self.doPost(url, data)
self.assertContains(response, 'Valid location must be specified', status_code=status.HTTP_400_BAD_REQUEST)
+
+
+class StockTestResultTest(StockAPITestCase):
+
+ def get_url(self):
+ return reverse('api-stock-test-result-list')
+
+ def test_list(self):
+
+ url = self.get_url()
+ response = self.client.get(url)
+
+ self.assertEqual(response.status_code, status.HTTP_200_OK)
+ self.assertGreaterEqual(len(response.data), 4)
+
+ response = self.client.get(url, data={'stock_item': 105})
+ 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 = self.get_url()
+
+ response = self.client.get(url)
+ n = len(response.data)
+
+ data = {
+ 'stock_item': 105,
+ 'test': 'Checked Steam Valve',
+ 'result': False,
+ 'value': '150kPa',
+ 'notes': 'I guess there was just too much pressure?',
+ }
+
+ response = self.client.post(url, data, format='json')
+
+ self.assertEqual(response.status_code, status.HTTP_201_CREATED)
+
+ response = self.client.get(url)
+ self.assertEqual(len(response.data), n + 1)
+
+ # And read out again
+ response = self.client.get(url, data={'test': 'Checked Steam Valve'})
+
+ self.assertEqual(len(response.data), 1)
+
+ test = response.data[0]
+ self.assertEqual(test['value'], '150kPa')
+ self.assertEqual(test['user'], 1)
diff --git a/InvenTree/stock/tests.py b/InvenTree/stock/tests.py
index edb9660000..8fce455a97 100644
--- a/InvenTree/stock/tests.py
+++ b/InvenTree/stock/tests.py
@@ -17,6 +17,7 @@ class StockTest(TestCase):
'part',
'location',
'stock',
+ 'stock_tests',
]
def setUp(self):
@@ -95,8 +96,8 @@ class StockTest(TestCase):
self.assertFalse(self.drawer2.has_items())
# Drawer 3 should have three stock items
- self.assertEqual(self.drawer3.stock_items.count(), 15)
- self.assertEqual(self.drawer3.item_count, 15)
+ self.assertEqual(self.drawer3.stock_items.count(), 16)
+ self.assertEqual(self.drawer3.item_count, 16)
def test_stock_count(self):
part = Part.objects.get(pk=1)
@@ -108,7 +109,7 @@ class StockTest(TestCase):
self.assertEqual(part.total_stock, 9000)
# There should be 18 widgets in stock
- self.assertEqual(StockItem.objects.filter(part=25).aggregate(Sum('quantity'))['quantity__sum'], 18)
+ self.assertEqual(StockItem.objects.filter(part=25).aggregate(Sum('quantity'))['quantity__sum'], 19)
def test_delete_location(self):
@@ -168,12 +169,12 @@ class StockTest(TestCase):
self.assertEqual(w1.quantity, 4)
# There should also be a new object still in drawer3
- self.assertEqual(StockItem.objects.filter(part=25).count(), 4)
+ self.assertEqual(StockItem.objects.filter(part=25).count(), 5)
widget = StockItem.objects.get(location=self.drawer3.id, part=25, quantity=4)
# Try to move negative units
self.assertFalse(widget.move(self.bathroom, 'Test', None, quantity=-100))
- self.assertEqual(StockItem.objects.filter(part=25).count(), 4)
+ self.assertEqual(StockItem.objects.filter(part=25).count(), 5)
# Try to move to a blank location
self.assertFalse(widget.move(None, 'null', None))
@@ -404,3 +405,29 @@ class VariantTest(StockTest):
item.serial += 1
item.save()
+
+
+class TestResultTest(StockTest):
+ """
+ Tests for the StockItemTestResult model.
+ """
+
+ def test_test_count(self):
+ item = StockItem.objects.get(pk=105)
+ tests = item.test_results
+ self.assertEqual(tests.count(), 4)
+
+ results = item.getTestResults(test="Temperature Test")
+ self.assertEqual(results.count(), 2)
+
+ # Passing tests
+ self.assertEqual(item.getTestResults(result=True).count(), 3)
+ self.assertEqual(item.getTestResults(result=False).count(), 1)
+
+ # Result map
+ result_map = item.testResultMap()
+
+ self.assertEqual(len(result_map), 3)
+
+ for test in ['Firmware Version', 'Settings Checksum', 'Temperature Test']:
+ self.assertIn(test, result_map.keys())
diff --git a/InvenTree/stock/urls.py b/InvenTree/stock/urls.py
index 149c0dde2f..ce99976f8b 100644
--- a/InvenTree/stock/urls.py
+++ b/InvenTree/stock/urls.py
@@ -24,6 +24,7 @@ stock_item_detail_urls = [
url(r'^add_tracking/', views.StockItemTrackingCreate.as_view(), name='stock-tracking-create'),
+ url(r'^test/', views.StockItemDetail.as_view(template_name='stock/item_tests.html'), name='stock-item-test-results'),
url(r'^children/', views.StockItemDetail.as_view(template_name='stock/item_childs.html'), name='stock-item-children'),
url(r'^attachments/', views.StockItemDetail.as_view(template_name='stock/item_attachments.html'), name='stock-item-attachments'),
url(r'^notes/', views.StockItemNotes.as_view(), name='stock-item-notes'),
@@ -51,12 +52,20 @@ stock_urls = [
url(r'^item/new/?', views.StockItemCreate.as_view(), name='stock-item-create'),
+ # URLs for StockItem attachments
url(r'^item/attachment/', include([
url(r'^new/', views.StockItemAttachmentCreate.as_view(), name='stock-item-attachment-create'),
url(r'^(?P
\d+)/edit/', views.StockItemAttachmentEdit.as_view(), name='stock-item-attachment-edit'),
url(r'^(?P\d+)/delete/', views.StockItemAttachmentDelete.as_view(), name='stock-item-attachment-delete'),
])),
+ # URLs for StockItem tests
+ url(r'^item/test/', include([
+ url(r'^new/', views.StockItemTestResultCreate.as_view(), name='stock-item-test-create'),
+ url(r'^(?P\d+)/edit/', views.StockItemTestResultEdit.as_view(), name='stock-item-test-edit'),
+ url(r'^(?P\d+)/delete/', views.StockItemTestResultDelete.as_view(), name='stock-item-test-delete'),
+ ])),
+
url(r'^track/', include(stock_tracking_urls)),
url(r'^adjust/?', views.StockAdjust.as_view(), name='stock-adjust'),
diff --git a/InvenTree/stock/views.py b/InvenTree/stock/views.py
index dc70cc6bfb..39bc7138b1 100644
--- a/InvenTree/stock/views.py
+++ b/InvenTree/stock/views.py
@@ -26,7 +26,7 @@ from datetime import datetime
from company.models import Company, SupplierPart
from part.models import Part
-from .models import StockItem, StockLocation, StockItemTracking, StockItemAttachment
+from .models import StockItem, StockLocation, StockItemTracking, StockItemAttachment, StockItemTestResult
from .admin import StockItemResource
@@ -38,6 +38,7 @@ from .forms import TrackingEntryForm
from .forms import SerializeStockForm
from .forms import ExportOptionsForm
from .forms import EditStockItemAttachmentForm
+from .forms import EditStockItemTestResultForm
class StockIndex(ListView):
@@ -228,6 +229,69 @@ class StockItemAttachmentDelete(AjaxDeleteView):
}
+class StockItemTestResultCreate(AjaxCreateView):
+ """
+ View for adding a new StockItemTestResult
+ """
+
+ model = StockItemTestResult
+ form_class = EditStockItemTestResultForm
+ ajax_form_title = _("Add Test Result")
+
+ def post_save(self, **kwargs):
+ """ Record the user that uploaded the test result """
+
+ self.object.user = self.request.user
+ self.object.save()
+
+ def get_initial(self):
+
+ initials = super().get_initial()
+
+ try:
+ stock_id = self.request.GET.get('stock_item', None)
+ initials['stock_item'] = StockItem.objects.get(pk=stock_id)
+ except (ValueError, StockItem.DoesNotExist):
+ pass
+
+ return initials
+
+ def get_form(self):
+
+ form = super().get_form()
+ form.fields['stock_item'].widget = HiddenInput()
+
+ return form
+
+
+class StockItemTestResultEdit(AjaxUpdateView):
+ """
+ View for editing a StockItemTestResult
+ """
+
+ model = StockItemTestResult
+ form_class = EditStockItemTestResultForm
+ ajax_form_title = _("Edit Test Result")
+
+ def get_form(self):
+
+ form = super().get_form()
+
+ form.fields['stock_item'].widget = HiddenInput()
+
+ return form
+
+
+class StockItemTestResultDelete(AjaxDeleteView):
+ """
+ View for deleting a StockItemTestResult
+ """
+
+ model = StockItemTestResult
+ ajax_form_title = _("Delete Test Result")
+ context_object_name = "result"
+
+
class StockExportOptions(AjaxView):
""" Form for selecting StockExport options """
diff --git a/InvenTree/templates/attachment_table.html b/InvenTree/templates/attachment_table.html
index 71664a3ccc..32407cbc5b 100644
--- a/InvenTree/templates/attachment_table.html
+++ b/InvenTree/templates/attachment_table.html
@@ -28,7 +28,7 @@
|