diff --git a/pattern_import_export_import_type/__init__.py b/pattern_import_export_import_type/__init__.py new file mode 100644 index 00000000..0650744f --- /dev/null +++ b/pattern_import_export_import_type/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/pattern_import_export_import_type/__manifest__.py b/pattern_import_export_import_type/__manifest__.py new file mode 100644 index 00000000..05ffa3ce --- /dev/null +++ b/pattern_import_export_import_type/__manifest__.py @@ -0,0 +1,22 @@ +# Copyright 2023 Akretion (https://www.akretion.com). +# @author Kévin Roche +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +{ + "name": "Pattern Import Export Import Type", + "summary": "Import Type (only update, create or both)", + "version": "14.0.1.0.0", + "category": "Extra Tools", + "website": "https://github.com/Shopinvader/pattern-import-export", + "author": "Akretion, Odoo Community Association (OCA)", + "maintainers": ["Kev-Roche"], + "license": "AGPL-3", + "application": False, + "installable": True, + "depends": [ + "pattern_import_export", + ], + "data": [ + "views/pattern_config.xml", + ], +} diff --git a/pattern_import_export_import_type/models/__init__.py b/pattern_import_export_import_type/models/__init__.py new file mode 100644 index 00000000..d6effefe --- /dev/null +++ b/pattern_import_export_import_type/models/__init__.py @@ -0,0 +1,3 @@ +from . import base +from . import pattern_chunk +from . import pattern_config diff --git a/pattern_import_export_import_type/models/base.py b/pattern_import_export_import_type/models/base.py new file mode 100644 index 00000000..f9ca7d43 --- /dev/null +++ b/pattern_import_export_import_type/models/base.py @@ -0,0 +1,30 @@ +# Copyright 2023 Akretion (https://www.akretion.com). +# @author Kévin Roche +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +import copy +from odoo import _, models +from odoo.exceptions import ValidationError + + +class Base(models.AbstractModel): + _inherit = "base" + + def _load_records_write(self, values): + import_type = self._context.get("pattern_import_type", None) + if import_type: + if import_type in ("update_and_creation", "update_only"): + return super()._load_records_write(values) + else: + raise ValidationError(_("Import Type not allowing updating record.")) + else: + return super()._load_records_write(values) + + def _load_records_create(self, values): + import_type = self._context.get("pattern_import_type", None) + if import_type: + if import_type in ("update_and_creation", "create_only"): + return super()._load_records_create(values) + else: + raise ValidationError(_("Import Type not allowing record creation.")) + else: + return super()._load_records_create(values) diff --git a/pattern_import_export_import_type/models/pattern_chunk.py b/pattern_import_export_import_type/models/pattern_chunk.py new file mode 100644 index 00000000..613b641d --- /dev/null +++ b/pattern_import_export_import_type/models/pattern_chunk.py @@ -0,0 +1,15 @@ +# Copyright 2023 Akretion (https://www.akretion.com). +# @author Kévin Roche +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import models + + +class PatternChunk(models.Model): + _inherit = "pattern.chunk" + + def run_import(self): + import_type = self.pattern_file_id.pattern_config_id.import_type + return super( + PatternChunk, self.with_context(pattern_import_type=import_type) + ).run_import() diff --git a/pattern_import_export_import_type/models/pattern_config.py b/pattern_import_export_import_type/models/pattern_config.py new file mode 100644 index 00000000..958450c4 --- /dev/null +++ b/pattern_import_export_import_type/models/pattern_config.py @@ -0,0 +1,19 @@ +# Copyright 2023 Akretion (https://www.akretion.com). +# @author Kévin Roche +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class PatternConfig(models.Model): + _inherit = "pattern.config" + + import_type = fields.Selection( + selection=[ + ("update_and_creation", "Update and Creation"), + ("update_only", "Update Only"), + ("create_only", "Creation Only"), + ], + string="Import Type", + default="update_and_creation", + ) diff --git a/pattern_import_export_import_type/readme/CONTRIBUTORS.rst b/pattern_import_export_import_type/readme/CONTRIBUTORS.rst new file mode 100644 index 00000000..dcae277c --- /dev/null +++ b/pattern_import_export_import_type/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Kévin Roche diff --git a/pattern_import_export_import_type/readme/DESCRIPTION.rst b/pattern_import_export_import_type/readme/DESCRIPTION.rst new file mode 100644 index 00000000..98c9b491 --- /dev/null +++ b/pattern_import_export_import_type/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +This module add an Type of import, allowing to create only, update only or both. diff --git a/pattern_import_export_import_type/tests/__init__.py b/pattern_import_export_import_type/tests/__init__.py new file mode 100644 index 00000000..ec5c7972 --- /dev/null +++ b/pattern_import_export_import_type/tests/__init__.py @@ -0,0 +1 @@ +from . import test_pattern_import_export_import_type diff --git a/pattern_import_export_import_type/tests/test_pattern_import_export_import_type.py b/pattern_import_export_import_type/tests/test_pattern_import_export_import_type.py new file mode 100644 index 00000000..9353d646 --- /dev/null +++ b/pattern_import_export_import_type/tests/test_pattern_import_export_import_type.py @@ -0,0 +1,43 @@ +# Copyright 2023 Akretion (https://www.akretion.com). +# @author Kévin Roche +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from uuid import uuid4 +from odoo.tests.common import SavepointCase +from odoo.addons.pattern_import_export.tests.test_pattern_import import ( + TestPatternImport, +) + + +class TestPatternImportExportImportType(TestPatternImport): + @classmethod + def setUpClass(cls): + super().setUpClass() + + def test_import_type_update_only(self): + # simulate a record update with "create only" (fail) + # then with "update only" (success) import type. + unique_name = str(uuid4()) + data = [{"id": self.user3.get_xml_id().get(self.user3.id), "name": unique_name}] + pattern_file = self.create_pattern(self.pattern_config_m2m, "import", data) + pattern_file.pattern_config_id.import_type = "create_only" + records = self.run_pattern_file(pattern_file) + self.assertFalse(records) + + pattern_file.pattern_config_id.import_type = "update_only" + records = self.run_pattern_file(pattern_file) + self.assertEqual(len(records), 1) + + def test_import_type_create(self): + # simulate a record creation with "update only" (fail) + # then with "update and create" (success) import type. + unique_name = str(uuid4()) + unique_login = str(uuid4()) + data = [{"name": unique_name, "login": unique_login}] + pattern_file = self.create_pattern(self.pattern_config_m2m, "import", data) + pattern_file.pattern_config_id.import_type = "update_only" + records = self.run_pattern_file(pattern_file) + self.assertFalse(records) + + pattern_file.pattern_config_id.import_type = "update_and_creation" + records = self.run_pattern_file(pattern_file) + self.assertEqual(len(records), 1) diff --git a/pattern_import_export_import_type/views/pattern_config.xml b/pattern_import_export_import_type/views/pattern_config.xml new file mode 100644 index 00000000..e1982fc1 --- /dev/null +++ b/pattern_import_export_import_type/views/pattern_config.xml @@ -0,0 +1,16 @@ + + + + + pattern.config + + + + + + + + +