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 bd39a80 commit 7a6316a
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 21 deletions.
5 changes: 3 additions & 2 deletions polars_db_process/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
"data": [
"security/ir.model.access.xml",
"wizards/df_process.xml",
"views/dataframe.xml",
"views/model_map.xml",
"views/df_field.xml",
"views/df_source.xml",
"views/db_config.xml",
"data/demo.xml",
"data/action.xml",
"data/demo.xml", # TODO remove
],
"demo": [
"data/demo.xml",
Expand Down
10 changes: 10 additions & 0 deletions polars_db_process/data/action.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<record id="remove_related_uidstring_record" model="ir.actions.server">
<field name="name">Remove ir_model_data created from there</field>
<field name="model_id" ref="polars_db_process.model_model_map" />
<field name="binding_model_id" ref="polars_db_process.model_model_map" />
<field name="state">code</field>
<field name="code">env['model.map']._remove_related_uidstring_record()</field>
</record>
</odoo>
2 changes: 1 addition & 1 deletion polars_db_process/data/demo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
>sqlite://.../polars_db_schema/tests/files/chinook.sqlite</field>
</record>

<record id="contact_chinook" model="dataframe">
<record id="contact_chinook" model="model.map">
<field name="model_id" ref="base.model_res_partner" />
<field name="code">Chinook Customers</field>
</record>
Expand Down
1 change: 1 addition & 0 deletions polars_db_process/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from . import df_source
from . import db_config
from . import model_map
3 changes: 3 additions & 0 deletions polars_db_process/models/db_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@ def _read_sql(self, query):
raise exceptions.ValidationError(err) from err
except Exception as err:
raise exceptions.ValidationError(err) from err

def _set_uidstring_module_name(self):
return "polars"
22 changes: 11 additions & 11 deletions polars_db_process/models/df_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

HELP = """Supported files: .xlsx and .sql
Sql files may contains a comment on first line
to be mapped automatically with dataframe, i.e:\n
to be mapped automatically with model_map, i.e:\n
-- {'model_id': 'product.product', 'db_conf_id': mydb}
-- {'code': 'my_delivery_address', 'db_conf_id': mydb}
"""
Expand All @@ -24,7 +24,7 @@ class DfSource(models.Model):
)

def _file_hook(self, file):
"Map sql file with the right Odoo model via dataframe and the right db.config"
"Map sql file with the right Odoo model via model_map and the right db.config"
vals = super()._file_hook(file)
if ".sql" in file:
# TODO: improve
Expand All @@ -36,31 +36,31 @@ def _file_hook(self, file):
model_name = metadata.get("model")
model = self.env["ir.model"].search([("model", "=", model_name)])
if model_name:
# we don't want to use these dataframes
dataframes = (
# we don't want to use these model_maps
model_maps = (
self.env["df.source"]
.search([])
.filtered(lambda s: not s.db_conf_id)
.mapped("dataframe_id")
.mapped("model_map_id")
)
dataframe = self.env["dataframe"].search(
model_map = self.env["model.map"].search(
[
("id", "not in", dataframes.ids),
("id", "not in", model_maps.ids),
("model_id", "=", model_name),
]
)
if dataframe:
if model_map:
# TODO use first
vals["dataframe_id"] = dataframe[0].id
vals["model_map_id"] = model_map[0].id
db_config = self.env["db.config"].search(
[("name", "ilike", metadata.get("db_conf_id"))]
)
vals["db_conf_id"] = db_config and db_config[0].id or False
else:
df = self.env["dataframe"].create(
df = self.env["model.map"].create(
{"code": model.name, "model_id": model and model[0].id}
)
vals["dataframe_id"] = df.id
vals["model_map_id"] = df.id
vals["query"] = content
return vals

Expand Down
20 changes: 20 additions & 0 deletions polars_db_process/models/model_map.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from odoo import models


class ModelMap(models.Model):
_inherit = "model.map"

def _remove_related_uidstring_record(self):
self = self.browse(self.env.context.get("active_ids"))
self = self and self[0] or False
res = self.env["ir.model.data"].search(
[
("model", "=", self.model_id.model),
("module", "=", self._get_uidstring_module_name()),
]
)
res.reference.unlink()
return True

def _get_uidstring_module_name(self):
return "polars"
2 changes: 1 addition & 1 deletion polars_db_process/views/db_config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
</field>
</record>

<record id="dataframe_list" model="ir.ui.view">
<record id="model_map_list" model="ir.ui.view">
<field name="model">db.config</field>
<field name="arch" type="xml">
<list>
Expand Down
File renamed without changes.
37 changes: 31 additions & 6 deletions polars_db_process/wizards/df_process.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,50 @@
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_sql()
self._pre_process_query()
return res

def _pre_process_sql(self):
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_sql()
self._process_query()

def _process_sql(self):
def _process_query(self):
self.ensure_one()
df = self.df_source_id.db_conf_id._read_sql(self.df_source_id.query)
if self.dataframe_id:
self.env[self.dataframe_id.model_id.model].create(df.to_dicts())
if self.model_map_id:
model = self.model_map_id.model_id.model
vals_list = df.to_dicts()
mapper = {}
for vals in vals_list:
uidstring = vals.pop("id")
if "parent_id" in vals:
vals["parent_id"] = mapper.get(vals["parent_id"])
rec = self.env[model].create(vals)
mapper[uidstring] = rec.id
logger.info(" >>> ", vals)
self._set_uidstring(uidstring, rec, model)

def _set_uidstring(self, uidstring, record, model):
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,
}
)

0 comments on commit 7a6316a

Please sign in to comment.