From 327682b719db455efe6a38d40d3295d33e484458 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 17 May 2020 00:26:10 +1000 Subject: [PATCH] Add forms / views / etc to create / edit / delete test results manually --- InvenTree/stock/forms.py | 20 +++++- .../stock/templates/stock/item_tests.html | 39 ++++++++++- InvenTree/stock/urls.py | 8 +++ InvenTree/stock/views.py | 65 ++++++++++++++++++- InvenTree/templates/attachment_table.html | 2 +- 5 files changed, 130 insertions(+), 4 deletions(-) diff --git a/InvenTree/stock/forms.py b/InvenTree/stock/forms.py index 98a4de56d6..0d543b1315 100644 --- a/InvenTree/stock/forms.py +++ b/InvenTree/stock/forms.py @@ -15,7 +15,9 @@ from InvenTree.helpers import GetExportFormats from InvenTree.forms import HelperForm from InvenTree.fields import RoundingDecimalFormField -from .models import StockLocation, StockItem, StockItemTracking, StockItemAttachment +from .models import StockLocation, StockItem, StockItemTracking +from .models import StockItemAttachment +from .models import StockItemTestResult class EditStockItemAttachmentForm(HelperForm): @@ -32,6 +34,22 @@ class EditStockItemAttachmentForm(HelperForm): ] +class EditStockItemTestResultForm(HelperForm): + """ + Form for creating / editing a StockItemTestResult object. + """ + + class Meta: + model = StockItemTestResult + fields = [ + 'stock_item', + 'test', + 'result', + 'value', + 'notes', + ] + + class EditStockLocationForm(HelperForm): """ Form for editing a StockLocation """ diff --git a/InvenTree/stock/templates/stock/item_tests.html b/InvenTree/stock/templates/stock/item_tests.html index a3e21962e1..6621637f1f 100644 --- a/InvenTree/stock/templates/stock/item_tests.html +++ b/InvenTree/stock/templates/stock/item_tests.html @@ -12,7 +12,9 @@
- +
+ +
@@ -36,4 +38,39 @@ loadStockTestResultsTable( } ); +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/urls.py b/InvenTree/stock/urls.py index f0b3091b84..ce99976f8b 100644 --- a/InvenTree/stock/urls.py +++ b/InvenTree/stock/urls.py @@ -52,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..84b5799898 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,68 @@ 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 @@