Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
bealdav committed Oct 28, 2024
1 parent 5e38a87 commit 4754b0a
Show file tree
Hide file tree
Showing 17 changed files with 300 additions and 21 deletions.
7 changes: 7 additions & 0 deletions polars_db_process/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ Authors

* Akretion

Contributors
------------

- Akretion

- David BEAL [email protected]

Maintainers
-----------

Expand Down
9 changes: 7 additions & 2 deletions polars_db_process/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"name": "Polars Database Process",
"version": "18.0.1.0.0",
"summary": "Allow to create a Polars dataframe from database query and "
"summary": "Allow to create a Polars dataframe from db.query and "
"check it and process it according to rules",
"category": "Reporting",
"license": "AGPL-3",
Expand All @@ -20,11 +20,16 @@
]
},
"data": [
"data/demo.xml",
"security/ir.model.access.xml",
"wizards/df_process.xml",
"views/dataframe.xml",
"views/df_field.xml",
"views/df_source.xml",
"views/db_config.xml",
"data/demo.xml",
],
"demo": [
"data/demo.xml",
],
"installable": True,
}
Binary file added polars_db_process/data/chinook.sqlite
Binary file not shown.
14 changes: 13 additions & 1 deletion polars_db_process/data/demo.xml
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
<odoo />
<odoo noupdate="1">
<record id="sqlite_chinook" model="db.config">
<field name="name">Chinook</field>
<field
name="string_connexion"
>sqlite://.../polars_db_schema/tests/files/chinook.sqlite</field>
</record>

<record id="contact_chinook" model="dataframe">
<field name="model_id" ref="base.model_res_partner" />
<field name="code">Chinook Customers</field>
</record>
</odoo>
4 changes: 4 additions & 0 deletions polars_db_process/data/files/customers.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--{"model": "res.partner"}
SELECT LastName AS name, State AS state, PostalCode AS zip, Email AS mail
-- , Company, Address, City, Country, Phone, Fax, SupportRepId
FROM customers;
3 changes: 1 addition & 2 deletions polars_db_process/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
from . import dataframe
from . import df_field
from . import df_source
from . import db_config
5 changes: 0 additions & 5 deletions polars_db_process/models/dataframe.py

This file was deleted.

44 changes: 44 additions & 0 deletions polars_db_process/models/db_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import connectorx as cx

from odoo import _, exceptions, fields, models

HELP = """
String connexion samples:
postgres://user:PASSWORD@server:port/database
mssql://user:PASSWORD@server:port/db.encrypt=true&trusted_connection=false
sqlite:///home/user/path/test.db
mysql://user:PASSWORD@server:port/database
oracle://user:PASSWORD@server:port/database
"""


class DbConfig(models.Model):
_name = "db.config"
_description = "External db.configuration"
_rec_name = "name"
_order = "name"
_rec_names_search = ["name"]

name = fields.Char(required=True)
string_connexion = fields.Char(required=True, help=HELP)
password = fields.Char(help="Not required for Sqlite")

def _get_connexion(self):
return self.string_connexion.replace("PASSWORD", self.password or "")

def test_connexion(self):
res = self._read_sql(self._get_connexion(), "SELECT 1")
if len(res):
# Not invalid in reality
raise exceptions.ValidationError(_("Connexion OK !"))

def _read_sql(self, connexion, query):
try:
return cx.read_sql(connexion, query, return_type="polars")
except RuntimeError as err:
raise exceptions.ValidationError(err) from err
except TimeoutError as err:
raise exceptions.ValidationError(err) from err
except Exception as err:
raise exceptions.ValidationError(err) from err
5 changes: 0 additions & 5 deletions polars_db_process/models/df_field.py

This file was deleted.

81 changes: 80 additions & 1 deletion polars_db_process/models/df_source.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,84 @@
from odoo import models
from pathlib import Path

