From ef305032c962b6b4f97e036a1d7eb587a3c24e16 Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 2 Nov 2021 20:04:25 +1100 Subject: [PATCH] Create new stock location via API forms --- InvenTree/stock/templates/stock/location.html | 25 +++----- InvenTree/stock/test_views.py | 58 ------------------- InvenTree/stock/urls.py | 2 - InvenTree/stock/views.py | 4 ++ InvenTree/templates/js/translated/stock.js | 22 ++++++- 5 files changed, 32 insertions(+), 79 deletions(-) diff --git a/InvenTree/stock/templates/stock/location.html b/InvenTree/stock/templates/stock/location.html index d3cc407298..78b82b5093 100644 --- a/InvenTree/stock/templates/stock/location.html +++ b/InvenTree/stock/templates/stock/location.html @@ -222,24 +222,13 @@ }); $('#location-create').click(function () { - launchModalForm("{% url 'stock-location-create' %}", - { - data: { - {% if location %} - location: {{ location.id }} - {% endif %} - }, - follow: true, - secondary: [ - { - field: 'parent', - label: '{% trans "New Location" %}', - title: '{% trans "Create new location" %}', - url: "{% url 'stock-location-create' %}", - }, - ] - }); - return false; + + createStockLocation({ + {% if location %} + parent: {{ location.pk }}, + {% endif %} + follow: true, + }); }); {% if location %} diff --git a/InvenTree/stock/test_views.py b/InvenTree/stock/test_views.py index 64d0ac42d5..3aacf8a139 100644 --- a/InvenTree/stock/test_views.py +++ b/InvenTree/stock/test_views.py @@ -63,32 +63,6 @@ class StockListTest(StockViewTestCase): self.assertEqual(response.status_code, 200) -class StockLocationTest(StockViewTestCase): - """ Tests for StockLocation views """ - - def test_qr_code(self): - # Request the StockLocation QR view - response = self.client.get(reverse('stock-location-qr', args=(1,)), HTTP_X_REQUESTED_WITH='XMLHttpRequest') - self.assertEqual(response.status_code, 200) - - # Test for an invalid StockLocation - response = self.client.get(reverse('stock-location-qr', args=(999,)), HTTP_X_REQUESTED_WITH='XMLHttpRequest') - self.assertEqual(response.status_code, 200) - - def test_create(self): - # Test StockLocation creation view - response = self.client.get(reverse('stock-location-create'), HTTP_X_REQUESTED_WITH='XMLHttpRequest') - self.assertEqual(response.status_code, 200) - - # Create with a parent - response = self.client.get(reverse('stock-location-create'), {'location': 1}, HTTP_X_REQUESTED_WITH='XMLHttpRequest') - self.assertEqual(response.status_code, 200) - - # Create with an invalid parent - response = self.client.get(reverse('stock-location-create'), {'location': 999}, HTTP_X_REQUESTED_WITH='XMLHttpRequest') - self.assertEqual(response.status_code, 200) - - class StockItemTest(StockViewTestCase): """" Tests for StockItem views """ @@ -289,38 +263,6 @@ class StockOwnershipTest(StockViewTestCase): 'owner': new_user_group_owner.pk, } - # Create new parent location - response = self.client.post(reverse('stock-location-create'), - parent_location, HTTP_X_REQUESTED_WITH='XMLHttpRequest') - self.assertContains(response, '"form_valid": true', status_code=200) - - # Retrieve created location - parent_location = StockLocation.objects.get(name=parent_location['name']) - - # Create new child location - new_location = { - 'name': 'Upper Left Drawer', - 'description': 'John\'s desk - Upper left drawer', - } - - # Try to create new location with neither parent or owner - response = self.client.post(reverse('stock-location-create'), - new_location, HTTP_X_REQUESTED_WITH='XMLHttpRequest') - self.assertContains(response, '"form_valid": false', status_code=200) - - # Try to create new location with invalid owner - new_location['parent'] = parent_location.id - new_location['owner'] = user_group_owner.pk - response = self.client.post(reverse('stock-location-create'), - new_location, HTTP_X_REQUESTED_WITH='XMLHttpRequest') - self.assertContains(response, '"form_valid": false', status_code=200) - - # Try to create new location with valid owner - new_location['owner'] = new_user_group_owner.pk - response = self.client.post(reverse('stock-location-create'), - new_location, HTTP_X_REQUESTED_WITH='XMLHttpRequest') - self.assertContains(response, '"form_valid": true', status_code=200) - # Retrieve created location location_created = StockLocation.objects.get(name=new_location['name']) diff --git a/InvenTree/stock/urls.py b/InvenTree/stock/urls.py index 58b76b9bf7..5441101aa1 100644 --- a/InvenTree/stock/urls.py +++ b/InvenTree/stock/urls.py @@ -8,8 +8,6 @@ from stock import views location_urls = [ - url(r'^new/', views.StockLocationCreate.as_view(), name='stock-location-create'), - url(r'^(?P\d+)/', include([ url(r'^delete/?', views.StockLocationDelete.as_view(), name='stock-location-delete'), url(r'^qr_code/?', views.StockLocationQRCode.as_view(), name='stock-location-qr'), diff --git a/InvenTree/stock/views.py b/InvenTree/stock/views.py index 583ad705db..44c1a824bd 100644 --- a/InvenTree/stock/views.py +++ b/InvenTree/stock/views.py @@ -931,6 +931,10 @@ class StockLocationCreate(AjaxCreateView): """ View for creating a new StockLocation A parent location (another StockLocation object) can be passed as a query parameter + + TODO: Remove this class entirely, as it has been migrated to the API forms + - Still need to check that all the functionality (as below) has been implemented + """ model = StockLocation diff --git a/InvenTree/templates/js/translated/stock.js b/InvenTree/templates/js/translated/stock.js index 8d0637238c..a987348d8b 100644 --- a/InvenTree/templates/js/translated/stock.js +++ b/InvenTree/templates/js/translated/stock.js @@ -59,13 +59,19 @@ function stockLocationFields(options={}) { - return { + var fields = { parent: { help_text: '{% trans "Parent stock location" %}', }, name: {}, description: {}, }; + + if (options.parent) { + fields.parent.value = options.parent; + } + + return fields; } @@ -82,6 +88,20 @@ function editStockLocation(pk, options={}) { } +/* + * Launch an API form to create a new stock location + */ +function createStockLocation(options={}) { + + var url = '{% url "api-location-list" %}'; + + options.method = 'POST'; + options.fields = stockLocationFields(options); + + constructForm(url, options); +} + + function stockItemFields(options={}) { var fields = { part: {},