From 495798dc98e5f3154db4b8e4b5f7cb2c664ee9ed Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 15 May 2022 23:20:12 +1000 Subject: [PATCH 01/42] Install libwebp-dev as part of dockerfile --- docker/Dockerfile | 2 +- requirements.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index cefd2c2b61..4c6a351adc 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -62,7 +62,7 @@ RUN apk -U upgrade RUN apk add --no-cache git make bash \ gcc libgcc g++ libstdc++ \ gnupg \ - libjpeg-turbo libjpeg-turbo-dev jpeg jpeg-dev \ + libjpeg-turbo libjpeg-turbo-dev jpeg jpeg-dev libwebp-dev \ libffi libffi-dev \ zlib zlib-dev \ # Special deps for WeasyPrint (these will be deprecated once WeasyPrint drops cairo requirement) diff --git a/requirements.txt b/requirements.txt index 5065b4f877..2369b18b44 100644 --- a/requirements.txt +++ b/requirements.txt @@ -38,7 +38,7 @@ importlib_metadata # Backport for importlib.metadata inventree # Install the latest version of the InvenTree API python library markdown==3.3.4 # Force particular version of markdown pep8-naming==0.11.1 # PEP naming convention extension -pillow==9.0.1 # Image manipulation +pillow==9.1.0 # Image manipulation py-moneyed==0.8.0 # Specific version requirement for py-moneyed pygments==2.7.4 # Syntax highlighting python-barcode[images]==0.13.1 # Barcode generator From 55f87033b25fd9e684c1d53ba281b09f1cc5ccf9 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 15 May 2022 23:36:41 +1000 Subject: [PATCH 02/42] Add unit tests for .webp support --- InvenTree/part/test_api.py | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/InvenTree/part/test_api.py b/InvenTree/part/test_api.py index f0770eb1f5..4e4162341a 100644 --- a/InvenTree/part/test_api.py +++ b/InvenTree/part/test_api.py @@ -970,24 +970,29 @@ class PartDetailTests(InvenTreeAPITestCase): ) self.assertEqual(response.status_code, 400) + self.assertIn('Upload a valid image', str(response.data)) - # Now try to upload a valid image file - img = PIL.Image.new('RGB', (128, 128), color='red') - img.save('dummy_image.jpg') + # Now try to upload a valid image file, in multiple formats + for fmt in ['jpg', 'png', 'bmp', 'webp']: + fn = f'dummy_image.{fmt}' - with open('dummy_image.jpg', 'rb') as dummy_image: - response = upload_client.patch( - url, - { - 'image': dummy_image, - }, - format='multipart', - ) + img = PIL.Image.new('RGB', (128, 128), color='red') + img.save(fn) - self.assertEqual(response.status_code, 200) + with open(fn, 'rb') as dummy_image: + response = upload_client.patch( + url, + { + 'image': dummy_image, + }, + format='multipart', + ) - # And now check that the image has been set - p = Part.objects.get(pk=pk) + self.assertEqual(response.status_code, 200) + + # And now check that the image has been set + p = Part.objects.get(pk=pk) + self.assertIsNotNone(p.image) def test_details(self): """ From 47269a88d2d04dc2457e5b6bb7f17c3868595640 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 15 May 2022 23:37:01 +1000 Subject: [PATCH 03/42] Ensure unit tests are run within a docker context as part of CI builds --- .github/workflows/docker_test.yaml | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/.github/workflows/docker_test.yaml b/.github/workflows/docker_test.yaml index d96621ee66..69fbb48fe7 100644 --- a/.github/workflows/docker_test.yaml +++ b/.github/workflows/docker_test.yaml @@ -3,9 +3,8 @@ # This CI action runs on pushes to either the master or stable branches # 1. Build the development docker image (as per the documentation) -# 2. Install requied python libs into the docker container -# 3. Launch the container -# 4. Check that the API endpoint is available +# 2. Launch the development server, and update the installation +# 3. Run unit tests within the docker context name: Docker Test @@ -15,6 +14,10 @@ on: - 'master' - 'stable' + pull_request: + branches-ignore: + - l10* + jobs: docker: @@ -26,12 +29,14 @@ jobs: - name: Build Docker Image run: | cd docker - docker-compose -f docker-compose.sqlite.yml build - docker-compose -f docker-compose.sqlite.yml run inventree-dev-server invoke update - docker-compose -f docker-compose.sqlite.yml up -d - - name: Sleepy Time - run: sleep 60 - - name: Test API + docker-compose build + docker-compose run inventree-dev-server invoke update + docker-compose up -d + - name: Wait for Server run: | - pip install requests - python3 ci/check_api_endpoint.py + cd docker + docker-compose run inventree-dev-server invoke wait + - name: Run unit tests + run: | + cd docker + docker-compose run inventree-dev-server invoke test From 206da0232867c8f3b0574649012c8d4337640e4b Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 16 May 2022 00:21:05 +1000 Subject: [PATCH 04/42] Skip some git hash checks if running tests under docker --- InvenTree/part/test_part.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/InvenTree/part/test_part.py b/InvenTree/part/test_part.py index 5932c36757..2241b68c0d 100644 --- a/InvenTree/part/test_part.py +++ b/InvenTree/part/test_part.py @@ -5,6 +5,7 @@ from __future__ import unicode_literals from allauth.account.models import EmailAddress +from django.conf import settings from django.contrib.auth import get_user_model from django.test import TestCase @@ -67,11 +68,21 @@ class TemplateTagTest(TestCase): def test_hash(self): result_hash = inventree_extras.inventree_commit_hash() - self.assertGreater(len(result_hash), 5) + if settings.DOCKER: + # Testing inside docker environment *may* return an empty git commit hash + # In such a case, skip this check + pass + else: + self.assertGreater(len(result_hash), 5) def test_date(self): d = inventree_extras.inventree_commit_date() - self.assertEqual(len(d.split('-')), 3) + if settings.DOCKER: + # Testing inside docker environment *may* return an empty git commit hash + # In such a case, skip this check + pass + else: + self.assertEqual(len(d.split('-')), 3) def test_github(self): self.assertIn('github.com', inventree_extras.inventree_github_url()) From 7e6d3d81b99e9d741056a3f22dba50df20d08de4 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 16 May 2022 21:47:09 +1000 Subject: [PATCH 05/42] Update dockerfile to 3.14 --- docker/Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index 4c6a351adc..1b7c16db30 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,4 +1,4 @@ -FROM alpine:3.13 as base +FROM alpine:3.14 as base # GitHub source ARG repository="https://github.com/inventree/InvenTree.git" @@ -68,7 +68,7 @@ RUN apk add --no-cache git make bash \ # Special deps for WeasyPrint (these will be deprecated once WeasyPrint drops cairo requirement) cairo cairo-dev pango pango-dev gdk-pixbuf \ # Fonts - fontconfig ttf-droid ttf-liberation ttf-dejavu ttf-opensans ttf-ubuntu-font-family font-croscore font-noto \ + fontconfig ttf-droid ttf-liberation ttf-dejavu ttf-opensans font-croscore font-noto \ # Core python python3 python3-dev py3-pip \ # SQLite support From b630fb285615260b660cefbd3b879d1c74436155 Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 16 May 2022 16:41:00 +0200 Subject: [PATCH 06/42] update envguard import --- InvenTree/InvenTree/tests.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index dc3aff85e6..97bd77a1e1 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -1,5 +1,6 @@ import json -from test.support import EnvironmentVarGuard + +from test import support from django.test import TestCase, override_settings import django.core.exceptions as django_exceptions @@ -449,7 +450,7 @@ class TestSettings(TestCase): def setUp(self) -> None: self.user_mdl = get_user_model() - self.env = EnvironmentVarGuard() + self.env = support.EnvironmentVarGuard() def run_reload(self): from plugin import registry From 825c50a43808b62b5bdd13ee3ca57dc8688859e8 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 17 May 2022 08:40:43 +1000 Subject: [PATCH 07/42] Change import style --- InvenTree/InvenTree/tests.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index dc3aff85e6..5ec8a863bc 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -1,5 +1,5 @@ import json -from test.support import EnvironmentVarGuard +from test import support from django.test import TestCase, override_settings import django.core.exceptions as django_exceptions @@ -449,7 +449,7 @@ class TestSettings(TestCase): def setUp(self) -> None: self.user_mdl = get_user_model() - self.env = EnvironmentVarGuard() + self.env = support.EnvironmentVarGuard() def run_reload(self): from plugin import registry From a40f189c7a6e2687306f15648e0d34d17a2bbfcd Mon Sep 17 00:00:00 2001 From: Matthias Mair Date: Tue, 17 May 2022 19:23:50 +0200 Subject: [PATCH 08/42] Use unierest mock for env setting --- InvenTree/InvenTree/tests.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index 97bd77a1e1..e1e2900d1c 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -1,6 +1,6 @@ import json -from test import support +from unittest import mock from django.test import TestCase, override_settings import django.core.exceptions as django_exceptions @@ -450,12 +450,11 @@ class TestSettings(TestCase): def setUp(self) -> None: self.user_mdl = get_user_model() - self.env = support.EnvironmentVarGuard() - def run_reload(self): + def run_reload(self, envs): from plugin import registry - with self.env: + with mock.patch.dict(os.environ, envs): settings.USER_ADDED = False registry.reload_plugins() @@ -471,15 +470,17 @@ class TestSettings(TestCase): self.assertEqual(user_count(), 0) # not enough set - self.env.set('INVENTREE_ADMIN_USER', 'admin') # set username - self.run_reload() + envs = {} + envs['INVENTREE_ADMIN_USER'] = 'admin' + self.run_reload(envs) self.assertEqual(user_count(), 0) # enough set - self.env.set('INVENTREE_ADMIN_USER', 'admin') # set username - self.env.set('INVENTREE_ADMIN_EMAIL', 'info@example.com') # set email - self.env.set('INVENTREE_ADMIN_PASSWORD', 'password123') # set password - self.run_reload() + envs = {'INVENTREE_ADMIN_USER': 'admin', # set username + 'INVENTREE_ADMIN_EMAIL': 'info@example.com', # set email + 'INVENTREE_ADMIN_PASSWORD': 'password123' # set password + } + self.run_reload(envs) self.assertEqual(user_count(), 1) # make sure to clean up From 6e19187929fa19334c571f43be76528e9719792b Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 18 May 2022 02:01:25 +0200 Subject: [PATCH 09/42] add missing import --- InvenTree/InvenTree/tests.py | 1 + 1 file changed, 1 insertion(+) diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index 08579b29a7..c569310049 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -1,4 +1,5 @@ import json +import os from unittest import mock From 9f0b00cc0eaace16a9ca609168c8aa94d102c7ca Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 18 May 2022 02:01:52 +0200 Subject: [PATCH 10/42] replace old function --- InvenTree/InvenTree/tests.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index c569310049..09bbe569ce 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -514,8 +514,7 @@ class TestSettings(TestCase): self.assertIn('InvenTree/InvenTree/config.yaml', config.get_config_file()) # with env set - with self.env: - self.env.set('INVENTREE_CONFIG_FILE', 'my_special_conf.yaml') + with mock.patch.dict(os.environ, {'INVENTREE_CONFIG_FILE': 'my_special_conf.yaml'}): self.assertIn('InvenTree/InvenTree/my_special_conf.yaml', config.get_config_file()) def test_helpers_plugin_file(self): @@ -523,8 +522,7 @@ class TestSettings(TestCase): self.assertIn('InvenTree/InvenTree/plugins.txt', config.get_plugin_file()) # with env set - with self.env: - self.env.set('INVENTREE_PLUGIN_FILE', 'my_special_plugins.txt') + with mock.patch.dict(os.environ, {'INVENTREE_PLUGIN_FILE': 'my_special_plugins.txt'}): self.assertIn('my_special_plugins.txt', config.get_plugin_file()) def test_helpers_setting(self): @@ -533,8 +531,7 @@ class TestSettings(TestCase): self.assertEqual(config.get_setting(TEST_ENV_NAME, None, '123!'), '123!') # with env set - with self.env: - self.env.set(TEST_ENV_NAME, '321') + with mock.patch.dict(os.environ, {'TEST_ENV_NAME': '321'}): self.assertEqual(config.get_setting(TEST_ENV_NAME, None), '321') From ca7fb691acc04a07d5b28aa4f7cec1a924a46953 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 18 May 2022 02:02:14 +0200 Subject: [PATCH 11/42] make change patch simpler --- InvenTree/InvenTree/tests.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index 09bbe569ce..6e7b9c5444 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -476,18 +476,17 @@ class TestSettings(TestCase): self.assertEqual(user_count(), 1) # not enough set - envs = {} - envs['INVENTREE_ADMIN_USER'] = 'admin' - self.run_reload(envs) + self.run_reload({ + 'INVENTREE_ADMIN_USER': 'admin' + }) self.assertEqual(user_count(), 0) # enough set - envs = { + self.run_reload({ 'INVENTREE_ADMIN_USER': 'admin', # set username 'INVENTREE_ADMIN_EMAIL': 'info@example.com', # set email 'INVENTREE_ADMIN_PASSWORD': 'password123' # set password - } - self.run_reload(envs) + }) self.assertEqual(user_count(), 1) # make sure to clean up From a570dab5e5ada31529889abfd6b74113757e46dc Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 18 May 2022 02:04:15 +0200 Subject: [PATCH 12/42] generalise function to make new methods simpler --- InvenTree/InvenTree/tests.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index 6e7b9c5444..02c95192df 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -457,10 +457,14 @@ class TestSettings(TestCase): self.user = user.objects.create_superuser('testuser1', 'test1@testing.com', 'password1') self.client.login(username='testuser1', password='password1') + def in_env_context(self, envs={}): + """Patch the env to include the given dict""" + return mock.patch.dict(os.environ, envs) + def run_reload(self, envs): from plugin import registry - with mock.patch.dict(os.environ, envs): + with self.in_env_context(envs): settings.USER_ADDED = False registry.reload_plugins() @@ -513,7 +517,7 @@ class TestSettings(TestCase): self.assertIn('InvenTree/InvenTree/config.yaml', config.get_config_file()) # with env set - with mock.patch.dict(os.environ, {'INVENTREE_CONFIG_FILE': 'my_special_conf.yaml'}): + with self.in_env_context({'INVENTREE_CONFIG_FILE': 'my_special_conf.yaml'}): self.assertIn('InvenTree/InvenTree/my_special_conf.yaml', config.get_config_file()) def test_helpers_plugin_file(self): @@ -521,7 +525,7 @@ class TestSettings(TestCase): self.assertIn('InvenTree/InvenTree/plugins.txt', config.get_plugin_file()) # with env set - with mock.patch.dict(os.environ, {'INVENTREE_PLUGIN_FILE': 'my_special_plugins.txt'}): + with self.in_env_context({'INVENTREE_PLUGIN_FILE': 'my_special_plugins.txt'}): self.assertIn('my_special_plugins.txt', config.get_plugin_file()) def test_helpers_setting(self): @@ -530,7 +534,7 @@ class TestSettings(TestCase): self.assertEqual(config.get_setting(TEST_ENV_NAME, None, '123!'), '123!') # with env set - with mock.patch.dict(os.environ, {'TEST_ENV_NAME': '321'}): + with self.in_env_context({'TEST_ENV_NAME': '321'}): self.assertEqual(config.get_setting(TEST_ENV_NAME, None), '321') From 9b377608568df1fa9b81a47d33127c43d670f2cb Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 18 May 2022 02:06:00 +0200 Subject: [PATCH 13/42] fix assertations --- InvenTree/InvenTree/tests.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index 02c95192df..f56e9809d0 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -483,7 +483,7 @@ class TestSettings(TestCase): self.run_reload({ 'INVENTREE_ADMIN_USER': 'admin' }) - self.assertEqual(user_count(), 0) + self.assertEqual(user_count(), 1) # enough set self.run_reload({ @@ -491,7 +491,7 @@ class TestSettings(TestCase): 'INVENTREE_ADMIN_EMAIL': 'info@example.com', # set email 'INVENTREE_ADMIN_PASSWORD': 'password123' # set password }) - self.assertEqual(user_count(), 1) + self.assertEqual(user_count(), 2) # make sure to clean up settings.TESTING_ENV = False From 4ac7d9626c176493e5c8b4baec5a473609924e4d Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 18 May 2022 02:07:28 +0200 Subject: [PATCH 14/42] add missing test from merge back in --- InvenTree/InvenTree/tests.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index f56e9809d0..f053a7c27b 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -493,6 +493,17 @@ class TestSettings(TestCase): }) self.assertEqual(user_count(), 2) + # create user manually + self.user_mdl.objects.create_user('testuser', 'test@testing.com', 'password') + self.assertEqual(user_count(), 3) + # check it will not be created again + self.run_reload({ + 'INVENTREE_ADMIN_USER': 'testuser', + 'INVENTREE_ADMIN_EMAIL': 'test@testing.com', + 'INVENTREE_ADMIN_PASSWORD': 'password', + }) + self.assertEqual(user_count(), 3) + # make sure to clean up settings.TESTING_ENV = False From bdf28b72df19fa7b7fa1ddc94977b8438aaf12d7 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 18 May 2022 02:25:44 +0200 Subject: [PATCH 15/42] fix default --- InvenTree/InvenTree/tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index f053a7c27b..bad14f8e00 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -461,7 +461,7 @@ class TestSettings(TestCase): """Patch the env to include the given dict""" return mock.patch.dict(os.environ, envs) - def run_reload(self, envs): + def run_reload(self, envs={}): from plugin import registry with self.in_env_context(envs): From 9a0189b6bbfb2a8feda0191a05b0d4da77c71319 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 18 May 2022 02:30:07 +0200 Subject: [PATCH 16/42] fix env name --- InvenTree/InvenTree/tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index bad14f8e00..1573a4387a 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -545,7 +545,7 @@ class TestSettings(TestCase): self.assertEqual(config.get_setting(TEST_ENV_NAME, None, '123!'), '123!') # with env set - with self.in_env_context({'TEST_ENV_NAME': '321'}): + with self.in_env_context({TEST_ENV_NAME: '321'}): self.assertEqual(config.get_setting(TEST_ENV_NAME, None), '321') From c4208782c5ef8171ae102e4254e77f08df41a7cf Mon Sep 17 00:00:00 2001 From: Matthias Mair Date: Wed, 18 May 2022 02:31:04 +0200 Subject: [PATCH 17/42] Update docker_test.yaml --- .github/workflows/docker_test.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/docker_test.yaml b/.github/workflows/docker_test.yaml index 69fbb48fe7..9b38743ab7 100644 --- a/.github/workflows/docker_test.yaml +++ b/.github/workflows/docker_test.yaml @@ -13,6 +13,7 @@ on: branches: - 'master' - 'stable' + - 'webp-support' pull_request: branches-ignore: From 21750c92d399af82aa738d639ea7c48939a32dc7 Mon Sep 17 00:00:00 2001 From: Matthias Mair Date: Wed, 18 May 2022 02:33:09 +0200 Subject: [PATCH 18/42] remove branch from test --- .github/workflows/docker_test.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/docker_test.yaml b/.github/workflows/docker_test.yaml index 9b38743ab7..69fbb48fe7 100644 --- a/.github/workflows/docker_test.yaml +++ b/.github/workflows/docker_test.yaml @@ -13,7 +13,6 @@ on: branches: - 'master' - 'stable' - - 'webp-support' pull_request: branches-ignore: From 3b53260d751e0ca239e4daa02eefaea167c82c33 Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 18 May 2022 11:51:14 +1000 Subject: [PATCH 19/42] Allow some variation in unit test --- InvenTree/InvenTree/tests.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index 1573a4387a..9ddde26418 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -533,7 +533,13 @@ class TestSettings(TestCase): def test_helpers_plugin_file(self): # normal run - not configured - self.assertIn('InvenTree/InvenTree/plugins.txt', config.get_plugin_file()) + + valid = [ + 'inventree/plugins.txt', + 'inventree/dev/plugins.txt', + ] + + self.assertIn(config.get_plugin_file().lower(), valid) # with env set with self.in_env_context({'INVENTREE_PLUGIN_FILE': 'my_special_plugins.txt'}): From e57087de638ccf26851a229946c3727616c4a017 Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 18 May 2022 12:19:10 +1000 Subject: [PATCH 20/42] Fix unit test --- InvenTree/InvenTree/tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index 9ddde26418..f5f302b917 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -539,7 +539,7 @@ class TestSettings(TestCase): 'inventree/dev/plugins.txt', ] - self.assertIn(config.get_plugin_file().lower(), valid) + self.assertTrue(any([opt in config.get_plugin_file().lower() for opt in valid])) # with env set with self.in_env_context({'INVENTREE_PLUGIN_FILE': 'my_special_plugins.txt'}): From 0f1dd3fe65043a6715ea804a7115e6091ffca4b2 Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 18 May 2022 13:02:23 +1000 Subject: [PATCH 21/42] Same fix for config file test --- InvenTree/InvenTree/tests.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index f5f302b917..5bb6a4aae2 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -525,7 +525,13 @@ class TestSettings(TestCase): def test_helpers_cfg_file(self): # normal run - not configured - self.assertIn('InvenTree/InvenTree/config.yaml', config.get_config_file()) + + valid = [ + 'inventree/config.yaml', + 'inventree/dev/config.yaml', + ] + + self.assertTrue(any([opt in config.get_config_file().lower() for opt in valid])) # with env set with self.in_env_context({'INVENTREE_CONFIG_FILE': 'my_special_conf.yaml'}): From 810671f42383b9860e085bf2811426244d0f3214 Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 18 May 2022 13:40:57 +1000 Subject: [PATCH 22/42] Yet another fix --- InvenTree/InvenTree/tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index 5bb6a4aae2..501eed0834 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -535,7 +535,7 @@ class TestSettings(TestCase): # with env set with self.in_env_context({'INVENTREE_CONFIG_FILE': 'my_special_conf.yaml'}): - self.assertIn('InvenTree/InvenTree/my_special_conf.yaml', config.get_config_file()) + self.assertIn('inventree/inventree/my_special_conf.yaml', config.get_config_file().lower()) def test_helpers_plugin_file(self): # normal run - not configured From f53c8865ad742d7f2b64a2edc35632426c099cbb Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 18 May 2022 14:14:40 +1000 Subject: [PATCH 23/42] Only run docker build on push --- .github/workflows/docker_test.yaml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.github/workflows/docker_test.yaml b/.github/workflows/docker_test.yaml index 69fbb48fe7..b4bb37715d 100644 --- a/.github/workflows/docker_test.yaml +++ b/.github/workflows/docker_test.yaml @@ -14,10 +14,6 @@ on: - 'master' - 'stable' - pull_request: - branches-ignore: - - l10* - jobs: docker: From ea3133be1d4da530941231e8aed5ba25e5a69ccb Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 18 May 2022 15:11:37 +1000 Subject: [PATCH 24/42] Combine docker-build and docker-test CI steps - We are building anyway, may as well test --- .github/workflows/docker_latest.yaml | 14 ++++++++++ .github/workflows/docker_test.yaml | 38 ---------------------------- 2 files changed, 14 insertions(+), 38 deletions(-) delete mode 100644 .github/workflows/docker_test.yaml diff --git a/.github/workflows/docker_latest.yaml b/.github/workflows/docker_latest.yaml index 6b248fe0b9..9942407c07 100644 --- a/.github/workflows/docker_latest.yaml +++ b/.github/workflows/docker_latest.yaml @@ -18,6 +18,20 @@ jobs: - name: Check version number run: | python3 ci/check_version_number.py --dev + - name: Build Docker Image + run: | + cd docker + docker-compose build + docker-compose run inventree-dev-server invoke update + docker-compose up -d + - name: Wait for Server + run: | + cd docker + docker-compose run inventree-dev-server invoke wait + - name: Run unit tests + run: | + cd docker + docker-compose run inventree-dev-server invoke test - name: Set up QEMU uses: docker/setup-qemu-action@v1 - name: Set up Docker Buildx diff --git a/.github/workflows/docker_test.yaml b/.github/workflows/docker_test.yaml deleted file mode 100644 index b4bb37715d..0000000000 --- a/.github/workflows/docker_test.yaml +++ /dev/null @@ -1,38 +0,0 @@ -# Test that the InvenTree docker image compiles correctly - -# This CI action runs on pushes to either the master or stable branches - -# 1. Build the development docker image (as per the documentation) -# 2. Launch the development server, and update the installation -# 3. Run unit tests within the docker context - -name: Docker Test - -on: - push: - branches: - - 'master' - - 'stable' - -jobs: - - docker: - runs-on: ubuntu-latest - - steps: - - name: Checkout Code - uses: actions/checkout@v2 - - name: Build Docker Image - run: | - cd docker - docker-compose build - docker-compose run inventree-dev-server invoke update - docker-compose up -d - - name: Wait for Server - run: | - cd docker - docker-compose run inventree-dev-server invoke wait - - name: Run unit tests - run: | - cd docker - docker-compose run inventree-dev-server invoke test From 3e05c5fde1a2fe52e5aa82d64a7477c4d392d5f6 Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 18 May 2022 15:12:19 +1000 Subject: [PATCH 25/42] Bring docker containers down --- .github/workflows/docker_latest.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/docker_latest.yaml b/.github/workflows/docker_latest.yaml index 9942407c07..9324c39b54 100644 --- a/.github/workflows/docker_latest.yaml +++ b/.github/workflows/docker_latest.yaml @@ -32,6 +32,10 @@ jobs: run: | cd docker docker-compose run inventree-dev-server invoke test + - name: Down again + run: | + cd docker + docker-compose down - name: Set up QEMU uses: docker/setup-qemu-action@v1 - name: Set up Docker Buildx From 2fde482eab5463803773625e1c06ed1034f6cc16 Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 18 May 2022 15:39:58 +1000 Subject: [PATCH 26/42] Simplify steps --- .github/workflows/docker_latest.yaml | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/.github/workflows/docker_latest.yaml b/.github/workflows/docker_latest.yaml index 9324c39b54..74b5eb966c 100644 --- a/.github/workflows/docker_latest.yaml +++ b/.github/workflows/docker_latest.yaml @@ -23,18 +23,12 @@ jobs: cd docker docker-compose build docker-compose run inventree-dev-server invoke update - docker-compose up -d - - name: Wait for Server - run: | - cd docker - docker-compose run inventree-dev-server invoke wait - name: Run unit tests run: | cd docker + docker-compose up -d + docker-compose run inventree-dev-server invoke wait docker-compose run inventree-dev-server invoke test - - name: Down again - run: | - cd docker docker-compose down - name: Set up QEMU uses: docker/setup-qemu-action@v1 From 6147afe35ff6c700e787e1032114db819cc2fb11 Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 18 May 2022 16:54:57 +1000 Subject: [PATCH 27/42] Catch errors when rendering custom plugin panels --- InvenTree/plugin/views.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/InvenTree/plugin/views.py b/InvenTree/plugin/views.py index ee855094cc..d03c28bf90 100644 --- a/InvenTree/plugin/views.py +++ b/InvenTree/plugin/views.py @@ -1,5 +1,10 @@ +import sys +import traceback from django.conf import settings +from django.views.debug import ExceptionReporter + +from error_report.models import Error from plugin.registry import registry @@ -21,7 +26,21 @@ class InvenTreePluginViewMixin: panels = [] for plug in registry.with_mixin('panel'): - panels += plug.render_panels(self, self.request, ctx) + + try: + panels += plug.render_panels(self, self.request, ctx) + except Exception as exc: + # Prevent any plugin error from crashing the page render + kind, info, data = sys.exc_info() + + # Log the error to the database + Error.objects.create( + kind=kind.__name__, + info=info, + data='\n'.join(traceback.format_exception(kind, info, data)), + path=self.request.path, + html=ExceptionReporter(self.request, kind, info, data).get_traceback_html(), + ) return panels From 4ceb35a43f063ccccfe30ef4b1a934a32f7e9c49 Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 18 May 2022 17:00:20 +1000 Subject: [PATCH 28/42] Fix PEP issue --- InvenTree/plugin/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/InvenTree/plugin/views.py b/InvenTree/plugin/views.py index d03c28bf90..8d28872695 100644 --- a/InvenTree/plugin/views.py +++ b/InvenTree/plugin/views.py @@ -29,7 +29,7 @@ class InvenTreePluginViewMixin: try: panels += plug.render_panels(self, self.request, ctx) - except Exception as exc: + except Exception: # Prevent any plugin error from crashing the page render kind, info, data = sys.exc_info() From 67c675d1a6d66f6f908d3b19cd26f6ef10d38f7e Mon Sep 17 00:00:00 2001 From: Jakob Haufe Date: Wed, 18 May 2022 13:24:50 +0200 Subject: [PATCH 29/42] Add ManufacturerPartAttachment class --- InvenTree/company/models.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/InvenTree/company/models.py b/InvenTree/company/models.py index 40afa6db6d..e33580abff 100644 --- a/InvenTree/company/models.py +++ b/InvenTree/company/models.py @@ -22,6 +22,7 @@ from stdimage.models import StdImageField from InvenTree.helpers import getMediaUrl, getBlankImage, getBlankThumbnail from InvenTree.fields import InvenTreeURLField +from InvenTree.models import InvenTreeAttachment from InvenTree.status_codes import PurchaseOrderStatus import InvenTree.validators @@ -380,6 +381,22 @@ class ManufacturerPart(models.Model): return s +class ManufacturerPartAttachment(InvenTreeAttachment): + """ + Model for storing file attachments against a ManufacturerPart object + """ + + @staticmethod + def get_api_url(): + return reverse('api-manufacturer-part-attachment-list') + + def getSubdir(self): + return os.path.join("manufacturer_part_files", str(self.manufacturer_part.id)) + + manufacturer_part = models.ForeignKey(ManufacturerPart, on_delete=models.CASCADE, + verbose_name=_('Manufacturer Part'), related_name='attachments') + + class ManufacturerPartParameter(models.Model): """ A ManufacturerPartParameter represents a key:value parameter for a MnaufacturerPart. From c608778a1b2469f15a9d5183e73fdc549435e66b Mon Sep 17 00:00:00 2001 From: Jakob Haufe Date: Sun, 1 May 2022 13:01:45 +0000 Subject: [PATCH 30/42] Add migration --- .../0043_manufacturerpartattachment.py | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 InvenTree/company/migrations/0043_manufacturerpartattachment.py diff --git a/InvenTree/company/migrations/0043_manufacturerpartattachment.py b/InvenTree/company/migrations/0043_manufacturerpartattachment.py new file mode 100644 index 0000000000..fe526992b0 --- /dev/null +++ b/InvenTree/company/migrations/0043_manufacturerpartattachment.py @@ -0,0 +1,33 @@ +# Generated by Django 3.2.13 on 2022-05-01 12:57 + +import InvenTree.fields +import InvenTree.models +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('company', '0042_supplierpricebreak_updated'), + ] + + operations = [ + migrations.CreateModel( + name='ManufacturerPartAttachment', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('attachment', models.FileField(blank=True, help_text='Select file to attach', null=True, upload_to=InvenTree.models.rename_attachment, verbose_name='Attachment')), + ('link', InvenTree.fields.InvenTreeURLField(blank=True, help_text='Link to external URL', null=True, verbose_name='Link')), + ('comment', models.CharField(blank=True, help_text='File comment', max_length=100, verbose_name='Comment')), + ('upload_date', models.DateField(auto_now_add=True, null=True, verbose_name='upload date')), + ('manufacturer_part', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='attachments', to='company.manufacturerpart', verbose_name='Manufacturer Part')), + ('user', models.ForeignKey(blank=True, help_text='User', null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL, verbose_name='User')), + ], + options={ + 'abstract': False, + }, + ), + ] From a373e669cd417c4c2e17746f1fec9597402c2c52 Mon Sep 17 00:00:00 2001 From: Jakob Haufe Date: Sun, 1 May 2022 13:14:50 +0000 Subject: [PATCH 31/42] Add permission --- InvenTree/users/models.py | 1 + 1 file changed, 1 insertion(+) diff --git a/InvenTree/users/models.py b/InvenTree/users/models.py index 7ed689f4a9..3c33614b97 100644 --- a/InvenTree/users/models.py +++ b/InvenTree/users/models.py @@ -101,6 +101,7 @@ class RuleSet(models.Model): 'company_supplierpart', 'company_manufacturerpart', 'company_manufacturerpartparameter', + 'company_manufacturerpartattachment', 'label_partlabel', ], 'stock_location': [ From 3ee32374b48b1ebef25f185d74d557a4239efe5a Mon Sep 17 00:00:00 2001 From: Jakob Haufe Date: Sun, 1 May 2022 13:15:37 +0000 Subject: [PATCH 32/42] Add serializer --- InvenTree/company/serializers.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/InvenTree/company/serializers.py b/InvenTree/company/serializers.py index 236dcc15db..54eeb2c191 100644 --- a/InvenTree/company/serializers.py +++ b/InvenTree/company/serializers.py @@ -8,6 +8,7 @@ from rest_framework import serializers from sql_util.utils import SubqueryCount +from InvenTree.serializers import InvenTreeAttachmentSerializer from InvenTree.serializers import InvenTreeDecimalField from InvenTree.serializers import InvenTreeImageSerializerField from InvenTree.serializers import InvenTreeModelSerializer @@ -16,7 +17,7 @@ from InvenTree.serializers import InvenTreeMoneySerializer from part.serializers import PartBriefSerializer from .models import Company -from .models import ManufacturerPart, ManufacturerPartParameter +from .models import ManufacturerPart, ManufacturerPartAttachment, ManufacturerPartParameter from .models import SupplierPart, SupplierPriceBreak from common.settings import currency_code_default, currency_code_mappings @@ -142,6 +143,29 @@ class ManufacturerPartSerializer(InvenTreeModelSerializer): ] +class ManufacturerPartAttachmentSerializer(InvenTreeAttachmentSerializer): + """ + Serializer for the ManufacturerPartAttachment class + """ + + class Meta: + model = ManufacturerPartAttachment + + fields = [ + 'pk', + 'manufacturer_part', + 'attachment', + 'filename', + 'link', + 'comment', + 'upload_date', + ] + + read_only_fields = [ + 'upload_date', + ] + + class ManufacturerPartParameterSerializer(InvenTreeModelSerializer): """ Serializer for the ManufacturerPartParameter model From 69ba271bf7ae4b78d01e663adaf495808cc14174 Mon Sep 17 00:00:00 2001 From: Jakob Haufe Date: Sun, 1 May 2022 13:51:09 +0000 Subject: [PATCH 33/42] Add API endpoints --- InvenTree/company/api.py | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/InvenTree/company/api.py b/InvenTree/company/api.py index 146a45f648..c171bf7448 100644 --- a/InvenTree/company/api.py +++ b/InvenTree/company/api.py @@ -12,13 +12,14 @@ from django.urls import include, re_path from django.db.models import Q from InvenTree.helpers import str2bool +from InvenTree.api import AttachmentMixin from .models import Company -from .models import ManufacturerPart, ManufacturerPartParameter +from .models import ManufacturerPart, ManufacturerPartAttachment, ManufacturerPartParameter from .models import SupplierPart, SupplierPriceBreak from .serializers import CompanySerializer -from .serializers import ManufacturerPartSerializer, ManufacturerPartParameterSerializer +from .serializers import ManufacturerPartSerializer, ManufacturerPartAttachmentSerializer, ManufacturerPartParameterSerializer from .serializers import SupplierPartSerializer, SupplierPriceBreakSerializer @@ -160,6 +161,32 @@ class ManufacturerPartDetail(generics.RetrieveUpdateDestroyAPIView): serializer_class = ManufacturerPartSerializer +class ManufacturerPartAttachmentList(AttachmentMixin, generics.ListCreateAPIView): + """ + API endpoint for listing (and creating) a ManufacturerPartAttachment (file upload). + """ + + queryset = ManufacturerPartAttachment.objects.all() + serializer_class = ManufacturerPartAttachmentSerializer + + filter_backends = [ + DjangoFilterBackend, + ] + + filter_fields = [ + 'manufacturer_part', + ] + + +class ManufacturerPartAttachmentDetail(AttachmentMixin, generics.RetrieveUpdateDestroyAPIView): + """ + Detail endpooint for ManufacturerPartAttachment model + """ + + queryset = ManufacturerPartAttachment.objects.all() + serializer_class = ManufacturerPartAttachmentSerializer + + class ManufacturerPartParameterList(generics.ListCreateAPIView): """ API endpoint for list view of ManufacturerPartParamater model. From 09a76277888aef563b52a2d26a6c25617515f527 Mon Sep 17 00:00:00 2001 From: Jakob Haufe Date: Sun, 1 May 2022 13:57:02 +0000 Subject: [PATCH 34/42] Add API URLs --- InvenTree/company/api.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/InvenTree/company/api.py b/InvenTree/company/api.py index c171bf7448..22c5c4b207 100644 --- a/InvenTree/company/api.py +++ b/InvenTree/company/api.py @@ -414,6 +414,12 @@ class SupplierPriceBreakDetail(generics.RetrieveUpdateDestroyAPIView): manufacturer_part_api_urls = [ + # Base URL for ManufacturerPartAttachment API endpoints + re_path(r'^attachment/', include([ + re_path(r'^(?P\d+)/', ManufacturerPartAttachmentDetail.as_view(), name='api-manufacturer-part-attachment-detail'), + re_path(r'^$', ManufacturerPartAttachmentList.as_view(), name='api-manufacturer-part-attachment-list'), + ])), + re_path(r'^parameter/', include([ re_path(r'^(?P\d+)/', ManufacturerPartParameterDetail.as_view(), name='api-manufacturer-part-parameter-detail'), From fc3e61df24290530b6a1cee67a1148d3573c883d Mon Sep 17 00:00:00 2001 From: Jakob Haufe Date: Sun, 1 May 2022 14:03:19 +0000 Subject: [PATCH 35/42] Add sidebar item --- .../company/templates/company/manufacturer_part_sidebar.html | 2 ++ 1 file changed, 2 insertions(+) diff --git a/InvenTree/company/templates/company/manufacturer_part_sidebar.html b/InvenTree/company/templates/company/manufacturer_part_sidebar.html index bd613f76aa..04f3a39a5b 100644 --- a/InvenTree/company/templates/company/manufacturer_part_sidebar.html +++ b/InvenTree/company/templates/company/manufacturer_part_sidebar.html @@ -4,5 +4,7 @@ {% trans "Parameters" as text %} {% include "sidebar_item.html" with label='parameters' text=text icon="fa-th-list" %} +{% trans "Attachments" as text %} +{% include "sidebar_item.html" with label='attachments' text=text icon="fa-paperclip" %} {% trans "Supplier Parts" as text %} {% include "sidebar_item.html" with label='supplier-parts' text=text icon="fa-building" %} \ No newline at end of file From c6d3cd9bae5070f289eff971376094b89fb0cde4 Mon Sep 17 00:00:00 2001 From: Jakob Haufe Date: Sun, 1 May 2022 14:12:00 +0000 Subject: [PATCH 36/42] Add content panel --- .../templates/company/manufacturer_part.html | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/InvenTree/company/templates/company/manufacturer_part.html b/InvenTree/company/templates/company/manufacturer_part.html index 5a0e741c1a..ae4690f1c6 100644 --- a/InvenTree/company/templates/company/manufacturer_part.html +++ b/InvenTree/company/templates/company/manufacturer_part.html @@ -144,6 +144,21 @@ src="{% static 'img/blank_image.png' %}" +
+
+
+

