Skip to content

Commit 83c7ca1

Browse files
[FIX] error messages do not appear in the excel file during import and chunks are not split correctly
1 parent 3be7d81 commit 83c7ca1

File tree

7 files changed

+46
-13
lines changed

7 files changed

+46
-13
lines changed

pattern_import_export/models/pattern_chunk.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,15 @@ def run(self):
7373
"state": "failed",
7474
}
7575
)
76-
self.with_delay().check_last()
76+
if not self.messages:
77+
self.messages = {"message": e.args[0]}
78+
next_chunk = self.get_next_chunk()
79+
if next_chunk:
80+
config = self.pattern_file_id.pattern_config_id
81+
priority = config.job_priority
82+
next_chunk.with_delay(priority=priority).run()
83+
else:
84+
self.with_delay().check_last()
7785
return "OK"
7886

7987
def _prepare_chunk_result(self, res):

pattern_import_export/models/pattern_config.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ def _get_header(self, use_description=False):
128128
header = []
129129
for export_line in self.export_fields:
130130
header.extend(export_line._get_header(use_description))
131+
header.insert(0, "#Error")
131132
return header
132133

133134
def generate_pattern(self):
@@ -173,7 +174,10 @@ def json2pattern_format(self, data):
173174
key = key.replace(IDENTIFIER_SUFFIX, "")
174175
if key == ".id":
175176
key = "id"
176-
val = val[key]
177+
if key == "#Error":
178+
val = None
179+
else:
180+
val = val[key]
177181
if val is None:
178182
break
179183
except IndexError:

pattern_import_export/models/pattern_file.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ def _compute_stat(self):
3939
record.progress = (record.nbr_error + record.nbr_success) * 100.0 / todo
4040
else:
4141
record.progress = 0
42+
if record.nbr_error and record.state != "failed":
43+
record.state = "failed"
4244

4345
@api.model_create_multi
4446
def create(self, vals):
@@ -141,7 +143,7 @@ def _prepare_chunk(self, start_idx, stop_idx, data):
141143
def _should_create_chunk(self, items, next_item):
142144
"""Customise this code if you want to add some additionnal
143145
item after reaching the limit"""
144-
return len(items) > self.pattern_config_id.chunk_size
146+
return len(items) >= self.pattern_config_id.chunk_size
145147

146148
def _create_chunk(self, start_idx, stop_idx, data):
147149
vals = self._prepare_chunk(start_idx, stop_idx, data)
@@ -157,7 +159,7 @@ def split_in_chunk(self):
157159
self.chunk_ids.unlink()
158160
try:
159161
items = []
160-
start_idx = 1
162+
start_idx = self.pattern_config_id.nr_of_header_rows + 1
161163
previous_idx = None
162164
# idx is the index position in the original file
163165
# we can have empty line that can be skipped
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
from . import pattern_config
22
from . import pattern_file
3+
from . import pattern_chunk
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright 2020 Akretion (https://www.akretion.com).
2+
# @author Sébastien BEAU <[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 PatternChunk(models.Model):
9+
_inherit = "pattern.chunk"
10+
11+
def check_last(self):
12+
res = super().check_last()
13+
if res == "There is still some running chunk":
14+
for record in self:
15+
record.pattern_file_id.write_error_in_xlsx()
16+
return res

pattern_import_export_xlsx/models/pattern_config.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def _build_main_sheet_structure(self, book):
4343
for row, lines in enumerate(self._get_output_headers(), start=1):
4444
for col, header in enumerate(lines.values(), start=1):
4545
main_sheet.cell(row=row, column=col, value=header)
46+
main_sheet.column_dimensions['A'].hidden=True
4647
return main_sheet
4748

4849
def _populate_main_sheet_rows(self, main_sheet, records):
@@ -83,7 +84,7 @@ def _create_validators(self, main_sheet, records, tabs):
8384
formula_range_src = "=" + quote_sheetname(tab_name) + "!" + range_src
8485
validation = DataValidation(type="list", formula1=formula_range_src)
8586
for idx_col in tab["idx_col_validator"]:
86-
col_letter_dst = get_column_letter(idx_col)
87+
col_letter_dst = get_column_letter(idx_col+1)
8788
range_dst = "${}${}:${}${}".format(
8889
col_letter_dst,
8990
str(self.row_start_records),

pattern_import_export_xlsx/models/pattern_file.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -70,23 +70,24 @@ def write_error_in_xlsx(self):
7070
infile = BytesIO(base64.b64decode(self.datas))
7171
wb = openpyxl.load_workbook(filename=infile)
7272
ws = self._get_worksheet(wb)
73+
ws.column_dimensions['A'].hidden=False
7374

7475
# we clear the error col if exist
7576
if ws["A1"].value == _("#Error"):
7677
ws.delete_cols(1)
7778
ws.insert_cols(1)
7879
ws.cell(1, 1, value=_("#Error"))
79-
last_row_idx = 0
80-
for chunk in self.chunk_ids:
81-
for message in chunk.messages:
82-
if "rows" in message:
83-
last_row_idx = message["rows"]["to"]
84-
ws.cell(message["rows"]["to"], 1, value=message["message"].strip())
80+
for chunk in self.chunk_ids.filtered(lambda c: c.nbr_error > 0):
81+
for key, message in chunk.messages.items():
82+
if "rows" in key:
83+
ws.cell(message["rows"]["to"], 1, value=message)
8584
else:
8685
# If no row are specify, this is a global message
8786
# that should be applied until the end of the chunk
88-
for idx in range(last_row_idx, chunk.stop_idx + 1):
89-
ws.cell(idx, 1, value=message["message"].strip())
87+
for idx in range(chunk.start_idx, chunk.stop_idx + 1):
88+
if self.pattern_config_id.nr_of_header_rows == 2 and idx == 2:
89+
idx = idx + 1
90+
ws.cell(idx, 1, value=message)
9091
output = BytesIO()
9192
wb.save(output)
9293
self.datas = base64.b64encode(output.getvalue())

0 commit comments

Comments
 (0)