Skip to content

Commit

Permalink
Add compat flag Model._browse_compat
Browse files Browse the repository at this point in the history
  • Loading branch information
florentx committed Dec 5, 2018
1 parent f45325e commit c60f35b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
4 changes: 3 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ Changelog
As an alternative, you can specify the ``protocol`` in the configuration
file.

* Change the return value of ``Model.browse()`` method if search domain is
* Change the return value of :meth:`Model.browse` method if search domain is
an empty list. It returns an empty ``RecordList`` except if some other
argument is provided (e.g.
``all_users = model('res.users').browse([], limit=None)``).
Compatibility tip: you can restore the old behavior with
``Model._browse_compat = True``.

* Change the return value of ``Client.read()`` and ``Model.read()`` methods
if search domain is an empty list: it returns ``False``.
Expand Down
6 changes: 6 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,12 @@ the same :class:`Model`.
.. method:: browse(domain, context=None)
.. automethod:: browse(domain, offset=0, limit=None, order=None, context=None)

.. note::

To enable the unsafe behavior (ERPpeek <= 1.7) of ``model.browse([])`` (i.e.
return all records), this class attribute can be set:
``Model._browse_compat = True``.

.. automethod:: get(domain, context=None)

.. automethod:: create
Expand Down
9 changes: 8 additions & 1 deletion erppeek.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,9 @@ def start_odoo_services(options=None, appname=None):
Import the ``odoo`` Python package and load the Odoo services.
The argument `options` receives the command line arguments
for ``odoo``. Example:
``['-c', '/path/to/odoo-server.conf', '--without-demo', 'all']``.
Return the ``odoo`` package.
"""
try:
Expand Down Expand Up @@ -1113,6 +1115,10 @@ def __exit__(self, exc_type, exc_value, tb):
class Model(object):
"""The class for Odoo models."""

# Enable Model.browse([]) to return all records.
# It was the default behavior before version 1.7.1
_browse_compat = False

def __new__(cls, client, name):
return client.model(name)

Expand Down Expand Up @@ -1181,7 +1187,8 @@ def browse(self, domain, *params, **kwargs):
if isinstance(domain, int_types):
assert not params and not kwargs
return Record(self, domain, context=context)
safe = domain or params or set(kwargs) & {'limit', 'offset', 'order'}
safe = (domain or params or self._browse_compat or
set(kwargs) & {'limit', 'offset', 'order'})
if safe and issearchdomain(domain):
kwargs['context'] = context
domain = self._execute('search', domain, *params, **kwargs)
Expand Down
16 changes: 15 additions & 1 deletion tests/test_model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from mock import sentinel, ANY
from mock import patch, sentinel, ANY

import erppeek
from ._common import XmlRpcTestCase, OBJ, callable
Expand Down Expand Up @@ -358,8 +358,20 @@ def test_browse(self):
self.assertOutput('')

def test_browse_empty(self):
OBJ = self.get_OBJ()
FooBar = self.model('foo.bar')

with patch('erppeek.Model._browse_compat', True):
records = FooBar.browse([])
self.assertIsInstance(records, erppeek.RecordList)
self.assertTrue(records)

records = FooBar.browse([], context={'lang': 'fr_CA'})
self.assertIsInstance(records, erppeek.RecordList)
self.assertTrue(records)

self.assertFalse(erppeek.Model._browse_compat)

records = FooBar.browse([])
self.assertIsInstance(records, erppeek.RecordList)
self.assertFalse(records)
Expand All @@ -377,6 +389,8 @@ def test_browse_empty(self):
self.assertTrue(records)

self.assertCalls(
OBJ('foo.bar', 'search', []),
OBJ('foo.bar', 'search', [], 0, None, None, False, {'lang': 'fr_CA'}),
OBJ('foo.bar', 'search', [], 0, 12, None),
OBJ('foo.bar', 'search', []),
)
Expand Down

0 comments on commit c60f35b

Please sign in to comment.