Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[14.0][ADD] script_file_import #279

Open
wants to merge 3 commits into
base: 14.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions script_file_import/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
24 changes: 24 additions & 0 deletions script_file_import/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2023 Akretion (https://www.akretion.com).
# @author Kévin Roche <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

{
"name": "Script File Import",
"summary": "base module for import with script",
"version": "14.0.1.0.0",
"category": "tools",
"website": "https://github.com/akretion/ak-odoo-incubator",
"author": "Akretion, Odoo Community Association (OCA)",
"maintainers": ["Kev-Roche"],
"license": "AGPL-3",
"application": False,
"installable": True,
"depends": [
"pattern_import_export",
],
"data": [
"views/menu.xml",
"views/script_file_import.xml",
"security/ir.model.access.csv",
],
}
2 changes: 2 additions & 0 deletions script_file_import/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import script_file_import
from . import abstract_script_processor
51 changes: 51 additions & 0 deletions script_file_import/models/abstract_script_processor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright 2023 Akretion (https://www.akretion.com).
# @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"
_description = "Abstract Script Processor"

def _process_line(self, line):
raise NotImplementedError()

def run(self, data):
headers = set()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

superfluous line ?

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()
try:
with self.env.cr.savepoint():
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(
f,
dialect=dialect,
fieldnames=headers,
)
writer.writeheader()
for row in data:
writer.writerow(row)
f.seek(0)
return base64.b64encode(f.read().encode("utf-8"))
43 changes: 43 additions & 0 deletions script_file_import/models/script_file_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright 2023 Akretion (https://www.akretion.com).
# @author Kévin Roche <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import fields, models


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):
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
1 change: 1 addition & 0 deletions script_file_import/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Kévin Roche <[email protected]>
1 change: 1 addition & 0 deletions script_file_import/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This module allows to
1 change: 1 addition & 0 deletions script_file_import/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This module allows to
3 changes: 3 additions & 0 deletions script_file_import/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_script_file_import,script.file.import.user,model_script_file_import,base.group_user,1,1,1,1
access_abstract_script_processor,abstract.script.processor.user,model_abstract_script_processor,base.group_user,1,1,1,1
21 changes: 21 additions & 0 deletions script_file_import/views/menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!-- Copyright (C) 2023 Akretion (<http://www.akretion.com>).
@author Kévin Roche <[email protected]>
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo>
<record id="action_script_file_import" model="ir.actions.act_window">
<field name="name">Script File Import</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">script.file.import</field>
<field name="view_mode">tree,form</field>
</record>

<menuitem
id="script_file_import_menu"
name="Script File Import"
parent="pattern_import_export.import_export_menu_root"
sequence="10"
groups="base.group_user"
action="action_script_file_import"
/>
</odoo>
60 changes: 60 additions & 0 deletions script_file_import/views/script_file_import.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!-- Copyright (C) 2023 Akretion (<http://www.akretion.com>).
@author Kévin Roche <[email protected]>
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo>
<record id="script_file_import_view_form" model="ir.ui.view">
<field name="name">script_file_import_form</field>
<field name="model">script.file.import</field>
<field name="arch" type="xml">
<form>
<header>
<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_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>
</form>
</field>
</record>

<record id="script_file_import_view_tree" model="ir.ui.view">
<field name="model">script.file.import</field>
<field name="arch" type="xml">
<tree string="Script">
<field name="in_filename" />
<field name="create_date" />
<field name="state" />
</tree>
</field>
</record>

<record id="script_file_import_view_search" model="ir.ui.view">
<field name="model">script.file.import</field>
<field name="arch" type="xml">
<search string="Script">
<field name="in_filename" />
</search>
</field>
</record>

</odoo>
1 change: 1 addition & 0 deletions setup/script_file_import/odoo/addons/script_file_import
6 changes: 6 additions & 0 deletions setup/script_file_import/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)
Loading