{% trans "Attachments" %}

+ {% include "spacer.html" %} +
+ {% include "attachment_button.html" %} +
+
+
+
+ {% include "attachment_table.html" %} +
+
+
From 72f330ab7553ea43769312ad7bf7a52e95a34b13 Mon Sep 17 00:00:00 2001 From: Jakob Haufe Date: Sun, 1 May 2022 14:33:44 +0000 Subject: [PATCH 37/42] Add JS --- .../templates/company/manufacturer_part.html | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/InvenTree/company/templates/company/manufacturer_part.html b/InvenTree/company/templates/company/manufacturer_part.html index ae4690f1c6..a51ea45099 100644 --- a/InvenTree/company/templates/company/manufacturer_part.html +++ b/InvenTree/company/templates/company/manufacturer_part.html @@ -193,6 +193,34 @@ src="{% static 'img/blank_image.png' %}" {% block js_ready %} {{ block.super }} +onPanelLoad("attachments", function() { + loadAttachmentTable('{% url "api-manufacturer-part-attachment-list" %}', { + filters: { + manufacturer_part: {{ part.pk }}, + }, + fields: { + manufacturer_part: { + value: {{ part.pk }}, + hidden: true + } + } + }); + + enableDragAndDrop( + '#attachment-dropzone', + '{% url "api-manufacturer-part-attachment-list" %}', + { + data: { + manufacturer_part: {{ part.id }}, + }, + label: 'attachment', + success: function(data, status, xhr) { + reloadAttachmentTable(); + } + } + ); +}); + function reloadParameters() { $("#parameter-table").bootstrapTable("refresh"); } From ed1cc1209e8c47f3dadb31d5381babf6e3117eab Mon Sep 17 00:00:00 2001 From: Jakob Haufe Date: Sun, 1 May 2022 17:48:55 +0000 Subject: [PATCH 38/42] Add admin class --- InvenTree/company/admin.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/InvenTree/company/admin.py b/InvenTree/company/admin.py index cc672f9ee5..ce5f5945b6 100644 --- a/InvenTree/company/admin.py +++ b/InvenTree/company/admin.py @@ -8,7 +8,7 @@ import import_export.widgets as widgets from .models import Company from .models import SupplierPart from .models import SupplierPriceBreak -from .models import ManufacturerPart, ManufacturerPartParameter +from .models import ManufacturerPart, ManufacturerPartAttachment, ManufacturerPartParameter from part.models import Part @@ -109,6 +109,16 @@ class ManufacturerPartAdmin(ImportExportModelAdmin): autocomplete_fields = ('part', 'manufacturer',) +class ManufacturerPartAttachmentAdmin(ImportExportModelAdmin): + """ + Admin class for ManufacturerPartAttachment model + """ + + list_display = ('manufacturer_part', 'attachment', 'comment') + + autocomplete_fields = ('manufacturer_part',) + + class ManufacturerPartParameterResource(ModelResource): """ Class for managing ManufacturerPartParameter data import/export @@ -175,4 +185,5 @@ admin.site.register(SupplierPart, SupplierPartAdmin) admin.site.register(SupplierPriceBreak, SupplierPriceBreakAdmin) admin.site.register(ManufacturerPart, ManufacturerPartAdmin) +admin.site.register(ManufacturerPartAttachment, ManufacturerPartAttachmentAdmin) admin.site.register(ManufacturerPartParameter, ManufacturerPartParameterAdmin) From 3f67682d53e4f0235acc1054aa67c1b9ca78dd56 Mon Sep 17 00:00:00 2001 From: Jakob Haufe Date: Wed, 18 May 2022 13:22:57 +0200 Subject: [PATCH 39/42] Increment API version --- InvenTree/InvenTree/api_version.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/api_version.py b/InvenTree/InvenTree/api_version.py index d2eab15468..e44aedf10b 100644 --- a/InvenTree/InvenTree/api_version.py +++ b/InvenTree/InvenTree/api_version.py @@ -4,11 +4,14 @@ InvenTree API version information # InvenTree API version -INVENTREE_API_VERSION = 49 +INVENTREE_API_VERSION = 50 """ Increment this API version number whenever there is a significant change to the API that any clients need to know about +v50 -> 2022-05-18 : https://github.com/inventree/InvenTree/pull/2912 + - Implement Attachments for manufacturer parts + v49 -> 2022-05-09 : https://github.com/inventree/InvenTree/pull/2957 - Allows filtering of plugin list by 'active' status - Allows filtering of plugin list by 'mixin' support From 0e0ba66b9a2c87a85c889872c1ddda860840fbdd Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 18 May 2022 21:40:53 +1000 Subject: [PATCH 40/42] Fix broken calls to offload_task --- InvenTree/plugin/base/integration/mixins.py | 3 ++- InvenTree/plugin/base/locate/api.py | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/InvenTree/plugin/base/integration/mixins.py b/InvenTree/plugin/base/integration/mixins.py index 86e3092e4f..64de5df22b 100644 --- a/InvenTree/plugin/base/integration/mixins.py +++ b/InvenTree/plugin/base/integration/mixins.py @@ -13,6 +13,7 @@ import InvenTree.helpers from plugin.helpers import MixinImplementationError, MixinNotImplementedError, render_template from plugin.models import PluginConfig, PluginSetting +from plugin.registry import registry from plugin.urls import PLUGIN_BASE @@ -204,7 +205,7 @@ class ScheduleMixin: Schedule.objects.create( name=task_name, - func='plugin.registry.call_function', + func=registry.call_plugin_function, args=f"'{slug}', '{func_name}'", schedule_type=task['schedule'], minutes=task.get('minutes', None), diff --git a/InvenTree/plugin/base/locate/api.py b/InvenTree/plugin/base/locate/api.py index a6776f2d40..f617ba3577 100644 --- a/InvenTree/plugin/base/locate/api.py +++ b/InvenTree/plugin/base/locate/api.py @@ -7,7 +7,7 @@ from rest_framework.views import APIView from InvenTree.tasks import offload_task -from plugin import registry +from plugin.registry import registry from stock.models import StockItem, StockLocation @@ -53,7 +53,7 @@ class LocatePluginView(APIView): try: StockItem.objects.get(pk=item_pk) - offload_task(registry.call_function, plugin, 'locate_stock_item', item_pk) + offload_task(registry.call_plugin_function, plugin, 'locate_stock_item', item_pk) data['item'] = item_pk @@ -66,7 +66,7 @@ class LocatePluginView(APIView): try: StockLocation.objects.get(pk=location_pk) - offload_task(registry.call_function, plugin, 'locate_stock_location', location_pk) + offload_task(registry.call_plugin_function, plugin, 'locate_stock_location', location_pk) data['location'] = location_pk From dd476ce796103f2a8fe84a0be240604249a060a8 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 18 May 2022 22:20:29 +1000 Subject: [PATCH 41/42] Add unit tests for the 'locate' plugin - Test various failure modes - Some of the failure modes didn't fail - this is also a failure - Fixing API code accordingly --- InvenTree/plugin/base/locate/api.py | 14 ++-- InvenTree/plugin/base/locate/test_locate.py | 89 +++++++++++++++++++++ 2 files changed, 95 insertions(+), 8 deletions(-) create mode 100644 InvenTree/plugin/base/locate/test_locate.py diff --git a/InvenTree/plugin/base/locate/api.py b/InvenTree/plugin/base/locate/api.py index f617ba3577..a7effe91b3 100644 --- a/InvenTree/plugin/base/locate/api.py +++ b/InvenTree/plugin/base/locate/api.py @@ -40,9 +40,6 @@ class LocatePluginView(APIView): # StockLocation to identify location_pk = request.data.get('location', None) - if not item_pk and not location_pk: - raise ParseError("Must supply either 'item' or 'location' parameter") - data = { "success": "Identification plugin activated", "plugin": plugin, @@ -59,8 +56,8 @@ class LocatePluginView(APIView): return Response(data) - except StockItem.DoesNotExist: - raise NotFound("StockItem matching PK '{item}' not found") + except (ValueError, StockItem.DoesNotExist): + raise NotFound(f"StockItem matching PK '{item_pk}' not found") elif location_pk: try: @@ -72,8 +69,9 @@ class LocatePluginView(APIView): return Response(data) - except StockLocation.DoesNotExist: - raise NotFound("StockLocation matching PK {'location'} not found") + except (ValueError, StockLocation.DoesNotExist): + raise NotFound(f"StockLocation matching PK '{location_pk}' not found") else: - raise NotFound() + raise ParseError("Must supply either 'item' or 'location' parameter") + diff --git a/InvenTree/plugin/base/locate/test_locate.py b/InvenTree/plugin/base/locate/test_locate.py new file mode 100644 index 0000000000..26fafcca49 --- /dev/null +++ b/InvenTree/plugin/base/locate/test_locate.py @@ -0,0 +1,89 @@ +""" +Unit tests for the 'locate' plugin mixin class +""" + +from django.urls import reverse + +from InvenTree.api_tester import InvenTreeAPITestCase + +from plugin.registry import registry + + +class LocatePluginTests(InvenTreeAPITestCase): + + fixtures = [ + 'category', + 'part', + 'location', + 'stock', + ] + + def test_installed(self): + """Test that a locate plugin is actually installed""" + + plugins = registry.with_mixin('locate') + + self.assertTrue(len(plugins) > 0) + + self.assertTrue('samplelocate' in [p.slug for p in plugins]) + + def test_locate_fail(self): + """Test various API failure modes""" + + url = reverse('api-locate-plugin') + + # Post without a plugin + response = self.post( + url, + {}, + expected_code=400 + ) + + self.assertIn("'plugin' field must be supplied", str(response.data)) + + # Post with a plugin that does not exist, or is invalid + for slug in ['xyz', 'event', 'plugin']: + response = self.post( + url, + { + 'plugin': slug, + }, + expected_code=400, + ) + + self.assertIn(f"Plugin '{slug}' is not installed, or does not support the location mixin", str(response.data)) + + # Post with a valid plugin, but no other data + response = self.post( + url, + { + 'plugin': 'samplelocate', + }, + expected_code=400 + ) + + self.assertIn("Must supply either 'item' or 'location' parameter", str(response.data)) + + # Post with valid plugin, invalid item or location + for pk in ['qq', 99999, -42]: + response = self.post( + url, + { + 'plugin': 'samplelocate', + 'item': pk, + }, + expected_code=404 + ) + + self.assertIn(f"StockItem matching PK '{pk}' not found", str(response.data)) + + response = self.post( + url, + { + 'plugin': 'samplelocate', + 'location': pk, + }, + expected_code=404, + ) + + self.assertIn(f"StockLocation matching PK '{pk}' not found", str(response.data)) \ No newline at end of file From c6590066b865416e5761718e2a61dca06ad44e81 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 18 May 2022 22:46:15 +1000 Subject: [PATCH 42/42] Add tests for successful location - Sample plugin now updates metadata tag --- InvenTree/plugin/base/locate/api.py | 1 - InvenTree/plugin/base/locate/test_locate.py | 63 ++++++++++++++++++- .../plugin/samples/locate/locate_sample.py | 24 ++++++- 3 files changed, 83 insertions(+), 5 deletions(-) diff --git a/InvenTree/plugin/base/locate/api.py b/InvenTree/plugin/base/locate/api.py index a7effe91b3..3004abb262 100644 --- a/InvenTree/plugin/base/locate/api.py +++ b/InvenTree/plugin/base/locate/api.py @@ -74,4 +74,3 @@ class LocatePluginView(APIView): else: raise ParseError("Must supply either 'item' or 'location' parameter") - diff --git a/InvenTree/plugin/base/locate/test_locate.py b/InvenTree/plugin/base/locate/test_locate.py index 26fafcca49..e145c2360b 100644 --- a/InvenTree/plugin/base/locate/test_locate.py +++ b/InvenTree/plugin/base/locate/test_locate.py @@ -7,6 +7,7 @@ from django.urls import reverse from InvenTree.api_tester import InvenTreeAPITestCase from plugin.registry import registry +from stock.models import StockItem, StockLocation class LocatePluginTests(InvenTreeAPITestCase): @@ -29,7 +30,7 @@ class LocatePluginTests(InvenTreeAPITestCase): def test_locate_fail(self): """Test various API failure modes""" - + url = reverse('api-locate-plugin') # Post without a plugin @@ -86,4 +87,62 @@ class LocatePluginTests(InvenTreeAPITestCase): expected_code=404, ) - self.assertIn(f"StockLocation matching PK '{pk}' not found", str(response.data)) \ No newline at end of file + self.assertIn(f"StockLocation matching PK '{pk}' not found", str(response.data)) + + def test_locate_item(self): + """ + Test that the plugin correctly 'locates' a StockItem + + As the background worker is not running during unit testing, + the sample 'locate' function will be called 'inline' + """ + + url = reverse('api-locate-plugin') + + item = StockItem.objects.get(pk=1) + + # The sample plugin will set the 'located' metadata tag + item.set_metadata('located', False) + + response = self.post( + url, + { + 'plugin': 'samplelocate', + 'item': 1, + }, + expected_code=200 + ) + + self.assertEqual(response.data['item'], 1) + + item.refresh_from_db() + + # Item metadata should have been altered! + self.assertTrue(item.metadata['located']) + + def test_locate_location(self): + """ + Test that the plugin correctly 'locates' a StockLocation + """ + + url = reverse('api-locate-plugin') + + for location in StockLocation.objects.all(): + + location.set_metadata('located', False) + + response = self.post( + url, + { + 'plugin': 'samplelocate', + 'location': location.pk, + }, + expected_code=200 + ) + + self.assertEqual(response.data['location'], location.pk) + + location.refresh_from_db() + + # Item metadata should have been altered! + self.assertTrue(location.metadata['located']) diff --git a/InvenTree/plugin/samples/locate/locate_sample.py b/InvenTree/plugin/samples/locate/locate_sample.py index 458b84cfa5..32a2dd713c 100644 --- a/InvenTree/plugin/samples/locate/locate_sample.py +++ b/InvenTree/plugin/samples/locate/locate_sample.py @@ -23,7 +23,23 @@ class SampleLocatePlugin(LocateMixin, InvenTreePlugin): SLUG = "samplelocate" TITLE = "Sample plugin for locating items" - VERSION = "0.1" + VERSION = "0.2" + + def locate_stock_item(self, item_pk): + + from stock.models import StockItem + + logger.info(f"SampleLocatePlugin attempting to locate item ID {item_pk}") + + try: + item = StockItem.objects.get(pk=item_pk) + logger.info(f"StockItem {item_pk} located!") + + # Tag metadata + item.set_metadata('located', True) + + except (ValueError, StockItem.DoesNotExist): + logger.error(f"StockItem ID {item_pk} does not exist!") def locate_stock_location(self, location_pk): @@ -34,5 +50,9 @@ class SampleLocatePlugin(LocateMixin, InvenTreePlugin): try: location = StockLocation.objects.get(pk=location_pk) logger.info(f"Location exists at '{location.pathstring}'") - except StockLocation.DoesNotExist: + + # Tag metadata + location.set_metadata('located', True) + + except (ValueError, StockLocation.DoesNotExist): logger.error(f"Location ID {location_pk} does not exist!")