diff --git a/pattern_import_export/models/pattern_chunk.py b/pattern_import_export/models/pattern_chunk.py index 23c0947b..e3fc948e 100644 --- a/pattern_import_export/models/pattern_chunk.py +++ b/pattern_import_export/models/pattern_chunk.py @@ -40,6 +40,7 @@ def run_import(self): "model": model, "record_ids": [], "purge_one2many": self.pattern_file_id.pattern_config_id.purge_one2many, + "pattern_file": self.pattern_file_id, } ) .env[model] diff --git a/pattern_import_export_custom_header/__manifest__.py b/pattern_import_export_custom_header/__manifest__.py index 575f7580..93e077ca 100644 --- a/pattern_import_export_custom_header/__manifest__.py +++ b/pattern_import_export_custom_header/__manifest__.py @@ -4,7 +4,7 @@ { "name": "Pattern export custom header", - "summary": "Allow to use custom headers names in export files", + "summary": "Allow to use custom headers names in export or import files", "version": "14.0.1.0.1", "category": "Extra Tools", "website": "https://github.com/shopinvader/pattern-import-export", diff --git a/pattern_import_export_custom_header/models/__init__.py b/pattern_import_export_custom_header/models/__init__.py index 9d55f4ed..872d411f 100644 --- a/pattern_import_export_custom_header/models/__init__.py +++ b/pattern_import_export_custom_header/models/__init__.py @@ -1 +1,2 @@ from . import pattern_config +from . import base diff --git a/pattern_import_export_custom_header/models/base.py b/pattern_import_export_custom_header/models/base.py new file mode 100644 index 00000000..17f10f9a --- /dev/null +++ b/pattern_import_export_custom_header/models/base.py @@ -0,0 +1,30 @@ +# Copyright (C) 2023 Akretion (). +# @author Chafique Delli +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import models + + +class Base(models.AbstractModel): + _inherit = "base" + + def _pattern_format2json(self, row): + pattern_config_ctx = self._context.get("pattern_config") + if pattern_config_ctx: + pattern_file = pattern_config_ctx.get("pattern_file") + pattern_config = pattern_file.pattern_config_id + new_row = {} + for custom_header in pattern_config.custom_header_ids: + value = "" + if custom_header.custom_name and custom_header.custom_name in row: + value = row[custom_header.custom_name] + elif ( + custom_header.initial_header_name + and custom_header.initial_header_name in row + ): + value = row[custom_header.initial_header_name] + if not value and custom_header.import_default_value: + value = custom_header.import_default_value + new_row[custom_header.initial_header_name] = value + row = new_row + return super()._pattern_format2json(row=row) diff --git a/pattern_import_export_custom_header/models/pattern_config.py b/pattern_import_export_custom_header/models/pattern_config.py index f7d433cf..80fd045a 100644 --- a/pattern_import_export_custom_header/models/pattern_config.py +++ b/pattern_import_export_custom_header/models/pattern_config.py @@ -19,7 +19,7 @@ class PatternConfig(models.Model): def _map_with_custom_header(self, data): return { - item.name: data.get(item.initial_header_name) + item.name: data.get(item.initial_header_name) or item.export_default_value for item in self.custom_header_ids } @@ -72,6 +72,8 @@ class PatternCustomHeader(models.Model): custom_name = fields.Char(string="Custom Header Name") initial_header_name = fields.Char(string="Initial Header Name") pattern_id = fields.Many2one("pattern.config", required=True) + import_default_value = fields.Char(string="Import default Value") + export_default_value = fields.Char(string="Export default Value") def _compute_name(self): for record in self: diff --git a/pattern_import_export_custom_header/views/pattern_config.xml b/pattern_import_export_custom_header/views/pattern_config.xml index 292dbf36..824e308f 100644 --- a/pattern_import_export_custom_header/views/pattern_config.xml +++ b/pattern_import_export_custom_header/views/pattern_config.xml @@ -30,6 +30,8 @@ name="custom_name" attrs="{'required': [('initial_header_name', '=', False)]}" /> + +