From fd9f25dc0dd50d59905e0a7ba85f6fd6f15f3d18 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 17 May 2022 21:26:33 +1000 Subject: [PATCH 01/28] Adds helper function for testing a file download via the api --- InvenTree/InvenTree/api_tester.py | 34 +++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/InvenTree/InvenTree/api_tester.py b/InvenTree/InvenTree/api_tester.py index c55c3d3ba3..0420e93369 100644 --- a/InvenTree/InvenTree/api_tester.py +++ b/InvenTree/InvenTree/api_tester.py @@ -2,6 +2,10 @@ Helper functions for performing API unit tests """ +import io +import re + +from django.http.response import StreamingHttpResponse from django.contrib.auth import get_user_model from django.contrib.auth.models import Group from rest_framework.test import APITestCase @@ -165,3 +169,33 @@ class InvenTreeAPITestCase(APITestCase): self.assertEqual(response.status_code, expected_code) return response + + def download_file(self, url, data, expected_code=None, expected_fn=None): + """ + Download a file from the server, and return an in-memory file + """ + + response = self.client.get(url, data=data, format='json') + + if expected_code is not None: + self.assertEqual(response.status_code, expected_code) + + # Check that the response is of the correct type + if not isinstance(response, StreamingHttpResponse): + raise ValueError("Response is not a StreamingHttpResponse object as expected") + + # Extract filename + disposition = response.headers['Content-Disposition'] + + result = re.search(r'attachment; filename="([\w.]+)"', disposition) + + fn = result.groups()[0] + + with io.BytesIO() as fo: + fo.name = fn + fo.write(response.getvalue()) + + if expected_fn is not None: + self.assertEqual(expected_fn, fn) + + return fo \ No newline at end of file From 55000d5c483f9aded276652e50ad49bfafa50e3a Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 17 May 2022 22:28:46 +1000 Subject: [PATCH 02/28] Add ability to download file as StringIO or BytesIO --- InvenTree/InvenTree/api_tester.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/InvenTree/InvenTree/api_tester.py b/InvenTree/InvenTree/api_tester.py index 0420e93369..122ce1950f 100644 --- a/InvenTree/InvenTree/api_tester.py +++ b/InvenTree/InvenTree/api_tester.py @@ -170,7 +170,7 @@ class InvenTreeAPITestCase(APITestCase): return response - def download_file(self, url, data, expected_code=None, expected_fn=None): + def download_file(self, url, data, expected_code=None, expected_fn=None, decode=False): """ Download a file from the server, and return an in-memory file """ @@ -191,11 +191,20 @@ class InvenTreeAPITestCase(APITestCase): fn = result.groups()[0] - with io.BytesIO() as fo: - fo.name = fn - fo.write(response.getvalue()) - if expected_fn is not None: self.assertEqual(expected_fn, fn) - - return fo \ No newline at end of file + + if decode: + # Decode data and return as StringIO file object + fo = io.StringIO() + fo.name = fo + fo.write(response.getvalue().decode('UTF-8')) + else: + # Return a a BytesIO file object + fo = io.BytesIO() + fo.name = fn + fo.write(response.getvalue()) + + fo.seek(0) + + return fo From 920e7e0bb7dfafec5365d8ddbbdbbe8b482358b9 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 17 May 2022 23:21:30 +1000 Subject: [PATCH 03/28] Adds helper function to process and validate a downloaded .csv file --- InvenTree/InvenTree/api_tester.py | 35 +++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/InvenTree/InvenTree/api_tester.py b/InvenTree/InvenTree/api_tester.py index 122ce1950f..3408322bd3 100644 --- a/InvenTree/InvenTree/api_tester.py +++ b/InvenTree/InvenTree/api_tester.py @@ -2,6 +2,7 @@ Helper functions for performing API unit tests """ +import csv import io import re @@ -208,3 +209,37 @@ class InvenTreeAPITestCase(APITestCase): fo.seek(0) return fo + + def process_csv(self, fo, delimiter=',', required_cols=None, excluded_cols=None, required_rows=None): + """ + Helper function to process and validate a downloaded csv file + """ + + # Check that the correct object type has been passed + self.assertTrue(isinstance(fo, io.StringIO)) + + fo.seek(0) + + reader = csv.reader(fo, delimiter=delimiter) + + headers = [] + rows = [] + + for idx, row in enumerate(reader): + if idx == 0: + headers = row + else: + rows.append(row) + + if required_cols is not None: + for col in required_cols: + self.assertIn(col, required_cols) + + if excluded_cols is not None: + for col in excluded_cols: + self.assertNotIn(col, excluded_cols) + + if required_rows is not None: + self.assertEqual(len(rows), required_rows) + + return (headers, rows) From a44267c306e5f3bec3dc9386022b8d2cd1fae735 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 17 May 2022 23:22:40 +1000 Subject: [PATCH 04/28] bug fix --- InvenTree/InvenTree/api_tester.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/InvenTree/InvenTree/api_tester.py b/InvenTree/InvenTree/api_tester.py index 3408322bd3..4fb6a9e652 100644 --- a/InvenTree/InvenTree/api_tester.py +++ b/InvenTree/InvenTree/api_tester.py @@ -233,11 +233,11 @@ class InvenTreeAPITestCase(APITestCase): if required_cols is not None: for col in required_cols: - self.assertIn(col, required_cols) + self.assertIn(col, headers) if excluded_cols is not None: for col in excluded_cols: - self.assertNotIn(col, excluded_cols) + self.assertNotIn(col, headers) if required_rows is not None: self.assertEqual(len(rows), required_rows) From 3488da19b024b94f27ab8d9f71588fe00cf70eca Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 17 May 2022 23:47:09 +1000 Subject: [PATCH 05/28] Return data as a list of dict objects --- InvenTree/InvenTree/api_tester.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/api_tester.py b/InvenTree/InvenTree/api_tester.py index 4fb6a9e652..1fa2816653 100644 --- a/InvenTree/InvenTree/api_tester.py +++ b/InvenTree/InvenTree/api_tester.py @@ -242,4 +242,15 @@ class InvenTreeAPITestCase(APITestCase): if required_rows is not None: self.assertEqual(len(rows), required_rows) - return (headers, rows) + # Return the file data as a list of dict items, based on the headers + data = [] + + for row in rows: + entry = {} + + for idx, col in enumerate(headers): + entry[col] = row[idx] + + data.append(entry) + + return data From a6be0b32c6a660c83698c476d0659b991212528e Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 17 May 2022 23:48:58 +1000 Subject: [PATCH 06/28] Add unit tests for exporting SalesOrder data --- InvenTree/InvenTree/api_tester.py | 12 ++-- InvenTree/order/admin.py | 6 ++ InvenTree/order/api.py | 4 +- InvenTree/order/test_api.py | 93 +++++++++++++++++++++++++++++++ 4 files changed, 107 insertions(+), 8 deletions(-) diff --git a/InvenTree/InvenTree/api_tester.py b/InvenTree/InvenTree/api_tester.py index 1fa2816653..9dde7c0f52 100644 --- a/InvenTree/InvenTree/api_tester.py +++ b/InvenTree/InvenTree/api_tester.py @@ -184,7 +184,7 @@ class InvenTreeAPITestCase(APITestCase): # Check that the response is of the correct type if not isinstance(response, StreamingHttpResponse): raise ValueError("Response is not a StreamingHttpResponse object as expected") - + # Extract filename disposition = response.headers['Content-Disposition'] @@ -230,24 +230,24 @@ class InvenTreeAPITestCase(APITestCase): headers = row else: rows.append(row) - + if required_cols is not None: for col in required_cols: self.assertIn(col, headers) - + if excluded_cols is not None: for col in excluded_cols: self.assertNotIn(col, headers) - + if required_rows is not None: self.assertEqual(len(rows), required_rows) - + # Return the file data as a list of dict items, based on the headers data = [] for row in rows: entry = {} - + for idx, col in enumerate(headers): entry[col] = row[idx] diff --git a/InvenTree/order/admin.py b/InvenTree/order/admin.py index eaeebff04d..ac74a004e3 100644 --- a/InvenTree/order/admin.py +++ b/InvenTree/order/admin.py @@ -105,6 +105,9 @@ class PurchaseOrderResource(ModelResource): model = PurchaseOrder skip_unchanged = True clean_model_instances = True + exclude = [ + 'metadata', + ] class PurchaseOrderLineItemResource(ModelResource): @@ -147,6 +150,9 @@ class SalesOrderResource(ModelResource): model = SalesOrder skip_unchanged = True clean_model_instances = True + exclude = [ + 'metadata', + ] class SalesOrderLineItemResource(ModelResource): diff --git a/InvenTree/order/api.py b/InvenTree/order/api.py index e65463d55c..2a9c3b99d2 100644 --- a/InvenTree/order/api.py +++ b/InvenTree/order/api.py @@ -667,9 +667,9 @@ class SalesOrderList(APIDownloadMixin, generics.ListCreateAPIView): outstanding = str2bool(outstanding) if outstanding: - queryset = queryset.filter(status__in=models.SalesOrderStatus.OPEN) + queryset = queryset.filter(status__in=SalesOrderStatus.OPEN) else: - queryset = queryset.exclude(status__in=models.SalesOrderStatus.OPEN) + queryset = queryset.exclude(status__in=SalesOrderStatus.OPEN) # Filter by 'overdue' status overdue = params.get('overdue', None) diff --git a/InvenTree/order/test_api.py b/InvenTree/order/test_api.py index 76aa8670a4..1a044c9b46 100644 --- a/InvenTree/order/test_api.py +++ b/InvenTree/order/test_api.py @@ -2,6 +2,8 @@ Tests for the Order API """ +import io + from datetime import datetime, timedelta from rest_framework import status @@ -908,6 +910,97 @@ class SalesOrderTest(OrderTest): self.assertEqual(order.get_metadata('xyz'), 'abc') +class SalesOrderDownloadTest(OrderTest): + """Unit tests for downloading SalesOrder data via the API endpoint""" + + def test_download_fail(self): + """Test that downloading without the 'export' option fails""" + + url = reverse('api-so-list') + + with self.assertRaises(ValueError): + self.download_file(url, {}, expected_code=200) + + def test_download_xls(self): + url = reverse('api-so-list') + + # Download .xls file + fo = self.download_file( + url, + { + 'export': 'xls', + }, + expected_code=200, + expected_fn='InvenTree_SalesOrders.xls', + ) + + self.assertTrue(isinstance(fo, io.BytesIO)) + + def test_download_csv(self): + + url = reverse('api-so-list') + + required_cols = [ + 'line_items', + 'id', + 'reference', + 'customer', + 'status', + 'shipment_date', + 'notes', + 'description', + ] + + excluded_cols = [ + 'metadata' + ] + + # Download .xls file + with self.download_file( + url, + { + 'export': 'csv', + }, + expected_code=200, + expected_fn='InvenTree_SalesOrders.csv', + decode=True + ) as fo: + + data = self.process_csv( + fo, + required_cols=required_cols, + excluded_cols=excluded_cols, + required_rows=models.SalesOrder.objects.count() + ) + + for line in data: + + order = models.SalesOrder.objects.get(pk=line['id']) + + self.assertEqual(line['description'], order.description) + self.assertEqual(line['status'], str(order.status)) + + # Download only outstanding sales orders + with self.download_file( + url, + { + 'export': 'tsv', + 'outstanding': True, + }, + expected_code=200, + expected_fn='InvenTree_SalesOrders.tsv', + decode=True, + ) as fo: + + self.process_csv( + fo, + required_cols=required_cols, + excluded_cols=excluded_cols, + required_rows=models.SalesOrder.objects.filter(status__in=SalesOrderStatus.OPEN).count(), + delimiter='\t', + ) + + class SalesOrderAllocateTest(OrderTest): """ Unit tests for allocating stock items against a SalesOrder From 0d078768feeccc087b9deaefe016d1d7684b1d21 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 18 May 2022 00:08:11 +1000 Subject: [PATCH 07/28] Unit tests for downloading PurchaseOrder data --- InvenTree/InvenTree/api_tester.py | 2 +- InvenTree/order/test_api.py | 63 +++++++++++++++++++++++++++++-- 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/InvenTree/InvenTree/api_tester.py b/InvenTree/InvenTree/api_tester.py index 9dde7c0f52..34976ffbfe 100644 --- a/InvenTree/InvenTree/api_tester.py +++ b/InvenTree/InvenTree/api_tester.py @@ -171,7 +171,7 @@ class InvenTreeAPITestCase(APITestCase): return response - def download_file(self, url, data, expected_code=None, expected_fn=None, decode=False): + def download_file(self, url, data, expected_code=None, expected_fn=None, decode=True): """ Download a file from the server, and return an in-memory file """ diff --git a/InvenTree/order/test_api.py b/InvenTree/order/test_api.py index 1a044c9b46..fffeb29216 100644 --- a/InvenTree/order/test_api.py +++ b/InvenTree/order/test_api.py @@ -325,6 +325,62 @@ class PurchaseOrderTest(OrderTest): self.assertEqual(order.get_metadata('yam'), 'yum') +class PurchaseOrderDownloadTest(OrderTest): + """Unit tests for downloading PurchaseOrder data via the API endpoint""" + + required_cols = [ + 'id', + 'line_items', + 'description', + 'issue_date', + 'notes', + 'reference', + 'status', + 'supplier_reference', + ] + + excluded_cols = [ + 'metadata', + ] + + def test_download_wrong_format(self): + """Incorrect format should default raise an error""" + + url = reverse('api-po-list') + + with self.assertRaises(ValueError): + self.download_file( + url, + { + 'export': 'xyz', + } + ) + + def test_download_csv(self): + """Download PurchaseOrder data as .csv""" + + with self.download_file( + reverse('api-po-list'), + { + 'export': 'csv', + }, + expected_code=200, + expected_fn='InvenTree_PurchaseOrders.csv', + ) as fo: + + data = self.process_csv( + fo, + required_cols=self.required_cols, + excluded_cols=self.excluded_cols, + required_rows=models.PurchaseOrder.objects.count() + ) + + for row in data: + order = models.PurchaseOrder.objects.get(pk=row['id']) + + self.assertEqual(order.description, row['description']) + self.assertEqual(order.reference, row['reference']) + class PurchaseOrderReceiveTest(OrderTest): """ Unit tests for receiving items against a PurchaseOrder @@ -925,16 +981,15 @@ class SalesOrderDownloadTest(OrderTest): url = reverse('api-so-list') # Download .xls file - fo = self.download_file( + with self.download_file( url, { 'export': 'xls', }, expected_code=200, expected_fn='InvenTree_SalesOrders.xls', - ) - - self.assertTrue(isinstance(fo, io.BytesIO)) + ) as fo: + self.assertTrue(isinstance(fo, io.BytesIO)) def test_download_csv(self): From c5b14944a1a710d0afbac5d7d5cc852ad6569fd7 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 18 May 2022 00:31:43 +1000 Subject: [PATCH 08/28] Unit tests for downloading BuildOrder data --- InvenTree/build/admin.py | 3 ++- InvenTree/build/test_api.py | 44 +++++++++++++++++++++++++++++++++++++ InvenTree/order/test_api.py | 1 + 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/InvenTree/build/admin.py b/InvenTree/build/admin.py index 32d843d057..5988850fe4 100644 --- a/InvenTree/build/admin.py +++ b/InvenTree/build/admin.py @@ -16,7 +16,7 @@ class BuildResource(ModelResource): # but we don't for other ones. # TODO: 2022-05-12 - Need to investigate why this is the case! - pk = Field(attribute='pk') + id = Field(attribute='pk') reference = Field(attribute='reference') @@ -45,6 +45,7 @@ class BuildResource(ModelResource): clean_model_instances = True exclude = [ 'lft', 'rght', 'tree_id', 'level', + 'metadata', ] diff --git a/InvenTree/build/test_api.py b/InvenTree/build/test_api.py index 068493ad5e..4ba54e9c73 100644 --- a/InvenTree/build/test_api.py +++ b/InvenTree/build/test_api.py @@ -511,6 +511,50 @@ class BuildTest(BuildAPITest): self.assertIn('This build output has already been completed', str(response.data)) + def test_download_build_orders(self): + + required_cols = [ + 'reference', + 'status', + 'completed', + 'batch', + 'notes', + 'title', + 'part', + 'part_name', + 'id', + 'quantity', + ] + + excluded_cols = [ + 'lft', 'rght', 'tree_id', 'level', + 'metadata', + ] + + with self.download_file( + reverse('api-build-list'), + { + 'export': 'csv', + } + ) as fo: + + data = self.process_csv( + fo, + required_cols=required_cols, + excluded_cols=excluded_cols, + required_rows=Build.objects.count() + ) + + for row in data: + + build = Build.objects.get(pk=row['id']) + + self.assertEqual(str(build.part.pk), row['part']) + self.assertEqual(build.part.full_name, row['part_name']) + + self.assertEqual(build.reference, row['reference']) + self.assertEqual(build.title, row['title']) + class BuildAllocationTest(BuildAPITest): """ diff --git a/InvenTree/order/test_api.py b/InvenTree/order/test_api.py index fffeb29216..bb058d84bb 100644 --- a/InvenTree/order/test_api.py +++ b/InvenTree/order/test_api.py @@ -381,6 +381,7 @@ class PurchaseOrderDownloadTest(OrderTest): self.assertEqual(order.description, row['description']) self.assertEqual(order.reference, row['reference']) + class PurchaseOrderReceiveTest(OrderTest): """ Unit tests for receiving items against a PurchaseOrder From 1c6d79451ecfee7c3fc1d53ccb4e0c8bdf404dec Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 18 May 2022 07:25:43 +1000 Subject: [PATCH 09/28] Don't decode downloaded .xls file --- InvenTree/order/test_api.py | 1 + 1 file changed, 1 insertion(+) diff --git a/InvenTree/order/test_api.py b/InvenTree/order/test_api.py index bb058d84bb..04be484310 100644 --- a/InvenTree/order/test_api.py +++ b/InvenTree/order/test_api.py @@ -989,6 +989,7 @@ class SalesOrderDownloadTest(OrderTest): }, expected_code=200, expected_fn='InvenTree_SalesOrders.xls', + deode=False, ) as fo: self.assertTrue(isinstance(fo, io.BytesIO)) From b6c2ade940c814d381c430bab71d840c327d997e Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 18 May 2022 07:52:29 +1000 Subject: [PATCH 10/28] Add unit test for downloading Part data --- InvenTree/order/test_api.py | 2 +- InvenTree/part/admin.py | 2 ++ InvenTree/part/test_api.py | 52 +++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/InvenTree/order/test_api.py b/InvenTree/order/test_api.py index 04be484310..f06e26370d 100644 --- a/InvenTree/order/test_api.py +++ b/InvenTree/order/test_api.py @@ -989,7 +989,7 @@ class SalesOrderDownloadTest(OrderTest): }, expected_code=200, expected_fn='InvenTree_SalesOrders.xls', - deode=False, + decode=False, ) as fo: self.assertTrue(isinstance(fo, io.BytesIO)) diff --git a/InvenTree/part/admin.py b/InvenTree/part/admin.py index 8021bd10fa..5fafc37fea 100644 --- a/InvenTree/part/admin.py +++ b/InvenTree/part/admin.py @@ -45,6 +45,7 @@ class PartResource(ModelResource): exclude = [ 'bom_checksum', 'bom_checked_by', 'bom_checked_date', 'lft', 'rght', 'tree_id', 'level', + 'metadata', ] def get_queryset(self): @@ -98,6 +99,7 @@ class PartCategoryResource(ModelResource): exclude = [ # Exclude MPTT internal model fields 'lft', 'rght', 'tree_id', 'level', + 'metadata', ] def after_import(self, dataset, result, using_transactions, dry_run, **kwargs): diff --git a/InvenTree/part/test_api.py b/InvenTree/part/test_api.py index 28df4503c9..46b7a477f8 100644 --- a/InvenTree/part/test_api.py +++ b/InvenTree/part/test_api.py @@ -822,6 +822,58 @@ class PartAPITest(InvenTreeAPITestCase): response = self.get('/api/part/10004/', {}) self.assertEqual(response.data['variant_stock'], 500) + def test_part_download(self): + """Test download of part data via the API""" + + url = reverse('api-part-list') + + required_cols = [ + 'id', + 'name', + 'description', + 'in_stock', + 'category_name', + 'keywords', + 'is_template', + 'virtual', + 'trackable', + 'active', + 'notes', + 'creation_date', + ] + + excluded_cols = [ + 'lft', 'rght', 'level', 'tree_id', + 'metadata', + ] + + with self.download_file( + url, + { + 'export': 'csv', + }, + expected_fn='InvenTree_Parts.csv', + ) as fo: + + data = self.process_csv( + fo, + excluded_cols=excluded_cols, + required_cols=required_cols, + required_rows=Part.objects.count(), + ) + + for row in data: + part = Part.objects.get(pk=row['id']) + + if part.IPN: + self.assertEqual(part.IPN, row['IPN']) + + self.assertEqual(part.name, row['name']) + self.assertEqual(part.description, row['description']) + + if part.category: + self.assertEqual(part.category.name, row['category_name']) + class PartDetailTests(InvenTreeAPITestCase): """ From aa3a86f3728d69aa3132be14513756d3b729fa3b Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 18 May 2022 21:33:40 +1000 Subject: [PATCH 11/28] Exclude metadata from StockLocation and StockItem model resource class --- InvenTree/stock/admin.py | 3 ++- InvenTree/stock/test_api.py | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/InvenTree/stock/admin.py b/InvenTree/stock/admin.py index 3d931e2d7c..4b7cf38bf5 100644 --- a/InvenTree/stock/admin.py +++ b/InvenTree/stock/admin.py @@ -31,6 +31,7 @@ class LocationResource(ModelResource): exclude = [ # Exclude MPTT internal model fields 'lft', 'rght', 'tree_id', 'level', + 'metadata', ] def after_import(self, dataset, result, using_transactions, dry_run, **kwargs): @@ -119,7 +120,7 @@ class StockItemResource(ModelResource): # Exclude MPTT internal model fields 'lft', 'rght', 'tree_id', 'level', # Exclude internal fields - 'serial_int', + 'serial_int', 'metadata', ] diff --git a/InvenTree/stock/test_api.py b/InvenTree/stock/test_api.py index 28a8e0de0b..ccdac8d2c6 100644 --- a/InvenTree/stock/test_api.py +++ b/InvenTree/stock/test_api.py @@ -344,6 +344,13 @@ class StockItemListTest(StockAPITestCase): for h in headers: self.assertIn(h, dataset.headers) + excluded_headers = [ + 'metadata', + ] + + for h in excluded_headers: + self.assertNotIn(h, dataset.headers) + # Now, add a filter to the results dataset = self.export_data({'location': 1}) From c7003fbed85c864177cd3472ac6b7e14e214b433 Mon Sep 17 00:00:00 2001 From: Maksim Stojkovic <18454392+maksimstojkovic@users.noreply.github.com> Date: Wed, 18 May 2022 22:01:47 +1000 Subject: [PATCH 12/28] Create a default shipment when creating SO --- InvenTree/templates/js/translated/order.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/InvenTree/templates/js/translated/order.js b/InvenTree/templates/js/translated/order.js index e2bee865fd..35bd791382 100644 --- a/InvenTree/templates/js/translated/order.js +++ b/InvenTree/templates/js/translated/order.js @@ -362,6 +362,17 @@ function createSalesOrder(options={}) { } }, onSuccess: function(data) { + inventreePut( + '{% url "api-so-shipment-list" %}', + { + order: data.pk, + reference: 1 + }, + { + method: 'POST' + } + ); + location.href = `/order/sales-order/${data.pk}/`; }, title: '{% trans "Create Sales Order" %}', From 9ae8a6b21925318fc525b0674b208e8afcd31a1a Mon Sep 17 00:00:00 2001 From: Maksim Stojkovic <18454392+maksimstojkovic@users.noreply.github.com> Date: Wed, 18 May 2022 23:43:06 +1000 Subject: [PATCH 13/28] Revert "Create a default shipment when creating SO" This reverts commit c7003fbed85c864177cd3472ac6b7e14e214b433. --- InvenTree/templates/js/translated/order.js | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/InvenTree/templates/js/translated/order.js b/InvenTree/templates/js/translated/order.js index 35bd791382..e2bee865fd 100644 --- a/InvenTree/templates/js/translated/order.js +++ b/InvenTree/templates/js/translated/order.js @@ -362,17 +362,6 @@ function createSalesOrder(options={}) { } }, onSuccess: function(data) { - inventreePut( - '{% url "api-so-shipment-list" %}', - { - order: data.pk, - reference: 1 - }, - { - method: 'POST' - } - ); - location.href = `/order/sales-order/${data.pk}/`; }, title: '{% trans "Create Sales Order" %}', From 2cf67ea0c977fdaae0b2cbd0eaec076e7585455f Mon Sep 17 00:00:00 2001 From: Maksim Stojkovic <18454392+maksimstojkovic@users.noreply.github.com> Date: Thu, 19 May 2022 00:10:55 +1000 Subject: [PATCH 14/28] Create default shipment in backend --- InvenTree/common/models.py | 7 +++++++ InvenTree/order/models.py | 16 ++++++++++++++++ InvenTree/templates/InvenTree/settings/so.html | 1 + 3 files changed, 24 insertions(+) diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index 83773fe48a..0ef766d19d 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -1111,6 +1111,13 @@ class InvenTreeSetting(BaseInvenTreeSetting): 'default': 'SO', }, + 'SALESORDER_DEFAULT_SHIPMENT': { + 'name': _('Sales Order Default Shipment'), + 'description': _('Enable creation of default shipment with sales orders'), + 'default': False, + 'validator': bool, + }, + 'PURCHASEORDER_REFERENCE_PREFIX': { 'name': _('Purchase Order Reference Prefix'), 'description': _('Prefix value for purchase order reference'), diff --git a/InvenTree/order/models.py b/InvenTree/order/models.py index 7460e81e56..58f4fd5c2f 100644 --- a/InvenTree/order/models.py +++ b/InvenTree/order/models.py @@ -12,6 +12,8 @@ from decimal import Decimal from django.db import models, transaction from django.db.models import Q, F, Sum from django.db.models.functions import Coalesce +from django.db.models.signals import post_save +from django.dispatch.dispatcher import receiver from django.core.validators import MinValueValidator from django.core.exceptions import ValidationError @@ -808,6 +810,20 @@ class SalesOrder(Order): def pending_shipment_count(self): return self.pending_shipments().count() +@receiver(post_save, sender=SalesOrder, dispatch_uid='build_post_save_log') +def after_save_sales_order(sender, instance: SalesOrder, created: bool, **kwargs): + """ + Callback function to be executed after a SalesOrder instance is saved + """ + if created and getSetting('SALESORDER_DEFAULT_SHIPMENT'): + # A new SalesOrder has just been created + + # Create default shipment + SalesOrderShipment.objects.create( + order=instance, + reference='1', + ) + class PurchaseOrderAttachment(InvenTreeAttachment): """ diff --git a/InvenTree/templates/InvenTree/settings/so.html b/InvenTree/templates/InvenTree/settings/so.html index e6fde3a093..ac84f5fa86 100644 --- a/InvenTree/templates/InvenTree/settings/so.html +++ b/InvenTree/templates/InvenTree/settings/so.html @@ -12,6 +12,7 @@ {% include "InvenTree/settings/setting.html" with key="SALESORDER_REFERENCE_PREFIX" %} + {% include "InvenTree/settings/setting.html" with key="SALESORDER_DEFAULT_SHIPMENT" icon="fa-truck-loading" %}
From 276075ce0508b3f6dfb35a9df41f3094966fb8aa Mon Sep 17 00:00:00 2001 From: Maksim Stojkovic <18454392+maksimstojkovic@users.noreply.github.com> Date: Thu, 19 May 2022 00:33:03 +1000 Subject: [PATCH 15/28] PEP styling --- InvenTree/common/models.py | 2 +- InvenTree/order/models.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index 0ef766d19d..92e5c1522c 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -1117,7 +1117,7 @@ class InvenTreeSetting(BaseInvenTreeSetting): 'default': False, 'validator': bool, }, - + 'PURCHASEORDER_REFERENCE_PREFIX': { 'name': _('Purchase Order Reference Prefix'), 'description': _('Prefix value for purchase order reference'), diff --git a/InvenTree/order/models.py b/InvenTree/order/models.py index 58f4fd5c2f..f4688f3736 100644 --- a/InvenTree/order/models.py +++ b/InvenTree/order/models.py @@ -810,6 +810,7 @@ class SalesOrder(Order): def pending_shipment_count(self): return self.pending_shipments().count() + @receiver(post_save, sender=SalesOrder, dispatch_uid='build_post_save_log') def after_save_sales_order(sender, instance: SalesOrder, created: bool, **kwargs): """ From 0831b85e29cb94f892f2420cec7b7da20282e86e Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Thu, 19 May 2022 01:39:16 +1000 Subject: [PATCH 16/28] Adding some unit tests for SalesOrderLineItem API --- InvenTree/order/test_api.py | 94 +++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/InvenTree/order/test_api.py b/InvenTree/order/test_api.py index f06e26370d..6549b1d89d 100644 --- a/InvenTree/order/test_api.py +++ b/InvenTree/order/test_api.py @@ -381,6 +381,20 @@ class PurchaseOrderDownloadTest(OrderTest): self.assertEqual(order.description, row['description']) self.assertEqual(order.reference, row['reference']) + def test_download_line_items(self): + + with self.download_file( + reverse('api-po-line-list'), + { + 'export': 'xlsx', + }, + decode=False, + expected_code=200, + expected_fn='InvenTree_PurchaseOrderItems.xlsx', + ) as fo: + + self.assertTrue(isinstance(fo, io.BytesIO)) + class PurchaseOrderReceiveTest(OrderTest): """ @@ -967,6 +981,86 @@ class SalesOrderTest(OrderTest): self.assertEqual(order.get_metadata('xyz'), 'abc') +class SalesOrderLineItemTest(OrderTest): + """ + Tests for the SalesOrderLineItem API + """ + + def setUp(self): + + super().setUp() + + # List of salable parts + parts = Part.objects.filter(salable=True) + + # Create a bunch of SalesOrderLineItems for each order + for idx, so in enumerate(models.SalesOrder.objects.all()): + + for part in parts: + models.SalesOrderLineItem.objects.create( + order=so, + part=part, + quantity=(idx + 1) * 5, + reference=f"Order {so.reference} - line {idx}", + ) + + self.url = reverse('api-so-line-list') + + def test_so_line_list(self): + + # List *all* lines + + response = self.get( + self.url, + {}, + expected_code=200, + ) + + n = models.SalesOrderLineItem.objects.count() + + # We should have received *all* lines + self.assertEqual(len(response.data), n) + + # List *all* lines, but paginate + response = self.get( + self.url, + { + "limit": 5, + }, + expected_code=200, + ) + + self.assertEqual(response.data['count'], n) + self.assertEqual(len(response.data['results']), 5) + + n_orders = models.SalesOrder.objects.count() + n_parts = Part.objects.filter(salable=True).count() + + # List by part + for part in Part.objects.filter(salable=True): + response = self.get( + self.url, + { + 'part': part.pk, + 'limit': 10, + } + ) + + self.assertEqual(response.data['count'], n_orders) + + # List by order + for order in models.SalesOrder.objects.all(): + response = self.get( + self.url, + { + 'order': order.pk, + 'limit': 10, + } + ) + + self.assertEqual(response.data['count'], n_parts) + + class SalesOrderDownloadTest(OrderTest): """Unit tests for downloading SalesOrder data via the API endpoint""" From 4dc997c4aecbfd6fd7ab5db8944bbbb50c30c2f7 Mon Sep 17 00:00:00 2001 From: Maksim Stojkovic <18454392+maksimstojkovic@users.noreply.github.com> Date: Thu, 19 May 2022 02:06:43 +1000 Subject: [PATCH 17/28] Added unit test --- InvenTree/order/test_sales_order.py | 37 +++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/InvenTree/order/test_sales_order.py b/InvenTree/order/test_sales_order.py index d43c94996c..954fecf2fe 100644 --- a/InvenTree/order/test_sales_order.py +++ b/InvenTree/order/test_sales_order.py @@ -10,6 +10,8 @@ from company.models import Company from InvenTree import status_codes as status +from common.models import InvenTreeSetting + from order.models import SalesOrder, SalesOrderLineItem, SalesOrderAllocation, SalesOrderShipment from part.models import Part @@ -200,3 +202,38 @@ class SalesOrderTest(TestCase): self.assertTrue(self.line.is_fully_allocated()) self.assertEqual(self.line.fulfilled_quantity(), 50) self.assertEqual(self.line.allocated_quantity(), 50) + + def test_default_shipment(self): + # Test sales order default shipment creation + + # Default setting value should be False + self.assertEqual(False, InvenTreeSetting.get_setting('SALESORDER_DEFAULT_SHIPMENT')) + + # Create an order + order_1 = SalesOrder.objects.create( + customer=self.customer, + reference='1235', + customer_reference='ABC 55556' + ) + + # Order should have no shipments when setting is False + self.assertEqual(0, order_1.shipment_count) + + # Update setting to True + InvenTreeSetting.set_setting('SALESORDER_DEFAULT_SHIPMENT', True, None) + self.assertEqual(True, InvenTreeSetting.get_setting('SALESORDER_DEFAULT_SHIPMENT')) + + # Create a second order + order_2 = SalesOrder.objects.create( + customer=self.customer, + reference='1236', + customer_reference='ABC 55557' + ) + + # Order should have one shipment + self.assertEqual(1, order_2.shipment_count) + self.assertEqual(1, order_2.pending_shipments().count()) + + # Shipment should have default reference of '1' + self.assertEqual('1', order_2.pending_shipments()[0].reference) + \ No newline at end of file From 9e8da0e8e06e1cd359340b8f0475a058ff8fad62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A1lm=C3=A1n=20R=C3=B3zsahegyi?= Date: Wed, 18 May 2022 22:20:42 +0200 Subject: [PATCH 18/28] Fix manufacturer part detail template with missing company view tests --- .../templates/company/manufacturer_part.html | 1 - InvenTree/company/test_views.py | 26 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/InvenTree/company/templates/company/manufacturer_part.html b/InvenTree/company/templates/company/manufacturer_part.html index a51ea45099..aa087fe207 100644 --- a/InvenTree/company/templates/company/manufacturer_part.html +++ b/InvenTree/company/templates/company/manufacturer_part.html @@ -94,7 +94,6 @@ src="{% static 'img/blank_image.png' %}" {% else %} {% trans "No manufacturer information available" %} {% endif %} - {% endif %} diff --git a/InvenTree/company/test_views.py b/InvenTree/company/test_views.py index 6d28e85d23..29900236bf 100644 --- a/InvenTree/company/test_views.py +++ b/InvenTree/company/test_views.py @@ -55,3 +55,29 @@ class CompanyViewTest(CompanyViewTestBase): response = self.client.get(reverse('company-index')) self.assertEqual(response.status_code, 200) + + def test_manufacturer_index(self): + """ Test the manufacturer index """ + + response = self.client.get(reverse('manufacturer-index')) + self.assertEqual(response.status_code, 200) + + def test_customer_index(self): + """ Test the customer index """ + + response = self.client.get(reverse('customer-index')) + self.assertEqual(response.status_code, 200) + + def test_manufacturer_part_detail_view(self): + """ Test the manufacturer part detail view """ + + response = self.client.get(reverse('manufacturer-part-detail', kwargs={'pk': 1})) + self.assertEqual(response.status_code, 200) + self.assertContains(response, 'MPN123') + + def test_supplier_part_detail_view(self): + """ Test the supplier part detail view """ + + response = self.client.get(reverse('supplier-part-detail', kwargs={'pk': 10})) + self.assertEqual(response.status_code, 200) + self.assertContains(response, 'MPN456-APPEL') From 9446702d78bc34900ebb338f34f79c032467463a Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 19 May 2022 09:36:14 +1000 Subject: [PATCH 19/28] Skip plugin loading for various database admin functions --- InvenTree/InvenTree/ready.py | 3 ++- InvenTree/plugin/apps.py | 10 ++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/InvenTree/InvenTree/ready.py b/InvenTree/InvenTree/ready.py index 9f5ad0ea49..e93972cf2e 100644 --- a/InvenTree/InvenTree/ready.py +++ b/InvenTree/InvenTree/ready.py @@ -39,7 +39,8 @@ def canAppAccessDatabase(allow_test=False): 'createsuperuser', 'wait_for_db', 'prerender', - 'rebuild', + 'rebuild_models', + 'rebuild_thumbnails', 'collectstatic', 'makemessages', 'compilemessages', diff --git a/InvenTree/plugin/apps.py b/InvenTree/plugin/apps.py index 233f037ec7..a176612fb6 100644 --- a/InvenTree/plugin/apps.py +++ b/InvenTree/plugin/apps.py @@ -7,7 +7,7 @@ from django.utils.translation import gettext_lazy as _ from maintenance_mode.core import set_maintenance_mode -from InvenTree.ready import isImportingData +from InvenTree.ready import canAppAccessDatabase from plugin import registry from plugin.helpers import check_git_version, log_error @@ -20,9 +20,8 @@ class PluginAppConfig(AppConfig): def ready(self): if settings.PLUGINS_ENABLED: - - if isImportingData(): # pragma: no cover - logger.info('Skipping plugin loading for data import') + if not canAppAccessDatabase(allow_test=True): + logger.info("Skipping plugin loading sequence") else: logger.info('Loading InvenTree plugins') @@ -48,3 +47,6 @@ class PluginAppConfig(AppConfig): registry.git_is_modern = check_git_version() if not registry.git_is_modern: # pragma: no cover # simulating old git seems not worth it for coverage log_error(_('Your enviroment has an outdated git version. This prevents InvenTree from loading plugin details.'), 'load') + + else: + logger.info("Plugins not enabled - skipping loading sequence") \ No newline at end of file From 7d9690b974263ba499d026eabee504b5bd6cb8ac Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 19 May 2022 09:53:12 +1000 Subject: [PATCH 20/28] Add logging message when plugin fails to render custom panels --- InvenTree/plugin/views.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/InvenTree/plugin/views.py b/InvenTree/plugin/views.py index 8d28872695..9b12ef12fe 100644 --- a/InvenTree/plugin/views.py +++ b/InvenTree/plugin/views.py @@ -1,3 +1,4 @@ +import logging import sys import traceback @@ -9,6 +10,9 @@ from error_report.models import Error from plugin.registry import registry +logger = logging.getLogger('inventree') + + class InvenTreePluginViewMixin: """ Custom view mixin which adds context data to the view, @@ -42,6 +46,8 @@ class InvenTreePluginViewMixin: html=ExceptionReporter(self.request, kind, info, data).get_traceback_html(), ) + logger.error(f"Plugin '{plug.slug}' could not render custom panels at '{self.request.path}'") + return panels def get_context_data(self, **kwargs): From 14b60cdedcde7623fd15e4471ead66a5febe9386 Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 19 May 2022 10:03:44 +1000 Subject: [PATCH 21/28] Custom panel content gets passed through the templating engine --- InvenTree/plugin/base/integration/mixins.py | 9 ++++++++- InvenTree/plugin/helpers.py | 11 +++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/InvenTree/plugin/base/integration/mixins.py b/InvenTree/plugin/base/integration/mixins.py index 64de5df22b..c347d6c406 100644 --- a/InvenTree/plugin/base/integration/mixins.py +++ b/InvenTree/plugin/base/integration/mixins.py @@ -11,7 +11,8 @@ from django.db.utils import OperationalError, ProgrammingError import InvenTree.helpers -from plugin.helpers import MixinImplementationError, MixinNotImplementedError, render_template +from plugin.helpers import MixinImplementationError, MixinNotImplementedError +from plugin.helpers import render_template, render_text from plugin.models import PluginConfig, PluginSetting from plugin.registry import registry from plugin.urls import PLUGIN_BASE @@ -578,10 +579,16 @@ class PanelMixin: if content_template: # Render content template to HTML panel['content'] = render_template(self, content_template, ctx) + else: + # Render content string to HTML + panel['content'] = render_text(panel.get('content', ''), ctx) if javascript_template: # Render javascript template to HTML panel['javascript'] = render_template(self, javascript_template, ctx) + else: + # Render javascript string to HTML + panel['javascript'] = render_text(panel.get('javascript', ''), ctx) # Check for required keys required_keys = ['title', 'content'] diff --git a/InvenTree/plugin/helpers.py b/InvenTree/plugin/helpers.py index 1217fa4d47..90ffe61478 100644 --- a/InvenTree/plugin/helpers.py +++ b/InvenTree/plugin/helpers.py @@ -245,4 +245,15 @@ def render_template(plugin, template_file, context=None): html = tmp.render(context) return html + + +def render_text(text, context=None): + """ + Locate a raw string with provided context + """ + + ctx = template.Context(context) + + return template.Template(text).render(ctx) + # endregion From ebcb9685b56dc293bfca2a07bb8c139e75f39abd Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 19 May 2022 10:04:20 +1000 Subject: [PATCH 22/28] Updates to samplepanel plugin - Enhanced content for "hello world" panel - Add an optional panel which breaks rendering --- .../plugin/samples/event/event_sample.py | 2 +- .../integration/custom_panel_sample.py | 39 +++++++++++++++++-- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/InvenTree/plugin/samples/event/event_sample.py b/InvenTree/plugin/samples/event/event_sample.py index 5411781e05..bea21c3ea0 100644 --- a/InvenTree/plugin/samples/event/event_sample.py +++ b/InvenTree/plugin/samples/event/event_sample.py @@ -12,7 +12,7 @@ class EventPluginSample(EventMixin, InvenTreePlugin): """ NAME = "EventPlugin" - SLUG = "event" + SLUG = "sampleevent" TITLE = "Triggered Events" def process_event(self, event, *args, **kwargs): diff --git a/InvenTree/plugin/samples/integration/custom_panel_sample.py b/InvenTree/plugin/samples/integration/custom_panel_sample.py index 0203fc4e04..dd84a2a86f 100644 --- a/InvenTree/plugin/samples/integration/custom_panel_sample.py +++ b/InvenTree/plugin/samples/integration/custom_panel_sample.py @@ -15,17 +15,23 @@ class CustomPanelSample(PanelMixin, SettingsMixin, InvenTreePlugin): """ NAME = "CustomPanelExample" - SLUG = "panel" + SLUG = "samplepanel" TITLE = "Custom Panel Example" DESCRIPTION = "An example plugin demonstrating how custom panels can be added to the user interface" VERSION = "0.1" SETTINGS = { 'ENABLE_HELLO_WORLD': { - 'name': 'Hello World', + 'name': 'Enable Hello World', 'description': 'Enable a custom hello world panel on every page', 'default': False, 'validator': bool, + }, + 'ENABLE_BROKEN_PANEL': { + 'name': 'Enable Broken Panel', + 'description': 'Enable a panel with rendering issues', + 'default': False, + 'validator': bool, } } @@ -58,15 +64,42 @@ class CustomPanelSample(PanelMixin, SettingsMixin, InvenTreePlugin): ] if self.get_setting('ENABLE_HELLO_WORLD'): + + # We can use template rendering in the raw content + content = """ + Hello world! +
+
+ We can render custom content using the templating system! +
+
+ + + +
Path{{ request.path }}
User{{ user.username }}
+ """ + panels.append({ # This 'hello world' panel will be displayed on any view which implements custom panels 'title': 'Hello World', 'icon': 'fas fa-boxes', - 'content': 'Hello world!', + 'content': content, 'description': 'A simple panel which renders hello world', 'javascript': 'console.log("Hello world, from a custom panel!");', }) + if self.get_setting('ENABLE_BROKEN_PANEL'): + + # Enabling this panel will cause panel rendering to break, + # due to the invalid tags + panels.append({ + 'title': 'Broken Panel', + 'icon': 'fas fa-times-circle', + 'content': '{% tag_not_loaded %}', + 'description': 'This panel is broken', + 'javascript': '{% another_bad_tag %}', + }) + # This panel will *only* display on the PartDetail view if isinstance(view, PartDetail): panels.append({ From 11b21a9cca8a7a9e2f7493caeda3038264196036 Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 19 May 2022 11:00:31 +1000 Subject: [PATCH 23/28] Allow registry.with_mixin to filter by active status --- InvenTree/plugin/base/integration/mixins.py | 1 + InvenTree/plugin/registry.py | 10 +++++++++- .../plugin/samples/integration/custom_panel_sample.py | 2 +- InvenTree/plugin/views.py | 2 +- 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/InvenTree/plugin/base/integration/mixins.py b/InvenTree/plugin/base/integration/mixins.py index c347d6c406..6977ef3dd9 100644 --- a/InvenTree/plugin/base/integration/mixins.py +++ b/InvenTree/plugin/base/integration/mixins.py @@ -60,6 +60,7 @@ class SettingsMixin: if not plugin: # Cannot find associated plugin model, return + logger.error(f"Plugin configuration not found for plugin '{self.slug}'") return # pragma: no cover PluginSetting.set_setting(key, value, user, plugin=plugin) diff --git a/InvenTree/plugin/registry.py b/InvenTree/plugin/registry.py index 3d58634340..d97fa73923 100644 --- a/InvenTree/plugin/registry.py +++ b/InvenTree/plugin/registry.py @@ -243,7 +243,7 @@ class PluginsRegistry: # endregion # region registry functions - def with_mixin(self, mixin: str): + def with_mixin(self, mixin: str, active=None): """ Returns reference to all plugins that have a specified mixin enabled """ @@ -251,6 +251,14 @@ class PluginsRegistry: for plugin in self.plugins.values(): if plugin.mixin_enabled(mixin): + + if active is not None: + # Filter by 'enabled' status + config = plugin.plugin_config() + + if config.active != active: + continue + result.append(plugin) return result diff --git a/InvenTree/plugin/samples/integration/custom_panel_sample.py b/InvenTree/plugin/samples/integration/custom_panel_sample.py index dd84a2a86f..3d44bc0c5b 100644 --- a/InvenTree/plugin/samples/integration/custom_panel_sample.py +++ b/InvenTree/plugin/samples/integration/custom_panel_sample.py @@ -58,7 +58,7 @@ class CustomPanelSample(PanelMixin, SettingsMixin, InvenTreePlugin): panels = [ { - # This panel will not be displayed, as it is missing the 'content' key + # Simple panel without any actual content 'title': 'No Content', } ] diff --git a/InvenTree/plugin/views.py b/InvenTree/plugin/views.py index 9b12ef12fe..ad4d54daea 100644 --- a/InvenTree/plugin/views.py +++ b/InvenTree/plugin/views.py @@ -29,7 +29,7 @@ class InvenTreePluginViewMixin: panels = [] - for plug in registry.with_mixin('panel'): + for plug in registry.with_mixin('panel', active=True): try: panels += plug.render_panels(self, self.request, ctx) From 80e3d0970a5b938c0ff3f5459aa745a8bbe42aed Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 19 May 2022 11:28:18 +1000 Subject: [PATCH 24/28] Adds unit tests for the samplepanel plugin --- InvenTree/plugin/apps.py | 2 +- .../plugin/base/integration/test_mixins.py | 158 +++++++++++++++++- InvenTree/plugin/registry.py | 2 +- setup.cfg | 2 +- 4 files changed, 160 insertions(+), 4 deletions(-) diff --git a/InvenTree/plugin/apps.py b/InvenTree/plugin/apps.py index a176612fb6..c0e894fef1 100644 --- a/InvenTree/plugin/apps.py +++ b/InvenTree/plugin/apps.py @@ -49,4 +49,4 @@ class PluginAppConfig(AppConfig): log_error(_('Your enviroment has an outdated git version. This prevents InvenTree from loading plugin details.'), 'load') else: - logger.info("Plugins not enabled - skipping loading sequence") \ No newline at end of file + logger.info("Plugins not enabled - skipping loading sequence") diff --git a/InvenTree/plugin/base/integration/test_mixins.py b/InvenTree/plugin/base/integration/test_mixins.py index ef3f7062e3..4768020bf1 100644 --- a/InvenTree/plugin/base/integration/test_mixins.py +++ b/InvenTree/plugin/base/integration/test_mixins.py @@ -2,14 +2,17 @@ from django.test import TestCase from django.conf import settings -from django.urls import include, re_path +from django.urls import include, re_path, reverse from django.contrib.auth import get_user_model +from django.contrib.auth.models import Group from plugin import InvenTreePlugin from plugin.mixins import AppMixin, SettingsMixin, UrlsMixin, NavigationMixin, APICallMixin from plugin.urls import PLUGIN_BASE from plugin.helpers import MixinNotImplementedError +from plugin.registry import registry + class BaseMixinDefinition: def test_mixin_name(self): @@ -244,3 +247,156 @@ class APICallMixinTest(BaseMixinDefinition, TestCase): # cover wrong token setting with self.assertRaises(MixinNotImplementedError): self.mixin_wrong2.has_api_call() + + +class PanelMixinTests(TestCase): + """Test that the PanelMixin plugin operates correctly""" + + fixtures = [ + 'category', + 'part', + 'location', + 'stock', + ] + + def setUp(self): + super().setUp() + + # Create a user which has all the privelages + user = get_user_model() + + self.user = user.objects.create_user( + username='username', + email='user@email.com', + password='password' + ) + + # Put the user into a group with the correct permissions + group = Group.objects.create(name='mygroup') + self.user.groups.add(group) + + # Give the group *all* the permissions! + for rule in group.rule_sets.all(): + rule.can_view = True + rule.can_change = True + rule.can_add = True + rule.can_delete = True + + rule.save() + + self.client.login(username='username', password='password') + + def test_installed(self): + """Test that the sample panel plugin is installed""" + + plugins = registry.with_mixin('panel') + + self.assertTrue(len(plugins) > 0) + + self.assertIn('samplepanel', [p.slug for p in plugins]) + + plugins = registry.with_mixin('panel', active=True) + + self.assertEqual(len(plugins), 0) + + def test_disabled(self): + """Test that the panels *do not load* if the plugin is not enabled""" + + plugin = registry.get_plugin('samplepanel') + + plugin.set_setting('ENABLE_HELLO_WORLD', True) + plugin.set_setting('ENABLE_BROKEN_PANEL', True) + + # Ensure that the plugin is *not* enabled + config = plugin.plugin_config() + + self.assertFalse(config.active) + + # Load some pages, ensure that the panel content is *not* loaded + for url in [ + reverse('part-detail', kwargs={'pk': 1}), + reverse('stock-item-detail', kwargs={'pk': 2}), + reverse('stock-location-detail', kwargs={'pk': 1}), + ]: + response = self.client.get( + url + ) + + self.assertEqual(response.status_code, 200) + + # Test that these panels have *not* been loaded + self.assertNotIn('No Content', str(response.content)) + self.assertNotIn('Hello world', str(response.content)) + self.assertNotIn('Custom Part Panel', str(response.content)) + + def test_enabled(self): + """ + Test that the panels *do* load if the plugin is enabled + """ + + plugin = registry.get_plugin('samplepanel') + + self.assertEqual(len(registry.with_mixin('panel', active=True)), 0) + + # Ensure that the plugin is enabled + config = plugin.plugin_config() + config.active = True + config.save() + + self.assertTrue(config.active) + self.assertEqual(len(registry.with_mixin('panel', active=True)), 1) + + # Load some pages, ensure that the panel content is *not* loaded + urls = [ + reverse('part-detail', kwargs={'pk': 1}), + reverse('stock-item-detail', kwargs={'pk': 2}), + reverse('stock-location-detail', kwargs={'pk': 1}), + ] + + plugin.set_setting('ENABLE_HELLO_WORLD', False) + plugin.set_setting('ENABLE_BROKEN_PANEL', False) + + for url in urls: + response = self.client.get(url) + + self.assertEqual(response.status_code, 200) + + self.assertIn('No Content', str(response.content)) + + # This panel is disabled by plugin setting + self.assertNotIn('Hello world!', str(response.content)) + + # This panel is only active for the "Part" view + if url == urls[0]: + self.assertIn('Custom Part Panel', str(response.content)) + else: + self.assertNotIn('Custom Part Panel', str(response.content)) + + # Enable the 'Hello World' panel + plugin.set_setting('ENABLE_HELLO_WORLD', True) + + for url in urls: + response = self.client.get(url) + + self.assertEqual(response.status_code, 200) + + self.assertIn('Hello world!', str(response.content)) + + # The 'Custom Part' panel should still be there, too + if url == urls[0]: + self.assertIn('Custom Part Panel', str(response.content)) + else: + self.assertNotIn('Custom Part Panel', str(response.content)) + + # Enable the 'broken panel' setting - this will cause all panels to not render + plugin.set_setting('ENABLE_BROKEN_PANEL', True) + + for url in urls: + response = self.client.get(url) + self.assertEqual(response.status_code, 200) + + # No custom panels should have been loaded + self.assertNotIn('No Content', str(response.content)) + self.assertNotIn('Hello world!', str(response.content)) + self.assertNotIn('Broken Panel', str(response.content)) + self.assertNotIn('Custom Part Panel', str(response.content)) diff --git a/InvenTree/plugin/registry.py b/InvenTree/plugin/registry.py index d97fa73923..1ec5adb161 100644 --- a/InvenTree/plugin/registry.py +++ b/InvenTree/plugin/registry.py @@ -255,7 +255,7 @@ class PluginsRegistry: if active is not None: # Filter by 'enabled' status config = plugin.plugin_config() - + if config.active != active: continue diff --git a/setup.cfg b/setup.cfg index a483481f5d..0aeaf4d01b 100644 --- a/setup.cfg +++ b/setup.cfg @@ -15,7 +15,7 @@ ignore = N806, # - N812 - lowercase imported as non-lowercase N812, -exclude = .git,__pycache__,*/migrations/*,*/lib/*,*/bin/*,*/media/*,*/static/* +exclude = .git,__pycache__,*/migrations/*,*/lib/*,*/bin/*,*/media/*,*/static/*,InvenTree/plugins/* max-complexity = 20 [coverage:run] From af88f6ec979fae4835ba06deaa8dbe29c3d656f7 Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 19 May 2022 11:55:53 +1000 Subject: [PATCH 25/28] python CI: wait for server before continuing --- .github/workflows/qc_checks.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/qc_checks.yaml b/.github/workflows/qc_checks.yaml index e73a1e8f98..93b208451b 100644 --- a/.github/workflows/qc_checks.yaml +++ b/.github/workflows/qc_checks.yaml @@ -153,6 +153,7 @@ jobs: invoke delete-data -f invoke import-fixtures invoke server -a 127.0.0.1:12345 & + invoke wait - name: Run Tests run: | cd ${{ env.wrapper_name }} From 140006a4cdefefdb37eb9a6f1b6293fb2c4e2a1b Mon Sep 17 00:00:00 2001 From: Maksim Stojkovic <18454392+maksimstojkovic@users.noreply.github.com> Date: Thu, 19 May 2022 12:19:55 +1000 Subject: [PATCH 26/28] Fix PEP styles --- InvenTree/order/test_sales_order.py | 1 - 1 file changed, 1 deletion(-) diff --git a/InvenTree/order/test_sales_order.py b/InvenTree/order/test_sales_order.py index 954fecf2fe..cbd572e24d 100644 --- a/InvenTree/order/test_sales_order.py +++ b/InvenTree/order/test_sales_order.py @@ -236,4 +236,3 @@ class SalesOrderTest(TestCase): # Shipment should have default reference of '1' self.assertEqual('1', order_2.pending_shipments()[0].reference) - \ No newline at end of file From adaec90909d2a081d75f756df0be11898167507b Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 19 May 2022 12:54:07 +1000 Subject: [PATCH 27/28] CI: Allow exchange rate test a few goes --- InvenTree/InvenTree/tests.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index 501eed0834..26b50a0eca 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -1,5 +1,6 @@ import json import os +import time from unittest import mock @@ -406,11 +407,23 @@ class CurrencyTests(TestCase): with self.assertRaises(MissingRate): convert_money(Money(100, 'AUD'), 'USD') - InvenTree.tasks.update_exchange_rates() + update_successful = False - rates = Rate.objects.all() + # Note: the update sometimes fails in CI, let's give it a few chances + for idx in range(10): + InvenTree.tasks.update_exchange_rates() - self.assertEqual(rates.count(), len(currency_codes())) + rates = Rate.objects.all() + + if rates.count() == len(currency_codes()): + update_successful = True + break + + else: + print("Exchange rate update failed - retrying") + time.sleep(1) + + self.assertTrue(update_successful) # Now that we have some exchange rate information, we can perform conversions From 07319731d20e7dbffc106bda2a86517040be0734 Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 19 May 2022 13:20:42 +1000 Subject: [PATCH 28/28] Validate that errors get logged --- InvenTree/plugin/base/integration/test_mixins.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/InvenTree/plugin/base/integration/test_mixins.py b/InvenTree/plugin/base/integration/test_mixins.py index 4768020bf1..c1afa39fc2 100644 --- a/InvenTree/plugin/base/integration/test_mixins.py +++ b/InvenTree/plugin/base/integration/test_mixins.py @@ -6,6 +6,8 @@ from django.urls import include, re_path, reverse from django.contrib.auth import get_user_model from django.contrib.auth.models import Group +from error_report.models import Error + from plugin import InvenTreePlugin from plugin.mixins import AppMixin, SettingsMixin, UrlsMixin, NavigationMixin, APICallMixin from plugin.urls import PLUGIN_BASE @@ -391,6 +393,8 @@ class PanelMixinTests(TestCase): # Enable the 'broken panel' setting - this will cause all panels to not render plugin.set_setting('ENABLE_BROKEN_PANEL', True) + n_errors = Error.objects.count() + for url in urls: response = self.client.get(url) self.assertEqual(response.status_code, 200) @@ -400,3 +404,6 @@ class PanelMixinTests(TestCase): self.assertNotIn('Hello world!', str(response.content)) self.assertNotIn('Broken Panel', str(response.content)) self.assertNotIn('Custom Part Panel', str(response.content)) + + # Assert that each request threw an error + self.assertEqual(Error.objects.count(), n_errors + len(urls))