From c6366a93259c12153ce740a2c5c999d72bd6d396 Mon Sep 17 00:00:00 2001 From: wolflu05 <76838159+wolflu05@users.noreply.github.com> Date: Mon, 15 Jul 2024 18:00:56 +0200 Subject: [PATCH 1/5] fix part attachment --- inventree_bulk_plugin/bulkcreate_objects.py | 40 +++++++++++++++++-- .../integration/test_bulkcreate_objects.py | 12 ++++-- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/inventree_bulk_plugin/bulkcreate_objects.py b/inventree_bulk_plugin/bulkcreate_objects.py index 1e21f80..b24339e 100644 --- a/inventree_bulk_plugin/bulkcreate_objects.py +++ b/inventree_bulk_plugin/bulkcreate_objects.py @@ -16,7 +16,7 @@ from djmoney.contrib.exchange.models import Rate from stock.models import StockLocation -from part.models import PartCategory, Part, PartParameter, PartParameterTemplate, PartCategoryParameterTemplate, PartAttachment, PartRelated +from part.models import PartCategory, Part, PartParameter, PartParameterTemplate, PartCategoryParameterTemplate, PartRelated from company.models import Company, ManufacturerPart, SupplierPart from stock.models import StockItem from common.models import InvenTreeSetting @@ -503,12 +503,11 @@ def create_object(self, data: ParseChildReturnElement, *, parent: Optional[Part] # create attachments for attachment in attachments: - PartAttachment.objects.create( + self.add_attachment( part=part, link=attachment.get("link", None), comment=attachment["comment"], attachment=self.attachments.get(attachment.get("file_url", None), None), - user=self.request.user, ) # create manufacturer part @@ -625,6 +624,41 @@ def get_stock_status_options(self): def get_currency_default(self): return InvenTreeSetting.get_setting('INVENTREE_DEFAULT_CURRENCY', 'USD') + + def add_attachment(self, part: Part, *, comment: str, link: str, attachment: File): + """Add an attachment to a part. + + Note: This supports the 'legacy' and 'modern' attachment system. + Ref: https://github.com/inventree/InvenTree/pull/7420 + """ + + # first try the modern attachment system + try: + from common.models import Attachment + + Attachment.objects.create( + model_type="part", + model_id=part.pk, + comment=comment, + link=link, + attachment=attachment, + upload_user=self.request.user, + ) + return + except ImportError: + pass + except Exception as e: + raise e + + # fallback to the legacy attachment system + from part.models import PartAttachment + + PartAttachment.objects.create( + part=part, + comment=comment, + attachment=attachment, + user=self.request.user, + ) bulkcreate_objects: dict[str, type[BulkCreateObject]] = { diff --git a/inventree_bulk_plugin/tests/integration/test_bulkcreate_objects.py b/inventree_bulk_plugin/tests/integration/test_bulkcreate_objects.py index 4367dd1..72f7428 100644 --- a/inventree_bulk_plugin/tests/integration/test_bulkcreate_objects.py +++ b/inventree_bulk_plugin/tests/integration/test_bulkcreate_objects.py @@ -8,12 +8,18 @@ from mptt.models import MPTTModel from company.models import Company, ManufacturerPart, SupplierPart -from part.models import Part, PartCategory, PartParameterTemplate, PartParameter, PartAttachment, PartRelated +from part.models import Part, PartCategory, PartParameterTemplate, PartParameter, PartRelated from stock.models import StockLocation, StockItem from common.models import InvenTreeSetting from ...bulkcreate_objects import get_model, get_model_instance, cast_model, cast_select, FieldDefinition, BulkCreateObject, StockLocationBulkCreateObject, PartCategoryBulkCreateObject, PartBulkCreateObject +# import modern Attachment model, if it exists otherwise fallback to the legacy attachment system +try: + from common.models import Attachment +except ImportError: + from InvenTree.models import PartAttachment as Attachment + # custom request factory, used to patch query_params which were not defined by default class CustomRequestFactory(RequestFactory): @@ -399,7 +405,7 @@ def extra_model_tests(self, obj): )) issues.extend(self.model_test( - PartAttachment, + Attachment, obj.fields["attachments"].items_type.fields, f"{obj.template_type}.attachments.[x]", ignore_fields=["file_url", "file_name", "file_headers"], @@ -475,7 +481,7 @@ def test_create_objects(self): (ManufacturerPart, 1), (SupplierPart, 1), (StockItem, 1), - (PartAttachment, 3) + (Attachment, 3) ] for model, count in expected_objs: From f236795f851830ef29581b0bbcaf20a1a1863718 Mon Sep 17 00:00:00 2001 From: wolflu05 <76838159+wolflu05@users.noreply.github.com> Date: Mon, 15 Jul 2024 18:03:09 +0200 Subject: [PATCH 2/5] fix: style --- .../tests/integration/test_bulkcreate_objects.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inventree_bulk_plugin/tests/integration/test_bulkcreate_objects.py b/inventree_bulk_plugin/tests/integration/test_bulkcreate_objects.py index 72f7428..a61f27d 100644 --- a/inventree_bulk_plugin/tests/integration/test_bulkcreate_objects.py +++ b/inventree_bulk_plugin/tests/integration/test_bulkcreate_objects.py @@ -14,7 +14,7 @@ from ...bulkcreate_objects import get_model, get_model_instance, cast_model, cast_select, FieldDefinition, BulkCreateObject, StockLocationBulkCreateObject, PartCategoryBulkCreateObject, PartBulkCreateObject -# import modern Attachment model, if it exists otherwise fallback to the legacy attachment system +# import modern Attachment model, if it exists otherwise fallback to the legacy attachment system try: from common.models import Attachment except ImportError: From 43f70d1712289a1280c265adb77dd5861138f42a Mon Sep 17 00:00:00 2001 From: wolflu05 <76838159+wolflu05@users.noreply.github.com> Date: Mon, 15 Jul 2024 18:15:27 +0200 Subject: [PATCH 3/5] fix: tests --- .../tests/integration/test_bulkcreate_objects.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inventree_bulk_plugin/tests/integration/test_bulkcreate_objects.py b/inventree_bulk_plugin/tests/integration/test_bulkcreate_objects.py index a61f27d..c63d287 100644 --- a/inventree_bulk_plugin/tests/integration/test_bulkcreate_objects.py +++ b/inventree_bulk_plugin/tests/integration/test_bulkcreate_objects.py @@ -18,7 +18,7 @@ try: from common.models import Attachment except ImportError: - from InvenTree.models import PartAttachment as Attachment + from part.models import PartAttachment as Attachment # custom request factory, used to patch query_params which were not defined by default @@ -408,7 +408,7 @@ def extra_model_tests(self, obj): Attachment, obj.fields["attachments"].items_type.fields, f"{obj.template_type}.attachments.[x]", - ignore_fields=["file_url", "file_name", "file_headers"], + ignore_fields=["file_url", "file_name", "file_headers", "model_type", "model_id"], ignore_model_required_fields=["part"], )) From c8ee8197fb0287ef761ec67079f6eaba4b3ddad5 Mon Sep 17 00:00:00 2001 From: wolflu05 <76838159+wolflu05@users.noreply.github.com> Date: Mon, 15 Jul 2024 18:25:23 +0200 Subject: [PATCH 4/5] fix: tests --- inventree_bulk_plugin/bulkcreate_objects.py | 1 + .../tests/integration/test_bulkcreate_objects.py | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/inventree_bulk_plugin/bulkcreate_objects.py b/inventree_bulk_plugin/bulkcreate_objects.py index b24339e..c5a1fcd 100644 --- a/inventree_bulk_plugin/bulkcreate_objects.py +++ b/inventree_bulk_plugin/bulkcreate_objects.py @@ -655,6 +655,7 @@ def add_attachment(self, part: Part, *, comment: str, link: str, attachment: Fil PartAttachment.objects.create( part=part, + link=link, comment=comment, attachment=attachment, user=self.request.user, diff --git a/inventree_bulk_plugin/tests/integration/test_bulkcreate_objects.py b/inventree_bulk_plugin/tests/integration/test_bulkcreate_objects.py index c63d287..793265a 100644 --- a/inventree_bulk_plugin/tests/integration/test_bulkcreate_objects.py +++ b/inventree_bulk_plugin/tests/integration/test_bulkcreate_objects.py @@ -408,8 +408,8 @@ def extra_model_tests(self, obj): Attachment, obj.fields["attachments"].items_type.fields, f"{obj.template_type}.attachments.[x]", - ignore_fields=["file_url", "file_name", "file_headers", "model_type", "model_id"], - ignore_model_required_fields=["part"], + ignore_fields=["file_url", "file_name", "file_headers"], + ignore_model_required_fields=["part", "model_type", "model_id"], )) issues.extend(self.model_test( From 6e48a53b68c3b9b7bb5e531064da782ee1de7342 Mon Sep 17 00:00:00 2001 From: wolflu05 <76838159+wolflu05@users.noreply.github.com> Date: Mon, 15 Jul 2024 21:30:15 +0200 Subject: [PATCH 5/5] fix: test --- inventree_bulk_plugin/bulkcreate_objects.py | 26 ++++++++++----------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/inventree_bulk_plugin/bulkcreate_objects.py b/inventree_bulk_plugin/bulkcreate_objects.py index c5a1fcd..5ace594 100644 --- a/inventree_bulk_plugin/bulkcreate_objects.py +++ b/inventree_bulk_plugin/bulkcreate_objects.py @@ -645,21 +645,19 @@ def add_attachment(self, part: Part, *, comment: str, link: str, attachment: Fil upload_user=self.request.user, ) return - except ImportError: - pass - except Exception as e: + except ImportError: # pragma: no cover + # fallback to the legacy attachment system + from part.models import PartAttachment + + PartAttachment.objects.create( + part=part, + link=link, + comment=comment, + attachment=attachment, + user=self.request.user, + ) + except Exception as e: # pragma: no cover raise e - - # fallback to the legacy attachment system - from part.models import PartAttachment - - PartAttachment.objects.create( - part=part, - link=link, - comment=comment, - attachment=attachment, - user=self.request.user, - ) bulkcreate_objects: dict[str, type[BulkCreateObject]] = {