Skip to content

Commit

Permalink
ADD base_tool module
Browse files Browse the repository at this point in the history
  • Loading branch information
bealdav committed Oct 9, 2023
1 parent 6d5e29b commit d3c5043
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions base_tool/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
auto
1 change: 1 addition & 0 deletions base_tool/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
14 changes: 14 additions & 0 deletions base_tool/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "Base Tools",
"summary": "Utilities that can be used in projects",
"version": "16.0.1.0.0",
"category": "Tools",
"website": "https://github.com/akretion/ak-odoo-incubator",
"author": " Akretion",
"license": "AGPL-3",
"depends": [
"product",
],
"data": [],
"installable": True,
}
1 change: 1 addition & 0 deletions base_tool/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import base
41 changes: 41 additions & 0 deletions base_tool/models/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import logging

from odoo import models

logger = logging.getLogger(__name__)


class Base(models.AbstractModel):
_inherit = "base"

def _unknown_data_in_field(self, model, field, data_list, company_id=None):
"""Compare a data list with matching db data as specified model / field
Example:
model is "product.product"
field is "default_code"
data_list is ["FURN_7777", "ANY", "E-COM07", "E-COM08", "MISS-TEAK"]
return missing data is ["ANY", "MISS-TEAK"]
"""

def unique(my_list):
return sorted(set(my_list))

domain = [(field, "in", data_list)]
if company_id:
domain += [("company_id", "=", company_id)]
data_list = unique(data_list)
known_data = [x[field] for x in self.env[model].search(domain)]

known_data = unique(known_data)
unknown_data = [x for x in data_list if x not in known_data]
if unknown_data:
logger.info(
f" >>> These data are not in this field {field} "
f"in model {model} in db '{self.env.cr.dbname}'."
)
else:
logger.info(
f"Great! all provided data are present db {self.env.cr.dbname}."
)
return unknown_data
1 change: 1 addition & 0 deletions base_tool/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This contains misc utilities tool scripts that can be used in your projects.
1 change: 1 addition & 0 deletions base_tool/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_module
34 changes: 34 additions & 0 deletions base_tool/tests/test_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from odoo.tests.common import TransactionCase


class Test(TransactionCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True))
cls.user_model = cls.env["res.users"].with_context(no_reset_password=True)

def test_no_missing_data_in_default_code(self):
data_list = ["E-COM07", "FURN_7777", "FURN_6666"]
unknown_data = self.unknown_data_in_default_code(data_list)
assert not unknown_data

def test_missing_data_in_default_code(self):
data_list = ["FURN_7777", "ANY", "E-COM07", "E-COM08", "MISS-TEAK"]
unknown_data = self.unknown_data_in_default_code(data_list)
assert unknown_data == ["ANY", "MISS-TEAK"]

def test_missing_data_in_default_code_with_company(self):
data_list = ["FURN_7777", "MISS-TEAK"]
self.env["product.product"].search(
[("default_code", "=", "FURN_7777")]
).company_id = 1
unknown_data = self.env["base"]._unknown_data_in_field(
"product.product", "default_code", data_list, company_id=1
)
assert unknown_data == ["MISS-TEAK"]

def unknown_data_in_default_code(self, data_list):
return self.env["base"]._unknown_data_in_field(
"product.product", "default_code", data_list
)
1 change: 1 addition & 0 deletions setup/base_tool/odoo/addons/base_tool
6 changes: 6 additions & 0 deletions setup/base_tool/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)

0 comments on commit d3c5043

Please sign in to comment.