-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
script_file_import: improve ui, fix import
- Loading branch information
1 parent
903e7af
commit 722f214
Showing
3 changed files
with
67 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,15 @@ | |
# @author Kévin Roche <[email protected]> | ||
# 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" | ||
|
@@ -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")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters