Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[14.0][ADD] pattern_import_export_import_type #125

Open
wants to merge 1 commit into
base: 14.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pattern_import_export_import_type/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
22 changes: 22 additions & 0 deletions pattern_import_export_import_type/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright 2023 Akretion (https://www.akretion.com).
# @author Kévin Roche <[email protected]>
# 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",
],
}
3 changes: 3 additions & 0 deletions pattern_import_export_import_type/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import base
from . import pattern_chunk
from . import pattern_config
24 changes: 24 additions & 0 deletions pattern_import_export_import_type/models/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2023 Akretion (https://www.akretion.com).
# @author Kévin Roche <[email protected]>
# 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")
if values and import_type and import_type not in ("update_and_creation", "update_only"):
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")
if values and import_type and import_type not in ("update_and_creation", "create_only"):
raise ValidationError(_("Import Type not allowing record creation."))
else:
return super()._load_records_create(values)
15 changes: 15 additions & 0 deletions pattern_import_export_import_type/models/pattern_chunk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2023 Akretion (https://www.akretion.com).
# @author Kévin Roche <[email protected]>
# 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()
19 changes: 19 additions & 0 deletions pattern_import_export_import_type/models/pattern_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2023 Akretion (https://www.akretion.com).
# @author Kévin Roche <[email protected]>
# 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",
)
1 change: 1 addition & 0 deletions pattern_import_export_import_type/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Kévin Roche <[email protected]>
1 change: 1 addition & 0 deletions pattern_import_export_import_type/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This module add an Type of import, allowing to create only, update only or both.
1 change: 1 addition & 0 deletions pattern_import_export_import_type/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_pattern_import_export_import_type
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright 2023 Akretion (https://www.akretion.com).
# @author Kévin Roche <[email protected]>
# 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 = [{"login#key": self.user3.login, "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)
chunk = pattern_file.chunk_ids
self.assertIn("Import Type not allowing updating record.", chunk.result_info)
self.assertNotEqual(unique_name, self.user3.name)
pattern_file.pattern_config_id.import_type = "update_only"
records = self.run_pattern_file(pattern_file)
chunk = pattern_file.chunk_ids
self.assertNotIn("Import Type not allowing", chunk.result_info)
self.assertEqual(unique_name, self.user3.name)

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)
chunk = pattern_file.chunk_ids
self.assertIn("Import Type not allowing record creation.", chunk.result_info)
self.assertFalse(records)

pattern_file.pattern_config_id.import_type = "update_and_creation"
records = self.run_pattern_file(pattern_file)
chunk = pattern_file.chunk_ids
self.assertNotIn("Import Type not allowing", chunk.result_info)
self.assertEqual(len(records), 1)
16 changes: 16 additions & 0 deletions pattern_import_export_import_type/views/pattern_config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!-- Copyright (C) 2023 Akretion (<http://www.akretion.com>).
@author Kévin Roche <[email protected]>
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo>
<record id="pattern_config_form_view" model="ir.ui.view">
<field name="model">pattern.config</field>
<field name="inherit_id" ref="pattern_import_export.pattern_config_form_view" />
<field name="arch" type="xml">
<field name="header_format" position="before">
<field name="import_type" />
</field>
</field>
</record>
</odoo>