from odoo import fields, models
from odoo.modules.module import get_module_path
from odoo.tools.safe_eval import safe_eval

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 dataframe, i.e:\n
-- {'model_id': 'product.product', 'db_id': mydb}
-- {'code': 'my_delivery_address', 'db_id': mydb}
"""


class DfSource(models.Model):
_inherit = "df.source"

name = fields.Char(help=HELP)
query = fields.Char()
# TODO : -> db_config_id
db_id = fields.Many2one(comodel_name="db.config", help="Database")

def _file_hook(self, file):
"Map sql file with the right Odoo model via dataframe and the right db.config"
vals = super()._file_hook(file)
if ".sql" in file:
# TODO: improve
content = self._get_file(file).decode("utf-8")
contents = content.split("\n")
if contents:
# we only detect first line
metadata = safe_eval(contents[0].replace("--", ""))
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 = (
self.env["df.source"]
.search([])
.filtered(lambda s: not s.db_id)
.mapped("dataframe_id")
)
dataframe = self.env["dataframe"].search(
[
("id", "not in", dataframes.ids),
("model_id", "=", model_name),
]
)
if dataframe:
# TODO use first
vals["dataframe_id"] = dataframe[0].id
db_config = self.env["db.config"].search(
[("name", "ilike", metadata.get("db_id"))]
)
vals["db_id"] = db_config and db_config[0].id or False
else:
df = self.env["dataframe"].create(
{"code": model.name, "model_id": model and model[0].id}
)
vals["dataframe_id"] = df.id
vals["query"] = content
return vals

def _populate(self):
chinook = self.env.ref(f"{MODULE}.sqlite_chinook")
if chinook:
# 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()
res.update(
{
"polars_db_process": {
"relative_path": "data/files",
"xmlid": "polars_db_process.contact_chinook",
}
}
)
return res
3 changes: 3 additions & 0 deletions polars_db_process/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- Akretion

- David BEAL <[email protected]>
11 changes: 11 additions & 0 deletions polars_db_process/security/ir.model.access.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<odoo>
<record id="db_config_all" model="ir.model.access">
<field name="name">Database Config</field>
<field name="model_id" ref="model_db_config" />
<field name="group_id" ref="base.group_user" />
<field name="perm_read" eval="1" />
<field name="perm_create" eval="1" />
<field name="perm_write" eval="1" />
<field name="perm_unlink" eval="1" />
</record>
</odoo>
14 changes: 12 additions & 2 deletions polars_db_process/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,8 @@ <h1 class="title">Polars Database Process</h1>
<li><a class="reference internal" href="#bug-tracker" id="toc-entry-1">Bug Tracker</a></li>
<li><a class="reference internal" href="#credits" id="toc-entry-2">Credits</a><ul>
<li><a class="reference internal" href="#authors" id="toc-entry-3">Authors</a></li>
<li><a class="reference internal" href="#maintainers" id="toc-entry-4">Maintainers</a></li>
<li><a class="reference internal" href="#contributors" id="toc-entry-4">Contributors</a></li>
<li><a class="reference internal" href="#maintainers" id="toc-entry-5">Maintainers</a></li>
</ul>
</li>
</ul>
Expand All @@ -412,8 +413,17 @@ <h2><a class="toc-backref" href="#toc-entry-3">Authors</a></h2>
<li>Akretion</li>
</ul>
</div>
<div class="section" id="contributors">
<h2><a class="toc-backref" href="#toc-entry-4">Contributors</a></h2>
<ul class="simple">
<li>Akretion<ul>
<li>David BEAL <a class="reference external" href="mailto:david.beal&#64;akretion.com">david.beal&#64;akretion.com</a></li>
</ul>
</li>
</ul>
</div>
<div class="section" id="maintainers">
<h2><a class="toc-backref" href="#toc-entry-4">Maintainers</a></h2>
<h2><a class="toc-backref" href="#toc-entry-5">Maintainers</a></h2>
<p>This module is maintained by the OCA.</p>
<a class="reference external image-reference" href="https://odoo-community.org">
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
Expand Down
59 changes: 59 additions & 0 deletions polars_db_process/views/db_config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<odoo>
<record id="db_config_form" model="ir.ui.view">
<field name="model">db.config</field>
<field name="arch" type="xml">
<form>
<header>
<button
type="object"
name="test_connexion"
string="Test Connexion"
/>
</header>
<sheet>
<group>
<group>
<field name="name" />
</group>
<group>
<field name="password" password="1" />
</group>
</group>
<group name="connexion">
<field
name="string_connexion"
placeholder="postgres://username:PASSWORD@server:port/database"
/>
<div>
<b
>PASSWORD in string connexion'll be replaced by password field</b>
</div>
</group>
</sheet>
</form>
</field>
</record>

<record id="dataframe_list" model="ir.ui.view">
<field name="model">db.config</field>
<field name="arch" type="xml">
<list>
<field name="name" />
</list>
</field>
</record>

<record id="db_config_action" model="ir.actions.act_window">
<field name="name">Database Config</field>
<field name="res_model">db.config</field>
<field name="view_mode">list,form</field>
<field name="path">db-config</field>
</record>

<menuitem
id="db_config_menu"
action="db_config_action"
parent="polars_process.polars_menu"
sequence="7"
/>
</odoo>
49 changes: 48 additions & 1 deletion polars_db_process/views/df_source.xml
Original file line number Diff line number Diff line change
@@ -1 +1,48 @@
<odoo />
<odoo>
<record id="df_source_form" model="ir.ui.view">
<field name="model">df.source</field>
<field name="inherit_id" ref="polars_process.df_source_form" />
<field name="arch" type="xml">
<xpath expr="//group[1]" position="after">
<separator />
<group>
<field name="db_id" invisible="template" />
<field name="query" widget="code" invisible="template" />
</group>
</xpath>
</field>
</record>

<record id="df_source_list" model="ir.ui.view">
<field name="model">df.source</field>
<field name="inherit_id" ref="polars_process.df_source_list" />
<field name="arch" type="xml">
<xpath expr="//list/field[last()]" position="after">
<field name="db_id" />
</xpath>
<xpath expr="//list/field[@name='template']" position="attributes">
<attribute
name="options"
>{'accepted_file_extensions': '.xlsx,.sql'}</attribute>
</xpath>
</field>
</record>

<record id="df_source_search" model="ir.ui.view">
<field name="model">df.source</field>
<field name="inherit_id" ref="polars_process.df_source_search" />
<field name="arch" type="xml">
<xpath expr="//field[@name='name']" position="after">
<field name="db_id" />
</xpath>
<xpath expr="//group" position="inside">
<filter
string="Database"
name="db_id"
domain="[]"
context="{'group_by': 'db_id'}"
/>
</xpath>
</field>
</record>
</odoo>
11 changes: 10 additions & 1 deletion polars_db_process/wizards/df_process.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
from odoo import models
from odoo import _, exceptions, models

MODULE = __name__[12 : __name__.index(".", 13)]


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

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

def _pre_process_sql(self):
raise exceptions.ValidationError(_("to be continued"))
2 changes: 1 addition & 1 deletion test-requirement.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
odoo-addon-polars_process @ git+https://github.com/OCA/reporting-engine.git@refs/pull/943/head#subdirectory=polars_process
odoo-addon-polars_process @ git+https://github.com/OCA/reporting-engine.git@refs/pull/943/head#subdirectory=polars_process

0 comments on commit 4754b0a

Please sign in to comment.