Skip to content
Snippets Groups Projects
Commit 407f1f6c authored by Adrian Torres's avatar Adrian Torres
Browse files

[REM] api: remove helpers one and aggregate


`api.one` has been deprecated since v9 because it often makes the code
less clear and behaves in ways developers and readers may not expect
since functions decorated with it usually expect a `list` of record(s)
instead of the recordset, whereas most modern Odoo code expects `self`
to be a recordset.

The function `aggregate` was solely being used by `api.one` thus it has
also been removed since there doesn't seem to be any other use for it
thus far.

closes odoo/odoo#34555

Signed-off-by: default avatarRaphael Collet (rco) <rco@openerp.com>
parent 9e71d57d
No related branches found
No related tags found
No related merge requests found
......@@ -34,7 +34,7 @@
__all__ = [
'Environment',
'Meta', 'guess', 'noguess',
'model', 'multi', 'one',
'model', 'multi',
'model_cr', 'model_cr_context',
'cr', 'cr_context',
'cr_uid', 'cr_uid_context',
......@@ -275,19 +275,6 @@ def downgrade(method, value, self, args, kwargs):
return value.ids
def aggregate(method, value, self):
""" Aggregate record-style ``value`` for a method decorated with ``@one``. """
spec = getattr(method, '_returns', None)
if spec:
# value is a list of instances, concatenate them
model, _, _ = spec
if model == 'self':
return sum(value, self.browse())
elif model:
return sum(value, self.env[model])
return value
def split_context(method, args, kwargs):
""" Extract the context from a pair of positional and keyword arguments.
Return a triple ``context, args, kwargs``.
......@@ -337,42 +324,6 @@ def multi(method):
return method
def one(method):
""" Decorate a record-style method where ``self`` is expected to be a
singleton instance. The decorated method automatically loops on records,
and makes a list with the results. In case the method is decorated with
:func:`returns`, it concatenates the resulting instances. Such a
method::
@api.one
def method(self, args):
return self.name
may be called in both record and traditional styles, like::
# recs = model.browse(cr, uid, ids, context)
names = recs.method(args)
names = model.method(cr, uid, ids, args, context=context)
.. deprecated:: 9.0
:func:`~.one` often makes the code less clear and behaves in ways
developers and readers may not expect.
It is strongly recommended to use :func:`~.multi` and either
iterate on the ``self`` recordset or ensure that the recordset
is a single record with :meth:`~odoo.models.Model.ensure_one`.
"""
def loop(method, self, *args, **kwargs):
result = [method(rec, *args, **kwargs) for rec in self]
return aggregate(method, result, self)
wrapper = decorator(loop, method)
wrapper._api = 'one'
return wrapper
def model_cr(method):
""" Decorate a record-style method where ``self`` is a recordset, but its
contents is not relevant, only the model is. Such a method::
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment