Skip to content

Commit

Permalink
[WIP] start adding xml support
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienbeau committed Dec 10, 2021
1 parent f35935c commit 791bfcb
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 6 deletions.
1 change: 1 addition & 0 deletions chunk_processing/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
],
"data": [
"views/chunk_item_view.xml",
"views/chunk_group_view.xml",
],
"demo": [],
}
2 changes: 2 additions & 0 deletions chunk_processing/components/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from . import processor
from . import processor_xml
from . import splitter
from . import splitter_json
from . import splitter_xml
26 changes: 26 additions & 0 deletions chunk_processing/components/processor_xml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2021 Akretion (https://www.akretion.com).
# @author Sébastien BEAU <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

import base64

from lxml import objectify

from odoo.addons.component.core import AbstractComponent


class ChunkProcessorXml(AbstractComponent):
_name = "chunk.importer.xml"
_collection = "chunk.item"

def _parse_data(self):
return objectify.fromstring(
base64.b64decode(self.collection.data)
).iterchildren()

def _import_item(self):
raise NotImplementedError

def run(self):
for item in self._parse_data():
self._import_item(item)
11 changes: 8 additions & 3 deletions chunk_processing/components/splitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# @author Sébastien BEAU <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

import base64

from odoo.addons.component.core import AbstractComponent


Expand All @@ -12,12 +14,15 @@ class ChunkSplitter(AbstractComponent):
def _parse_data(self, data):
raise NotImplementedError

def _prepare_chunk(self, start_idx, stop_idx, data):
def _convert_items_to_data(self, items):
raise NotImplementedError

def _prepare_chunk(self, start_idx, stop_idx, items):
return {
"start_idx": start_idx,
"stop_idx": stop_idx,
"data": data,
"nbr_item": len(data),
"data": base64.b64encode(self._convert_items_to_data(items)),
"nbr_item": len(items),
"state": "pending",
"group_id": self.collection.id,
}
Expand Down
25 changes: 25 additions & 0 deletions chunk_processing/components/splitter_xml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2021 Akretion (https://www.akretion.com).
# @author Sébastien BEAU <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from lxml import etree

from odoo.addons.component.core import Component


class ChunkSplitterXml(Component):
_inherit = "chunk.splitter"
_name = "chunk.splitter.xml"
_usage = "xml"

def _parse_data(self, data):
tree = etree.fromstring(data)
items = tree.xpath(self.collection.xml_split_xpath)
for idx, item in enumerate(items):
yield idx + 1, item

def _convert_items_to_data(self, items):
data = etree.Element("data")
for item in items:
data.append(item[1])
return etree.tostring(data)
1 change: 1 addition & 0 deletions chunk_processing/models/chunk_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ChunkGroup(models.Model):
("xml", "XML"),
]
)
xml_split_xpath = fields.Char()
state = fields.Selection(
[("pending", "Pending"), ("failed", "Failed"), ("done", "Done")],
default="pending",
Expand Down
2 changes: 1 addition & 1 deletion chunk_processing/models/chunk_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ChunkItem(models.Model):
)
start_idx = fields.Integer()
stop_idx = fields.Integer()
data = fields.Serialized()
data = fields.Binary()
record_ids = fields.Serialized()
messages = fields.Serialized()
result_info = fields.Html()
Expand Down
64 changes: 64 additions & 0 deletions chunk_processing/views/chunk_group_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>

<record id="chunk_group_view_tree" model="ir.ui.view">
<field name="model">chunk.group</field>
<field name="arch" type="xml">
<tree string="Chunk Group">
<field name="data_format" />
<field name="date_done" />
</tree>
</field>
</record>

<record id="chunk_group_view_form" model="ir.ui.view">
<field name="model">chunk.group</field>
<field name="arch" type="xml">
<form string="Chunk Group" create="false">
<header>
<button
name="split_in_chunk"
string="Re-launch Import"
type="object"
confirm="Are you sure to re-split the current file?"
/>
<field name="state" widget="statusbar" />
</header>
<sheet>
<group>
<group name="config" string="Config">
<field name="process_multi" readonly="1" />
<field name="chunk_size" readonly="1" />
<field name="job_priority" readonly="1" />
<field name="data_format" readonly="1" />
<field name="apply_on_model" readonly="1" />
<field name="usage" readonly="1" />
</group>
<group>
<field name="create_date" readonly="1" />
<field name="date_done" readonly="1" />
</group>
</group>
<notebook>
<page name="chunk" string="Chunk">
<div>
<label for="progress" />
<field name="progress" readonly="1" widget="progressbar" />
</div>
<group>
<field name="nbr_error" />
<field name="nbr_success" />
</group>
<field
name="info"
readonly="1"
attrs="{'invisible': [('info', '=', False)]}"
/>
<field name="item_ids" nolabel="1" />
</page>
</notebook>
</sheet>
</form>
</field>
</record>
</odoo>
1 change: 1 addition & 0 deletions chunk_processing/views/chunk_item_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
</header>
<sheet>
<group>
<field name="data" />
<field name="start_idx" />
<field name="stop_idx" />
<field name="nbr_error" />
Expand Down
4 changes: 2 additions & 2 deletions pattern_import_export/models/pattern_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class PatternFile(models.Model):
pattern_config_id = fields.Many2one(
"pattern.config", required=True, string="Export pattern"
)
chunk_group_id = fields.Many2one("chunk.group")
chunk_group_id = fields.Many2one("chunk.group", string="Chunk Group")
chunk_item_ids = fields.One2many("chunk.item", related="chunk_group_id.item_ids")
state = fields.Selection(
[("pending", "Pending"), ("failed", "Failed"), ("done", "Done")],
Expand All @@ -29,7 +29,7 @@ class PatternFile(models.Model):
info = fields.Char(related="chunk_group_id.info")

_sql_constraints = [
("uniq_group_id", "unique(group_id)", "The Group must be unique!")
("uniq_chunk_group_id", "unique(chunk_group_id)", "The Group must be unique!")
]

def _add_chunk_group(self):
Expand Down

0 comments on commit 791bfcb

Please sign in to comment.