Skip to content

Commit

Permalink
[14.0][ADD] pattern_import_export_import_type
Browse files Browse the repository at this point in the history
  • Loading branch information
Kev-Roche committed Sep 1, 2023
1 parent 6f9aad4 commit 7b90201
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 0 deletions.
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
30 changes: 30 additions & 0 deletions pattern_import_export_import_type/models/base.py
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)
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,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 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>

0 comments on commit 7b90201

Please sign in to comment.