-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[14.0][ADD] pattern_import_export_import_type
- Loading branch information
Showing
11 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import models |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# 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", 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
pattern_import_export_import_type/models/pattern_config.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* Kévin Roche <[email protected]> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import test_pattern_import_export_import_type |
43 changes: 43 additions & 0 deletions
43
pattern_import_export_import_type/tests/test_pattern_import_export_import_type.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# 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 = [{"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) |
16 changes: 16 additions & 0 deletions
16
pattern_import_export_import_type/views/pattern_config.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|