Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
bealdav committed Oct 31, 2024
1 parent 3fd0732 commit ce86d44
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 66 deletions.
67 changes: 16 additions & 51 deletions polars_db_process/models/df_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
MODULE = __name__[12 : __name__.index(".", 13)]

HELP = """Supported files: .xlsx and .sql
Sql files may contains a comment on first line
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}
Sql files may contains a comment on first line captured by File Parameters field
to be mapped automatically with related objects, i.e:\n
-- {'model': 'product.product', 'db_conf': mydb, 'code': 'my_delivery_address'}
"""

PARAMS = """{'model': False, 'code': False, 'db_conf': False}
Expand Down Expand Up @@ -40,73 +39,39 @@ def _reset_process(self):
mapp._remove_uidstring_related_records()
return res

# def tmp(self, file, vals=None):
# def guess_model_and_db():
# domain=[]
# if meta.get("model"):
# domain.append(("model_id.name", '=', meta.get("model")))
# if meta.get("code"):
# domain.append(("code", '=', meta.get("code")))
# if domain:
# res = self.env["model.map"].search(domain)
# vals["model_map_id"] = res and res[0].id
# vals["db_conf_id"] = db_confs.get(meta.get("db_conf"))
# guess_model_and_db()

def _file_hook(self, file):
def _file_hook(self, file, db_confs, model_map):
"Map sql file with the right Odoo model via model_map and the right db.config"
vals = super()._file_hook(file)
vals = super()._file_hook(file, db_confs, model_map)
if ".sql" in file:
# TODO: improve
# db_confs = {x.name: x.id for x in self.env["db.config"].search([])}
content = self._get_file(file).decode("utf-8")
contents = content.split("\n")
if contents:
vals["query"] = content
# TODO: improve
# we only detect first line
meta = safe_eval(contents[0].replace("--", ""))
if meta:
vals["params"] = meta
model_name = meta.get("model")
model = self.env["ir.model"].search([("model", "=", model_name)])
if model_name:
# 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("model_map_id")
)
model_map = self.env["model.map"].search(
[
("id", "not in", model_maps.ids),
("model_id", "=", model_name),
]
)
if model_map:
# TODO use first
vals["model_map_id"] = model_map[0].id
db_config = self.env["db.config"].search(
[("name", "ilike", meta.get("db_conf_id"))]
)
vals["db_conf_id"] = db_config and db_config[0].id or False
else:
df = self.env["model.map"].create(
{"code": model.name, "model_id": model and model[0].id}
)
vals["model_map_id"] = df.id
vals["query"] = content
if meta.get("code"):
vals["model_map_id"] = model_map["code"].get(meta["code"])
elif meta.get("model"):
vals["model_map_id"] = model_map["model"].get(meta["model"])
if meta.get("db_conf"):
vals["db_conf_id"] = db_confs.get(meta["db_conf"]) or False
return vals

def _populate(self):
chinook = self.env.ref(f"{MODULE}.sqlite_chinook")
if chinook:
# TODO fix
# Demo behavior only
path = Path(get_module_path(MODULE)) / "data/chinook.sqlite"
chinook.string_connexion = f"sqlite://{str(path)}"
return super()._populate()

def _get_test_file_paths(self):
res = super()._get_test_file_paths()
def _get_modules_w_df_files(self):
res = super()._get_modules_w_df_files()
res.update(
{
"polars_db_process": {
Expand Down
4 changes: 2 additions & 2 deletions polars_db_schema/models/db_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
class DfSource(models.Model):
_inherit = "df.source"

# def _get_test_file_paths(self):
# res = super()._get_test_file_paths()
# def _get_modules_w_df_files(self):
# res = super()._get_modules_w_df_files()
# res.update(
# {
# "polars_db_schema": {
Expand Down
48 changes: 35 additions & 13 deletions polars_process/models/df_source.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import base64
from collections import defaultdict
from pathlib import Path

from odoo import fields, models
from odoo import _, exceptions, fields, models
from odoo.modules.module import get_module_path

DF_SOURCE_DIR = "data/df"
# You can store files .xlxs and .sql in this relative path of your module
DF_RELATIVE_SOURCE_DIR = "data/df"


class DfSource(models.Model):
Expand Down Expand Up @@ -50,26 +52,46 @@ def _reset_process(self):
self.state = "draft"

def _populate(self):
def create_attach(myfile, addon, idstring):
existing_files = {x.name: x.id for x in self.env["df.source"].search([])}

def get_model_map():
model_map = defaultdict(dict)
for elm in self.env["model.map"].search([]):
model_map["model"].update({elm.model_id.model: elm.id})
if elm.code:
model_map["code"].update({elm.code: elm.id})

def upsert(vals):
if vals["name"] in existing_files:
self.env[self._name].browse(existing_files[vals["name"]]).write(vals)
else:
vals.update({"rename": True})
self.env[self._name].sudo().create(vals)

def update_file_vals(myfile, addon, idstring):
with open(myfile, "rb") as f:
name = f.name[f.name.find(addon) :]
vals = {
"model_map_id": self.env.ref(idstring).id,
"name": name,
"readonly": True,
"rename": True,
}
vals.update(self._file_hook(name))
self.env[self._name].sudo().create(vals)
vals.update(self._file_hook(name, db_confs, model_map))
upsert(vals)

self.env[self._name].search([("template", "=", False)]).unlink()
paths = self._get_test_file_paths()
paths = self._get_modules_w_df_files()
db_confs = {x.name: x.id for x in self.env["db.config"].search([])}
model_map = get_model_map()
for addon, data in paths.items():
if "xmlid" not in data:
raise exceptions.ValidationError(
_("Missing xmlid key in _get_modules_w_df_files()")
)
idstring = data["xmlid"]
if self.env.ref(idstring):
mpath = Path(get_module_path(addon)) / DF_SOURCE_DIR
mpath = Path(get_module_path(addon)) / DF_RELATIVE_SOURCE_DIR
for mfile in tuple(mpath.iterdir()):
create_attach(mfile, addon, idstring)
update_file_vals(mfile, addon, idstring)
action = self.env.ref("polars_process.df_source_action")._get_action_dict()
return action

Expand All @@ -80,11 +102,11 @@ def _get_file(self, name=None):
name = self.name or name
module = name[: name.find("/")]
path = Path(get_module_path(module))
path = path / DF_SOURCE_DIR / name[name.rfind("/") + 1 :]
path = path / DF_RELATIVE_SOURCE_DIR / name[name.rfind("/") + 1 :]
with open(path, "rb") as f:
return f.read()

def _get_test_file_paths(self):
def _get_modules_w_df_files(self):
"""
You may override if you want populate files in your module
returns:
Expand All @@ -99,6 +121,6 @@ def _get_test_file_paths(self):
}
}

def _file_hook(self, file):
def _file_hook(self, file, db_confs, model_map):
"Overide me in your own module"
return {}

0 comments on commit ce86d44

Please sign in to comment.