Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
bealdav committed Oct 29, 2024
1 parent 7a6316a commit bcd1d78
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 52 deletions.
2 changes: 1 addition & 1 deletion polars_db_process/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
"data": [
"security/ir.model.access.xml",
"wizards/df_process.xml",
"wizards/df_process_wiz.xml",
"views/model_map.xml",
"views/df_field.xml",
"views/df_source.xml",
Expand Down
2 changes: 1 addition & 1 deletion polars_db_process/wizards/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from . import df_process
from . import df_process_wiz
50 changes: 0 additions & 50 deletions polars_db_process/wizards/df_process.py

This file was deleted.

87 changes: 87 additions & 0 deletions polars_db_process/wizards/df_process_wiz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import logging

from odoo import _, exceptions, models

logger = logging.getLogger(__name__)


class DfProcessWiz(models.TransientModel):
_inherit = "df.process.wiz"

def _pre_process(self):
res = super()._pre_process()
if not self.file:
self._pre_process_query()
return res

def _pre_process_query(self):
"You may inherit to set your own behavior"
if not self.df_source_id.db_conf_id:
raise exceptions.ValidationError(
_("Missing database configuration in your df source ")
)
self._process_query()

def _process_query(self):
self.ensure_one()
df = self.df_source_id.db_conf_id._read_sql(self.df_source_id.query)
if self.model_map_id:
if self.model_map_id.action == "import":
self._data_import(df)

def _data_import(self, df):
model = self.model_map_id.model_id.model
vals_list = df.to_dicts()
mapper = {}
rebellious = {}
for vals in vals_list:
for key in self._get_rebellious_fields().get(model):
if key in vals:
rebellious[key] = vals.pop(key)
uidstring = vals.pop("id")
nvals = {
x: val for x, val in vals.items() if x in self.env[model]._fields.keys()
}
if "parent_id" in nvals:
# TODO move this specific behavior elsewhere
nvals["parent_id"] = mapper.get(nvals["parent_id"])
rec = self.env[model].create(nvals)
mapper[uidstring] = rec.id
logger.info(f" >>> {vals}")
self._set_uidstring(uidstring, rec, model)
self._process_rebellious_fields(rec, rebellious)

def _set_uidstring(self, uidstring, record, model):
"""Create Unique Id String also know as XmlId in the Odoo world,
even if not really xml ;-)"""
self.env["ir.model.data"].create(
{
"res_id": record.id,
"model": model,
"module": self.model_map_id._get_uidstring_module_name(),
"name": uidstring,
"noupdate": False,
}
)

def _process_rebellious_fields(self, record, rebellious):
"""Override Suggestion:
self._rebellious_fallback(record, rebellious)
or any other alternative
"""

def _rebellious_fallback(self, record, rebellious):
for key in rebellious:
try:
record[key] = rebellious[key]
except Exception:
logger.warning(f"\n\n\n\n\nPb here {rebellious[key]}")
continue

def _get_rebellious_fields(self):
"""inherit me
Some fields may break your process and could be benefit
of a specific process. We have to know them
i.e. {"res.partner": ["vat"]}
"""
return {}
File renamed without changes.

0 comments on commit bcd1d78

Please sign in to comment.