Skip to content

Commit f228775

Browse files
[14.0][IMP] add default_value for import and export
1 parent e896fd6 commit f228775

File tree

6 files changed

+38
-2
lines changed

6 files changed

+38
-2
lines changed

pattern_import_export/models/pattern_chunk.py

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def run_import(self):
4040
"model": model,
4141
"record_ids": [],
4242
"purge_one2many": self.pattern_file_id.pattern_config_id.purge_one2many,
43+
"pattern_file": self.pattern_file_id,
4344
}
4445
)
4546
.env[model]

pattern_import_export_custom_header/__manifest__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
{
66
"name": "Pattern export custom header",
7-
"summary": "Allow to use custom headers names in export files",
7+
"summary": "Allow to use custom headers names in export or import files",
88
"version": "14.0.1.0.1",
99
"category": "Extra Tools",
1010
"website": "https://github.com/shopinvader/pattern-import-export",
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
from . import pattern_config
2+
from . import base
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright (C) 2023 Akretion (<http://www.akretion.com>).
2+
# @author Chafique Delli <[email protected]>
3+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
4+
5+
from odoo import models
6+
7+
8+
class Base(models.AbstractModel):
9+
_inherit = "base"
10+
11+
def _pattern_format2json(self, row):
12+
pattern_config_ctx = self._context.get("pattern_config")
13+
if pattern_config_ctx:
14+
pattern_file = pattern_config_ctx.get("pattern_file")
15+
pattern_config = pattern_file.pattern_config_id
16+
new_row = {}
17+
for custom_header in pattern_config.custom_header_ids:
18+
value = ""
19+
if custom_header.custom_name and custom_header.custom_name in row:
20+
value = row[custom_header.custom_name]
21+
elif (
22+
custom_header.initial_header_name
23+
and custom_header.initial_header_name in row
24+
):
25+
value = row[custom_header.initial_header_name]
26+
if not value and custom_header.import_default_value:
27+
value = custom_header.import_default_value
28+
new_row[custom_header.initial_header_name] = value
29+
row = new_row
30+
return super()._pattern_format2json(row=row)

pattern_import_export_custom_header/models/pattern_config.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class PatternConfig(models.Model):
1919

2020
def _map_with_custom_header(self, data):
2121
return {
22-
item.name: data.get(item.initial_header_name)
22+
item.name: data.get(item.initial_header_name) or item.export_default_value
2323
for item in self.custom_header_ids
2424
}
2525

@@ -72,6 +72,8 @@ class PatternCustomHeader(models.Model):
7272
custom_name = fields.Char(string="Custom Header Name")
7373
initial_header_name = fields.Char(string="Initial Header Name")
7474
pattern_id = fields.Many2one("pattern.config", required=True)
75+
import_default_value = fields.Char(string="Import default Value")
76+
export_default_value = fields.Char(string="Export default Value")
7577

7678
def _compute_name(self):
7779
for record in self:

pattern_import_export_custom_header/views/pattern_config.xml

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
name="custom_name"
3131
attrs="{'required': [('initial_header_name', '=', False)]}"
3232
/>
33+
<field name="import_default_value" />
34+
<field name="export_default_value" />
3335
</tree>
3436
</field>
3537
</page>

0 commit comments

Comments
 (0)