Skip to content

Commit 722f214

Browse files
committed
script_file_import: improve ui, fix import
1 parent 903e7af commit 722f214

File tree

3 files changed

+67
-18
lines changed

3 files changed

+67
-18
lines changed

script_file_import/models/abstract_script_processor.py

+29-11
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22
# @author Kévin Roche <kevin.roche@akretion.com>
33
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
44

5+
import base64
56
import csv
7+
import io
8+
import logging
69

710
from odoo import models
811

12+
_logger = logging.getLogger(__name__)
13+
914

1015
class AbstractScriptProcessor(models.AbstractModel):
1116
_name = "abstract.script.processor"
@@ -17,17 +22,30 @@ def _process_line(self, line):
1722
def run(self, data):
1823
headers = set()
1924
output = []
20-
for line in csv.reader(data):
21-
output.append(self._process_line(line))
22-
headers |= set(output.keys())
23-
return self.write_output(headers, output)
24-
25-
def write_output(self, headers, output):
25+
in_file = io.StringIO(base64.b64decode(data).decode("utf-8"))
26+
reader = csv.DictReader(in_file)
27+
for idx, line in enumerate(reader):
28+
_logger.info("Process line %s", idx)
29+
self.flush()
30+
with self.env.cr.savepoint():
31+
try:
32+
self._process_line(line)
33+
except Exception as e:
34+
line["error"] = str(e)
35+
output.append(line)
36+
_logger.error("Script import error %s", e)
37+
headers = ["error"] + reader.fieldnames
38+
return self.write_output(headers, output, reader.dialect)
39+
40+
def write_output(self, headers, data, dialect):
41+
f = io.StringIO()
2642
writer = csv.DictWriter(
27-
output,
28-
delimiter=",",
29-
quotechar='"',
43+
f,
44+
dialect=dialect,
3045
fieldnames=headers,
3146
)
32-
33-
return data
47+
writer.writeheader()
48+
for row in data:
49+
writer.writerow(row)
50+
f.seek(0)
51+
return base64.b64encode(f.read().encode("utf-8"))

script_file_import/models/script_file_import.py

+20-4
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,35 @@ class ScriptFileImport(models.Model):
99
_name = "script.file.import"
1010
_description = "Script File Import"
1111

12+
in_filename = fields.Char()
13+
out_filename = fields.Char(compute="_compute_out_filename")
14+
1215
in_data = fields.Binary(string="Input CSV file", required=True)
1316

1417
out_data = fields.Binary(string="Output CSV file", readonly=True)
1518

1619
processor = fields.Selection(
1720
string="Processor", selection="_get_processor", required=True
1821
)
22+
state = fields.Selection(
23+
[
24+
("draft", "Draft"),
25+
("processing", "Processing"),
26+
("done", "Done"),
27+
],
28+
)
1929

2030
def _get_processor(self):
2131
return []
2232

33+
def run_in_background(self):
34+
self.state = "processing"
35+
self.with_delay().run()
36+
2337
def run(self):
24-
selected_processor = dict(
25-
self._fields["processor"]._description_selection(self.env)
26-
).get(self.processor)
27-
self.out_data = self.env[selected_processor].run(self.in_data)
38+
self.out_data = self.env[self.processor].run(self.in_data)
39+
self.state = "done"
40+
41+
def _compute_out_filename(self):
42+
for record in self:
43+
record.out_filename = "Result-" + record.in_filename

script_file_import/views/script_file_import.xml

+18-3
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,27 @@
99
<field name="arch" type="xml">
1010
<form>
1111
<header>
12-
<button name="run" string="Launch Processor" type="object" />
12+
<button
13+
name="run_in_background"
14+
string="Launch Processor"
15+
type="object"
16+
/>
17+
<field
18+
name="state"
19+
widget="statusbar"
20+
statusbar_visible="draft,processing,done"
21+
/>
1322
</header>
1423
<sheet>
1524
<group>
16-
<field name="in_data" />
17-
<field name="out_data" readonly="1" />
25+
<field name="in_filename" invisible="1" />
26+
<field name="out_filename" invisible="1" />
27+
<field name="in_data" filename="in_filename" />
28+
<field
29+
name="out_data"
30+
filename="out_filename"
31+
readonly="1"
32+
/>
1833
<field name="processor" />
1934
</group>
2035
</sheet>

0 commit comments

Comments
 (0)