From 791bfcbf32992f8a749fdc214038320f784bac06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20BEAU?= Date: Thu, 9 Dec 2021 18:12:02 +0100 Subject: [PATCH] [WIP] start adding xml support --- chunk_processing/__manifest__.py | 1 + chunk_processing/components/__init__.py | 2 + chunk_processing/components/processor_xml.py | 26 ++++++++ chunk_processing/components/splitter.py | 11 +++- chunk_processing/components/splitter_xml.py | 25 ++++++++ chunk_processing/models/chunk_group.py | 1 + chunk_processing/models/chunk_item.py | 2 +- chunk_processing/views/chunk_group_view.xml | 64 ++++++++++++++++++++ chunk_processing/views/chunk_item_view.xml | 1 + pattern_import_export/models/pattern_file.py | 4 +- 10 files changed, 131 insertions(+), 6 deletions(-) create mode 100644 chunk_processing/components/processor_xml.py create mode 100644 chunk_processing/components/splitter_xml.py create mode 100644 chunk_processing/views/chunk_group_view.xml diff --git a/chunk_processing/__manifest__.py b/chunk_processing/__manifest__.py index 934a070a..0a17d355 100644 --- a/chunk_processing/__manifest__.py +++ b/chunk_processing/__manifest__.py @@ -23,6 +23,7 @@ ], "data": [ "views/chunk_item_view.xml", + "views/chunk_group_view.xml", ], "demo": [], } diff --git a/chunk_processing/components/__init__.py b/chunk_processing/components/__init__.py index 3242cabf..0466f74e 100644 --- a/chunk_processing/components/__init__.py +++ b/chunk_processing/components/__init__.py @@ -1,3 +1,5 @@ from . import processor +from . import processor_xml from . import splitter from . import splitter_json +from . import splitter_xml diff --git a/chunk_processing/components/processor_xml.py b/chunk_processing/components/processor_xml.py new file mode 100644 index 00000000..ff85cd0b --- /dev/null +++ b/chunk_processing/components/processor_xml.py @@ -0,0 +1,26 @@ +# Copyright 2021 Akretion (https://www.akretion.com). +# @author Sébastien BEAU +# 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) diff --git a/chunk_processing/components/splitter.py b/chunk_processing/components/splitter.py index 96813c88..c939d6a8 100644 --- a/chunk_processing/components/splitter.py +++ b/chunk_processing/components/splitter.py @@ -2,6 +2,8 @@ # @author Sébastien BEAU # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +import base64 + from odoo.addons.component.core import AbstractComponent @@ -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, } diff --git a/chunk_processing/components/splitter_xml.py b/chunk_processing/components/splitter_xml.py new file mode 100644 index 00000000..88df4890 --- /dev/null +++ b/chunk_processing/components/splitter_xml.py @@ -0,0 +1,25 @@ +# Copyright 2021 Akretion (https://www.akretion.com). +# @author Sébastien BEAU +# 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) diff --git a/chunk_processing/models/chunk_group.py b/chunk_processing/models/chunk_group.py index d2b64be7..df02093a 100644 --- a/chunk_processing/models/chunk_group.py +++ b/chunk_processing/models/chunk_group.py @@ -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", diff --git a/chunk_processing/models/chunk_item.py b/chunk_processing/models/chunk_item.py index 562f2d1b..d80cca5f 100644 --- a/chunk_processing/models/chunk_item.py +++ b/chunk_processing/models/chunk_item.py @@ -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() diff --git a/chunk_processing/views/chunk_group_view.xml b/chunk_processing/views/chunk_group_view.xml new file mode 100644 index 00000000..cc9fd600 --- /dev/null +++ b/chunk_processing/views/chunk_group_view.xml @@ -0,0 +1,64 @@ + + + + + chunk.group + + + + + + + + + + chunk.group + +
+
+
+ + + + + + + + + + + + + + + + + +
+
+ + + + + + +
+
+
+
+
+
+
diff --git a/chunk_processing/views/chunk_item_view.xml b/chunk_processing/views/chunk_item_view.xml index e1334344..f1d30232 100644 --- a/chunk_processing/views/chunk_item_view.xml +++ b/chunk_processing/views/chunk_item_view.xml @@ -23,6 +23,7 @@ + diff --git a/pattern_import_export/models/pattern_file.py b/pattern_import_export/models/pattern_file.py index ee085337..8f6e4c2b 100644 --- a/pattern_import_export/models/pattern_file.py +++ b/pattern_import_export/models/pattern_file.py @@ -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")], @@ -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):