Skip to content

Commit

Permalink
Refactor: load arg in read() (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
kmagusiak authored Mar 6, 2023
1 parent c5011a1 commit 35e19b6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
4 changes: 2 additions & 2 deletions odoo_connect/explore.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def cache(self, fields: List[str] = [], computed=False, exists=False) -> "Instan
# an exists() check is not needed because read() will return only existing rows
if not missing_ids:
return self
for d in self.__model.read(list(missing_ids), list(fieldset), load='raw'):
for d in self.__model._read(list(missing_ids), list(fieldset)):
id = d['id']
if id in model_cache:
model_cache[id].update(d)
Expand Down Expand Up @@ -195,7 +195,7 @@ def exists(self) -> "Instance":
def search(self, domain: List, **kw) -> "Instance":
"""Search for an instance"""
fields = self._default_fields()
data = self.__model.search_read(domain, fields, load='raw', **kw)
data = self.__model._search_read(domain, fields, **kw)
# add only new data, keep cache consistent
model_cache = self.__cache()
model_cache.update({d['id']: d for d in data if d['id'] not in model_cache})
Expand Down
27 changes: 24 additions & 3 deletions odoo_connect/odoo_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ def __read_dict_recursive(self, data, fields):

# read the data from children
model = self.odoo.get_model(model_name)
children_data = model.read(list(ids), list(child_fields), load='raw')
children_data = model._read(list(ids), list(child_fields))
model.__read_dict_recursive(children_data, child_fields)
children_data = {e['id']: e for e in children_data}

Expand All @@ -482,6 +482,27 @@ def __read_dict_recursive(self, data, fields):

return data

def _read(self, ids: List[int], fields: List[str], **kwargs):
"""Raw read() function"""
return self.read(ids, fields, load='raw', **kwargs)

def _search_read(self, domain: List, fields: List[str], **kwargs):
"""Raw search_read() function"""
if self.odoo.major_version >= 15:
return self.search_read(domain, fields, load='raw', **kwargs)
# before v15, load argument is not supported
data = self.search_read(domain, fields, **kwargs)
for d in data:
for k, v in d.items():
if (
isinstance(v, list)
and len(v) == 2
and isinstance(v[0], int)
and isinstance(v[1], str)
):
d[k] = v[0]
return data

def read_dict(
self,
ids: Union[List[int], int],
Expand All @@ -502,7 +523,7 @@ def read_dict(
ids = [ids]
single = True
fields = self.__prepare_dict_fields(fields)
data = self.read(ids, list(fields))
data = self._read(ids, list(fields))
result = self.__read_dict_recursive(data, fields)
return result[0] if single else result

Expand All @@ -518,7 +539,7 @@ def search_read_dict(self, domain: List, fields: Union[List[str], Dict[str, Dict
:return: A list of found objects
"""
fields = self.__prepare_dict_fields(fields)
data = self.search_read(domain, list(fields), load='raw', **kwargs)
data = self._search_read(domain, list(fields), **kwargs)
return self.__read_dict_recursive(data, fields)

def read_group_dict(
Expand Down
10 changes: 8 additions & 2 deletions pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@
set -eu
# Check directory
cd "$(dirname "$0")"
[ -d .git ] || cd ../..
[ -d .git ]
[[ "$0" != *.git/hooks/* ]] || cd ../..

pre_commit() {
flake8
black --check .
isort --check-only .
}

format() {
black .
isort .
}

# Commands
case "${1:-run}" in
run|lint)
pre_commit
echo "All good to commit"
;;
format)
format;;
install)
echo "Installing pre-commit"
cd .git/hooks
Expand Down
7 changes: 6 additions & 1 deletion tests/mock_odoo_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,12 @@ def default_rpc_handler():
@h.patch_generic
def version(service, method, args):
if service == 'common' and method == 'version':
return {"server_version": "15.0", "server_serie": "mocked"}
return {
"server_version": "16.0+e",
"server_version_info": [16, 0, 0, 'mock', 0, 'e'],
"server_serie": "16.0",
"protocol_version": "1",
}

@h.patch_generic
def authenticate(service, method, args):
Expand Down

0 comments on commit 35e19b6

Please sign in to comment.