Skip to content

Commit

Permalink
script_file_import: improve ui, fix import
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienbeau committed Sep 18, 2023
1 parent 903e7af commit 722f214
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 18 deletions.
40 changes: 29 additions & 11 deletions script_file_import/models/abstract_script_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
# @author Kévin Roche <kevin.roche@akretion.com>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

import base64
import csv
import io
import logging

from odoo import models

_logger = logging.getLogger(__name__)


class AbstractScriptProcessor(models.AbstractModel):
_name = "abstract.script.processor"
Expand All @@ -17,17 +22,30 @@ def _process_line(self, line):
def run(self, data):
headers = set()
output = []
for line in csv.reader(data):
output.append(self._process_line(line))
headers |= set(output.keys())
return self.write_output(headers, output)

def write_output(self, headers, output):
in_file = io.StringIO(base64.b64decode(data).decode("utf-8"))
reader = csv.DictReader(in_file)
for idx, line in enumerate(reader):
_logger.info("Process line %s", idx)
self.flush()
with self.env.cr.savepoint():
try:
self._process_line(line)
except Exception as e:
line["error"] = str(e)
output.append(line)
_logger.error("Script import error %s", e)
headers = ["error"] + reader.fieldnames
return self.write_output(headers, output, reader.dialect)

def write_output(self, headers, data, dialect):
f = io.StringIO()
writer = csv.DictWriter(
output,
delimiter=",",
quotechar='"',
f,
dialect=dialect,
fieldnames=headers,
)

return data
writer.writeheader()
for row in data:
writer.writerow(row)
f.seek(0)
return base64.b64encode(f.read().encode("utf-8"))
24 changes: 20 additions & 4 deletions script_file_import/models/script_file_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,35 @@ class ScriptFileImport(models.Model):
_name = "script.file.import"
_description = "Script File Import"

in_filename = fields.Char()
out_filename = fields.Char(compute="_compute_out_filename")

in_data = fields.Binary(string="Input CSV file", required=True)

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

processor = fields.Selection(
string="Processor", selection="_get_processor", required=True
)
state = fields.Selection(
[
("draft", "Draft"),
("processing", "Processing"),
("done", "Done"),
],
)

def _get_processor(self):
return []

def run_in_background(self):
self.state = "processing"
self.with_delay().run()

def run(self):
selected_processor = dict(
self._fields["processor"]._description_selection(self.env)
).get(self.processor)
self.out_data = self.env[selected_processor].run(self.in_data)
self.out_data = self.env[self.processor].run(self.in_data)
self.state = "done"

def _compute_out_filename(self):
for record in self:
record.out_filename = "Result-" + record.in_filename
21 changes: 18 additions & 3 deletions script_file_import/views/script_file_import.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,27 @@
<field name="arch" type="xml">
<form>
<header>
<button name="run" string="Launch Processor" type="object" />
<button
name="run_in_background"
string="Launch Processor"
type="object"
/>
<field
name="state"
widget="statusbar"
statusbar_visible="draft,processing,done"
/>
</header>
<sheet>
<group>
<field name="in_data" />
<field name="out_data" readonly="1" />
<field name="in_filename" invisible="1" />
<field name="out_filename" invisible="1" />
<field name="in_data" filename="in_filename" />
<field
name="out_data"
filename="out_filename"
readonly="1"
/>
<field name="processor" />
</group>
</sheet>
Expand Down

0 comments on commit 722f214

Please sign in to comment.