diff options
| author | Adrian Torres <adt@odoo.com> | 2019-09-23 11:52:06 +0000 |
|---|---|---|
| committer | Adrian Torres <adt@odoo.com> | 2020-03-23 15:14:10 +0000 |
| commit | 9befa3d245f058f7b687509bec88160986b2b877 (patch) | |
| tree | a4b60ecfd5950c9688f09d31dc5243c825cbc302 | |
| parent | 0d16e7331cc859a6f060eeed8ea2b53801c08ca6 (diff) | |
[FIX] *: adapt code for resolve_2many_commands removal
This code adapts all business code instances of calls to
`resolve_2many_commands` and replaces them by calls to `new` which
returns a record-like object whose api is more familiar than the
`resolve_2many_commands` api.
| -rw-r--r-- | addons/account/models/account_move.py | 9 | ||||
| -rw-r--r-- | addons/payment/models/payment_acquirer.py | 4 | ||||
| -rw-r--r-- | addons/sale/models/payment.py | 4 | ||||
| -rw-r--r-- | addons/stock/models/stock_warehouse.py | 5 | ||||
| -rw-r--r-- | addons/website_forum/models/forum.py | 2 | ||||
| -rw-r--r-- | odoo/addons/base/tests/test_orm.py | 138 |
6 files changed, 12 insertions, 150 deletions
diff --git a/addons/account/models/account_move.py b/addons/account/models/account_move.py index 2b7c8352f3e..bde55d46f4c 100644 --- a/addons/account/models/account_move.py +++ b/addons/account/models/account_move.py @@ -3253,8 +3253,8 @@ class AccountMoveLine(models.Model): if move.is_invoice(include_receipts=True): currency = move.currency_id partner = self.env['res.partner'].browse(vals.get('partner_id')) - taxes = self.resolve_2many_commands('tax_ids', vals.get('tax_ids', []), fields=['id']) - tax_ids = set(tax['id'] for tax in taxes) + taxes = self.new({'tax_ids': vals.get('tax_ids', [])}).tax_ids + tax_ids = set(taxes.ids) taxes = self.env['account.tax'].browse(tax_ids) # Ensure consistency between accounting & business fields. @@ -3322,8 +3322,7 @@ class AccountMoveLine(models.Model): tax = repartition_line.invoice_tax_id or repartition_line.refund_tax_id vals['tax_exigible'] = tax.tax_exigibility == 'on_invoice' elif vals.get('tax_ids'): - tax_ids = [v['id'] for v in self.resolve_2many_commands('tax_ids', vals['tax_ids'], fields=['id'])] - taxes = self.env['account.tax'].browse(tax_ids).flatten_taxes_hierarchy() + taxes = self.new({'tax_ids': vals['tax_ids']}).tax_ids.flatten_taxes_hierarchy() vals['tax_exigible'] = not any(tax.tax_exigibility == 'on_payment' for tax in taxes) lines = super(AccountMoveLine, self).create(vals_list) @@ -3346,7 +3345,7 @@ class AccountMoveLine(models.Model): return line[field_name].id != vals[field_name] if field.type in ('one2many', 'many2many'): current_ids = set(line[field_name].ids) - after_write_ids = set(r['id'] for r in line.resolve_2many_commands(field_name, vals[field_name], fields=['id'])) + after_write_ids = set(line.new({field_name: vals[field_name]})[field_name].ids) return current_ids != after_write_ids if field.type == 'monetary' and line[field.currency_field]: return not line[field.currency_field].is_zero(line[field_name] - vals[field_name]) diff --git a/addons/payment/models/payment_acquirer.py b/addons/payment/models/payment_acquirer.py index 6453965ff5f..afe2a1fe65b 100644 --- a/addons/payment/models/payment_acquirer.py +++ b/addons/payment/models/payment_acquirer.py @@ -865,8 +865,8 @@ class PaymentTransaction(models.Model): @api.model def _compute_reference_prefix(self, values): if values and values.get('invoice_ids'): - many_list = self.resolve_2many_commands('invoice_ids', values['invoice_ids'], fields=['name']) - return ','.join(dic['name'] for dic in many_list) + invoices = self.new({'invoice_ids': values['invoice_ids']}).invoice_ids + return ','.join(invoices.mapped('name')) return None @api.model diff --git a/addons/sale/models/payment.py b/addons/sale/models/payment.py index 59689215508..9000db0caea 100644 --- a/addons/sale/models/payment.py +++ b/addons/sale/models/payment.py @@ -138,8 +138,8 @@ class PaymentTransaction(models.Model): def _compute_reference_prefix(self, values): prefix = super(PaymentTransaction, self)._compute_reference_prefix(values) if not prefix and values and values.get('sale_order_ids'): - many_list = self.resolve_2many_commands('sale_order_ids', values['sale_order_ids'], fields=['name']) - return ','.join(dic['name'] for dic in many_list) + sale_orders = self.new({'sale_order_ids': values['sale_order_ids']}).sale_order_ids + return ','.join(sale_orders.mapped('name')) return prefix def action_view_sales_orders(self): diff --git a/addons/stock/models/stock_warehouse.py b/addons/stock/models/stock_warehouse.py index 39e5d9e34ee..930cbe05ff3 100644 --- a/addons/stock/models/stock_warehouse.py +++ b/addons/stock/models/stock_warehouse.py @@ -146,8 +146,9 @@ class Warehouse(models.Model): warehouses._update_reception_delivery_resupply(vals.get('reception_steps'), vals.get('delivery_steps')) if vals.get('resupply_wh_ids') and not vals.get('resupply_route_ids'): - resupply_whs = self.resolve_2many_commands('resupply_wh_ids', vals['resupply_wh_ids']) - new_resupply_whs = self.browse([wh['id'] for wh in resupply_whs]) + new_resupply_whs = self.new({ + 'resupply_wh_ids': vals['resupply_wh_ids'] + }).resupply_wh_ids._origin old_resupply_whs = {warehouse.id: warehouse.resupply_wh_ids for warehouse in warehouses} # If another partner assigned diff --git a/addons/website_forum/models/forum.py b/addons/website_forum/models/forum.py index 7f862506369..5aba126395d 100644 --- a/addons/website_forum/models/forum.py +++ b/addons/website_forum/models/forum.py @@ -484,7 +484,7 @@ class Post(models.Model): tag_ids = False if 'tag_ids' in vals: - tag_ids = set(tag.get('id') for tag in self.resolve_2many_commands('tag_ids', vals['tag_ids'])) + tag_ids = set(self.new({'tag_ids': vals['tag_ids']}).tag_ids.ids) for post in self: if 'state' in vals: diff --git a/odoo/addons/base/tests/test_orm.py b/odoo/addons/base/tests/test_orm.py index 0cc2d4a1524..08ddd02b218 100644 --- a/odoo/addons/base/tests/test_orm.py +++ b/odoo/addons/base/tests/test_orm.py @@ -380,141 +380,3 @@ class TestInherits(TransactionCase): user.write({'image_1920': 'R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='}) write_date_after = user.write_date self.assertNotEqual(write_date_before, write_date_after) - - -CREATE = lambda values: (0, False, values) -UPDATE = lambda id, values: (1, id, values) -DELETE = lambda id: (2, id, False) -FORGET = lambda id: (3, id, False) -LINK_TO = lambda id: (4, id, False) -DELETE_ALL = lambda: (5, False, False) -REPLACE_WITH = lambda ids: (6, False, ids) - - -class TestO2MSerialization(TransactionCase): - """ test the orm method 'write' on one2many fields """ - - def setUp(self): - super(TestO2MSerialization, self).setUp() - self.partner = self.registry('res.partner') - - def test_no_command(self): - " empty list of commands yields an empty list of records " - results = self.env['res.partner'].resolve_2many_commands('child_ids', []) - self.assertEqual(results, []) - - def test_CREATE_commands(self): - " returns the VALUES dict as-is " - values = [{'foo': 'bar'}, {'foo': 'baz'}, {'foo': 'baq'}] - results = self.env['res.partner'].resolve_2many_commands('child_ids', [CREATE(v) for v in values]) - self.assertEqual(results, values) - - def test_LINK_TO_command(self): - " reads the records from the database, records are returned with their ids. " - ids = [ - self.env['res.partner'].create({'name': 'foo'}).id, - self.env['res.partner'].create({'name': 'bar'}).id, - self.env['res.partner'].create({'name': 'baz'}).id, - ] - commands = [LINK_TO(v) for v in ids] - - results = self.env['res.partner'].resolve_2many_commands('child_ids', commands, ['name']) - self.assertItemsEqual(results, [ - {'id': ids[0], 'name': 'foo'}, - {'id': ids[1], 'name': 'bar'}, - {'id': ids[2], 'name': 'baz'}, - ]) - - def test_bare_ids_command(self): - " same as the equivalent LINK_TO commands " - ids = [ - self.env['res.partner'].create({'name': 'foo'}).id, - self.env['res.partner'].create({'name': 'bar'}).id, - self.env['res.partner'].create({'name': 'baz'}).id, - ] - - results = self.env['res.partner'].resolve_2many_commands('child_ids', ids, ['name']) - self.assertItemsEqual(results, [ - {'id': ids[0], 'name': 'foo'}, - {'id': ids[1], 'name': 'bar'}, - {'id': ids[2], 'name': 'baz'}, - ]) - - def test_UPDATE_command(self): - " take the in-db records and merge the provided information in " - foo = self.env['res.partner'].create({'name': 'foo'}) - bar = self.env['res.partner'].create({'name': 'bar'}) - baz = self.env['res.partner'].create({'name': 'baz', 'city': 'tag'}) - commands = [ - LINK_TO(foo.id), - UPDATE(bar.id, {'name': 'qux', 'city': 'tagtag'}), - UPDATE(baz.id, {'name': 'quux'}), - ] - - results = self.env['res.partner'].resolve_2many_commands('child_ids', commands, ['name', 'city']) - self.assertItemsEqual(results, [ - {'id': foo.id, 'name': 'foo', 'city': False}, - {'id': bar.id, 'name': 'qux', 'city': 'tagtag'}, - {'id': baz.id, 'name': 'quux', 'city': 'tag'}, - ]) - - def test_DELETE_command(self): - " deleted records are not returned at all. " - ids = [ - self.env['res.partner'].create({'name': 'foo'}).id, - self.env['res.partner'].create({'name': 'bar'}).id, - self.env['res.partner'].create({'name': 'baz'}).id, - ] - commands = [DELETE(v) for v in ids] - - results = self.env['res.partner'].resolve_2many_commands('child_ids', commands, ['name']) - self.assertEqual(results, []) - - def test_mixed_commands(self): - ids = [ - self.env['res.partner'].create({'name': name}).id - for name in ['NObar', 'baz', 'qux', 'NOquux', 'NOcorge', 'garply'] - ] - commands = [ - CREATE({'name': 'foo'}), - UPDATE(ids[0], {'name': 'bar'}), - LINK_TO(ids[1]), - DELETE(ids[2]), - UPDATE(ids[3], {'name': 'quux',}), - UPDATE(ids[4], {'name': 'corge'}), - CREATE({'name': 'grault'}), - LINK_TO(ids[5]), - ] - - results = self.env['res.partner'].resolve_2many_commands('child_ids', commands, ['name']) - self.assertItemsEqual(results, [ - {'name': 'foo'}, - {'id': ids[0], 'name': 'bar'}, - {'id': ids[1], 'name': 'baz'}, - {'id': ids[3], 'name': 'quux'}, - {'id': ids[4], 'name': 'corge'}, - {'name': 'grault'}, - {'id': ids[5], 'name': 'garply'}, - ]) - - def test_LINK_TO_pairs(self): - "LINK_TO commands can be written as pairs, instead of triplets" - ids = [ - self.env['res.partner'].create({'name': 'foo'}).id, - self.env['res.partner'].create({'name': 'bar'}).id, - self.env['res.partner'].create({'name': 'baz'}).id, - ] - commands = [(4, id) for id in ids] - - results = self.env['res.partner'].resolve_2many_commands('child_ids', commands, ['name']) - self.assertItemsEqual(results, [ - {'id': ids[0], 'name': 'foo'}, - {'id': ids[1], 'name': 'bar'}, - {'id': ids[2], 'name': 'baz'}, - ]) - - def test_singleton_commands(self): - "DELETE_ALL can appear as a singleton" - commands = [DELETE_ALL()] - results = self.env['res.partner'].resolve_2many_commands('child_ids', commands, ['name']) - self.assertEqual(results, []) |
