Skip to content

Commit 9a40f91

Browse files
committed
[14.0][ADD] pattern_import_export_import_type
1 parent 6f9aad4 commit 9a40f91

11 files changed

+153
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import models
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright 2023 Akretion (https://www.akretion.com).
2+
# @author Kévin Roche <[email protected]>
3+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
4+
5+
{
6+
"name": "Pattern Import Export Import Type",
7+
"summary": "Import Type (only update, create or both)",
8+
"version": "14.0.1.0.0",
9+
"category": "Extra Tools",
10+
"website": "https://github.com/Shopinvader/pattern-import-export",
11+
"author": "Akretion, Odoo Community Association (OCA)",
12+
"maintainers": ["Kev-Roche"],
13+
"license": "AGPL-3",
14+
"application": False,
15+
"installable": True,
16+
"depends": [
17+
"pattern_import_export",
18+
],
19+
"data": [
20+
"views/pattern_config.xml",
21+
],
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from . import base
2+
from . import pattern_chunk
3+
from . import pattern_config
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright 2023 Akretion (https://www.akretion.com).
2+
# @author Kévin Roche <[email protected]>
3+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
4+
import copy
5+
from odoo import _, models
6+
from odoo.exceptions import ValidationError
7+
8+
9+
class Base(models.AbstractModel):
10+
_inherit = "base"
11+
12+
def _load_records_write(self, values):
13+
import_type = self._context.get("pattern_import_type")
14+
if values and import_type and import_type not in ("update_and_creation", "update_only"):
15+
raise ValidationError(_("Import Type not allowing updating record."))
16+
else:
17+
return super()._load_records_write(values)
18+
19+
def _load_records_create(self, values):
20+
import_type = self._context.get("pattern_import_type")
21+
if values and import_type and import_type not in ("update_and_creation", "create_only"):
22+
raise ValidationError(_("Import Type not allowing record creation."))
23+
else:
24+
return super()._load_records_create(values)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright 2023 Akretion (https://www.akretion.com).
2+
# @author Kévin Roche <[email protected]>
3+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
4+
5+
from odoo import models
6+
7+
8+
class PatternChunk(models.Model):
9+
_inherit = "pattern.chunk"
10+
11+
def run_import(self):
12+
import_type = self.pattern_file_id.pattern_config_id.import_type
13+
return super(
14+
PatternChunk, self.with_context(pattern_import_type=import_type)
15+
).run_import()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright 2023 Akretion (https://www.akretion.com).
2+
# @author Kévin Roche <[email protected]>
3+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
4+
5+
from odoo import fields, models
6+
7+
8+
class PatternConfig(models.Model):
9+
_inherit = "pattern.config"
10+
11+
import_type = fields.Selection(
12+
selection=[
13+
("update_and_creation", "Update and Creation"),
14+
("update_only", "Update Only"),
15+
("create_only", "Creation Only"),
16+
],
17+
string="Import Type",
18+
default="update_and_creation",
19+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* Kévin Roche <[email protected]>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This module add an Type of import, allowing to create only, update only or both.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import test_pattern_import_export_import_type
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright 2023 Akretion (https://www.akretion.com).
2+
# @author Kévin Roche <[email protected]>
3+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
4+
from uuid import uuid4
5+
from odoo.tests.common import SavepointCase
6+
from odoo.addons.pattern_import_export.tests.test_pattern_import import (
7+
TestPatternImport,
8+
)
9+
10+
11+
class TestPatternImportExportImportType(TestPatternImport):
12+
@classmethod
13+
def setUpClass(cls):
14+
super().setUpClass()
15+
16+
def test_import_type_update_only(self):
17+
# simulate a record update with "create only" (fail)
18+
# then with "update only" (success) import type.
19+
unique_name = str(uuid4())
20+
data = [{"login#key": self.user3.login, "name": unique_name}]
21+
pattern_file = self.create_pattern(self.pattern_config_m2m, "import", data)
22+
pattern_file.pattern_config_id.import_type = "create_only"
23+
records = self.run_pattern_file(pattern_file)
24+
chunk = pattern_file.chunk_ids
25+
self.assertIn("Import Type not allowing updating record.", chunk.result_info)
26+
self.assertNotEqual(unique_name, self.user3.name)
27+
pattern_file.pattern_config_id.import_type = "update_only"
28+
records = self.run_pattern_file(pattern_file)
29+
chunk = pattern_file.chunk_ids
30+
self.assertNotIn("Import Type not allowing", chunk.result_info)
31+
self.assertEqual(unique_name, self.user3.name)
32+
33+
def test_import_type_create(self):
34+
# simulate a record creation with "update only" (fail)
35+
# then with "update and create" (success) import type.
36+
unique_name = str(uuid4())
37+
unique_login = str(uuid4())
38+
data = [{"name": unique_name, "login": unique_login}]
39+
pattern_file = self.create_pattern(self.pattern_config_m2m, "import", data)
40+
pattern_file.pattern_config_id.import_type = "update_only"
41+
records = self.run_pattern_file(pattern_file)
42+
chunk = pattern_file.chunk_ids
43+
self.assertIn("Import Type not allowing record creation.", chunk.result_info)
44+
self.assertFalse(records)
45+
46+
pattern_file.pattern_config_id.import_type = "update_and_creation"
47+
records = self.run_pattern_file(pattern_file)
48+
chunk = pattern_file.chunk_ids
49+
self.assertNotIn("Import Type not allowing", chunk.result_info)
50+
self.assertEqual(len(records), 1)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!-- Copyright (C) 2023 Akretion (<http://www.akretion.com>).
3+
@author Kévin Roche <[email protected]>
4+
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
5+
<odoo>
6+
<record id="pattern_config_form_view" model="ir.ui.view">
7+
<field name="model">pattern.config</field>
8+
<field name="inherit_id" ref="pattern_import_export.pattern_config_form_view" />
9+
<field name="arch" type="xml">
10+
<field name="header_format" position="before">
11+
<field name="import_type" />
12+
</field>
13+
</field>
14+
</record>
15+
</odoo>
16+

0 commit comments

Comments
 (0)