From 0e96654b6ad8daea171b0f725f8032b58567e444 Mon Sep 17 00:00:00 2001 From: Matthias Mair Date: Sat, 7 Jan 2023 13:23:38 +0100 Subject: [PATCH] [FR] Move URL endpoints to API namespace (#4163) * Move endpoints [FR] Move download URL endpoints to API namespace Fixes #3927 * rename endpoint ref name and update js * update endpoint name and js * rename endpoint and fix js * add docstring --- InvenTree/part/api.py | 13 +++++++++++++ InvenTree/part/templates/part/part_base.html | 2 +- InvenTree/part/test_bom_export.py | 4 ++-- InvenTree/part/test_views.py | 8 ++++---- InvenTree/part/urls.py | 9 --------- InvenTree/templates/js/translated/bom.js | 4 ++-- 6 files changed, 22 insertions(+), 18 deletions(-) diff --git a/InvenTree/part/api.py b/InvenTree/part/api.py index 0b7d000c7d..a7cdc08d45 100644 --- a/InvenTree/part/api.py +++ b/InvenTree/part/api.py @@ -36,6 +36,7 @@ from plugin.serializers import MetadataSerializer from stock.models import StockItem, StockLocation from . import serializers as part_serializers +from . import views from .models import (BomItem, BomItemSubstitute, Part, PartAttachment, PartCategory, PartCategoryParameterTemplate, PartInternalPriceBreak, PartParameter, @@ -2196,6 +2197,9 @@ part_api_urls = [ re_path(r'^(?P\d+)/?', PartThumbsUpdate.as_view(), name='api-part-thumbs-update'), ])), + # BOM template + re_path(r'^bom_template/?', views.BomUploadTemplate.as_view(), name='api-bom-upload-template'), + re_path(r'^(?P\d+)/', include([ # Endpoint for extra serial number information @@ -2218,6 +2222,15 @@ part_api_urls = [ # Part pricing re_path(r'^pricing/', PartPricingDetail.as_view(), name='api-part-pricing'), + # BOM download + re_path(r'^bom-download/?', views.BomDownload.as_view(), name='api-bom-download'), + + # QR code download + re_path(r'^qr_code/?', views.PartQRCode.as_view(), name='api-part-qr'), + + # Old pricing endpoint + re_path(r'^pricing2/', views.PartPricing.as_view(), name='part-pricing'), + # Part detail endpoint re_path(r'^.*$', PartDetail.as_view(), name='api-part-detail'), ])), diff --git a/InvenTree/part/templates/part/part_base.html b/InvenTree/part/templates/part/part_base.html index bfa9c04c04..93550543e7 100644 --- a/InvenTree/part/templates/part/part_base.html +++ b/InvenTree/part/templates/part/part_base.html @@ -441,7 +441,7 @@ {% if barcodes %} $("#show-qr-code").click(function() { launchModalForm( - "{% url 'part-qr' part.id %}", + "{% url 'api-part-qr' part.id %}", { no_post: true, } diff --git a/InvenTree/part/test_bom_export.py b/InvenTree/part/test_bom_export.py index 051f56b9ac..c40b7607ed 100644 --- a/InvenTree/part/test_bom_export.py +++ b/InvenTree/part/test_bom_export.py @@ -23,11 +23,11 @@ class BomExportTest(InvenTreeTestCase): """Perform test setup functions""" super().setUp() - self.url = reverse('bom-download', kwargs={'pk': 100}) + self.url = reverse('api-bom-download', kwargs={'pk': 100}) def test_bom_template(self): """Test that the BOM template can be downloaded from the server.""" - url = reverse('bom-upload-template') + url = reverse('api-bom-upload-template') # Download an XLS template response = self.client.get(url, data={'format': 'xls'}) diff --git a/InvenTree/part/test_views.py b/InvenTree/part/test_views.py index 1bb92cbbf3..469ef4ec8e 100644 --- a/InvenTree/part/test_views.py +++ b/InvenTree/part/test_views.py @@ -110,7 +110,7 @@ class PartDetailTest(PartViewTestCase): def test_bom_download(self): """Test downloading a BOM for a valid part.""" - response = self.client.get(reverse('bom-download', args=(1,)), HTTP_X_REQUESTED_WITH='XMLHttpRequest') + response = self.client.get(reverse('api-bom-download', args=(1,)), HTTP_X_REQUESTED_WITH='XMLHttpRequest') self.assertEqual(response.status_code, 200) self.assertIn('streaming_content', dir(response)) @@ -120,12 +120,12 @@ class PartQRTest(PartViewTestCase): def test_html_redirect(self): """A HTML request for a QR code should be redirected (use an AJAX request instead)""" - response = self.client.get(reverse('part-qr', args=(1,))) + response = self.client.get(reverse('api-part-qr', args=(1,))) self.assertEqual(response.status_code, 302) def test_valid_part(self): """Test QR code response for a Part""" - response = self.client.get(reverse('part-qr', args=(1,)), HTTP_X_REQUESTED_WITH='XMLHttpRequest') + response = self.client.get(reverse('api-part-qr', args=(1,)), HTTP_X_REQUESTED_WITH='XMLHttpRequest') self.assertEqual(response.status_code, 200) data = str(response.content) @@ -135,6 +135,6 @@ class PartQRTest(PartViewTestCase): def test_invalid_part(self): """Test response for an invalid Part ID value""" - response = self.client.get(reverse('part-qr', args=(9999,)), HTTP_X_REQUESTED_WITH='XMLHttpRequest') + response = self.client.get(reverse('api-part-qr', args=(9999,)), HTTP_X_REQUESTED_WITH='XMLHttpRequest') self.assertEqual(response.status_code, 200) diff --git a/InvenTree/part/urls.py b/InvenTree/part/urls.py index 74b99a8fbb..9cd5cafbec 100644 --- a/InvenTree/part/urls.py +++ b/InvenTree/part/urls.py @@ -11,14 +11,8 @@ from django.urls import include, re_path from . import views part_detail_urls = [ - re_path(r'^bom-download/?', views.BomDownload.as_view(), name='bom-download'), - - re_path(r'^pricing/', views.PartPricing.as_view(), name='part-pricing'), - re_path(r'^bom-upload/?', views.BomUpload.as_view(), name='upload-bom'), - re_path(r'^qr_code/?', views.PartQRCode.as_view(), name='part-qr'), - # Normal thumbnail with form re_path(r'^thumb-select/?', views.PartImageSelect.as_view(), name='part-image-select'), @@ -40,9 +34,6 @@ part_urls = [ re_path(r'^import/?', views.PartImportTemplate.as_view(), name='part-template-download'), re_path(r'^import-api/', views.PartImportAjax.as_view(), name='api-part-import'), - # Download a BOM upload template - re_path(r'^bom_template/?', views.BomUploadTemplate.as_view(), name='bom-upload-template'), - # Individual part using pk re_path(r'^(?P\d+)/', include(part_detail_urls)), diff --git a/InvenTree/templates/js/translated/bom.js b/InvenTree/templates/js/translated/bom.js index ee45ac2d9c..2331bf1625 100644 --- a/InvenTree/templates/js/translated/bom.js +++ b/InvenTree/templates/js/translated/bom.js @@ -293,7 +293,7 @@ function downloadBomTemplate(options={}) { $(opts.modal).modal('hide'); // Download the file - location.href = `{% url "bom-upload-template" %}?format=${format}`; + location.href = `{% url "api-bom-upload-template" %}?format=${format}`; } }); @@ -373,7 +373,7 @@ function exportBom(part_id, options={}) { 'pricing_data', ]; - var url = `/part/${part_id}/bom-download/?`; + var url = `/api/part/${part_id}/bom-download/?`; field_names.forEach(function(fn) { var val = getFormFieldValue(fn, fields[fn], opts);