From 76ee0afb2944521f0608732c34626d3d89b2d505 Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Wed, 17 Apr 2019 08:46:29 +0000 Subject: [PATCH] [REF] api: remove deprecated env.in_onchange --- addons/hr_holidays/models/hr_leave.py | 2 +- .../hr_holidays/models/hr_leave_allocation.py | 2 +- .../test_new_api/tests/test_new_fields.py | 34 +++++++------- .../tests/test_performance.py | 5 -- odoo/api.py | 36 +++++---------- odoo/fields.py | 6 +-- odoo/models.py | 46 +++++++++---------- 7 files changed, 53 insertions(+), 78 deletions(-) diff --git a/addons/hr_holidays/models/hr_leave.py b/addons/hr_holidays/models/hr_leave.py index 54680fcc4f4..f52670a1c9c 100644 --- a/addons/hr_holidays/models/hr_leave.py +++ b/addons/hr_holidays/models/hr_leave.py @@ -757,7 +757,7 @@ class HolidaysRequest(models.Model): current_employee = self.env['hr.employee'].search([('user_id', '=', self.env.uid)], limit=1) is_officer = self.env.user.has_group('hr_holidays.group_hr_holidays_user') is_manager = self.env.user.has_group('hr_holidays.group_hr_holidays_manager') - if self.env.in_onchange: + if not all(self._ids): return for holiday in self: val_type = holiday.holiday_status_id.validation_type diff --git a/addons/hr_holidays/models/hr_leave_allocation.py b/addons/hr_holidays/models/hr_leave_allocation.py index 287a94224f7..c953d32860f 100644 --- a/addons/hr_holidays/models/hr_leave_allocation.py +++ b/addons/hr_holidays/models/hr_leave_allocation.py @@ -522,7 +522,7 @@ class HolidaysAllocation(models.Model): def _check_approval_update(self, state): """ Check if target state is achievable. """ current_employee = self.env['hr.employee'].search([('user_id', '=', self.env.uid)], limit=1) - if not current_employee or self.env.in_onchange: + if not current_employee or not all(self._ids): return is_officer = self.env.user.has_group('hr_holidays.group_hr_holidays_user') is_manager = self.env.user.has_group('hr_holidays.group_hr_holidays_manager') diff --git a/odoo/addons/test_new_api/tests/test_new_fields.py b/odoo/addons/test_new_api/tests/test_new_fields.py index c83704dbbe3..bb9f10c7792 100644 --- a/odoo/addons/test_new_api/tests/test_new_fields.py +++ b/odoo/addons/test_new_api/tests/test_new_fields.py @@ -1116,17 +1116,16 @@ class TestFields(common.TransactionCase): env = self.env(user=self.env.ref('base.user_demo')) self.assertEqual(env.user.login, "demo") - with self.env.do_in_onchange(): - # create a new message as demo user - discussion = self.env.ref('test_new_api.discussion_0') - message = env['test_new_api.message'].new({'discussion': discussion}) - self.assertEqual(message.discussion, discussion) + # create a new message as demo user + discussion = self.env.ref('test_new_api.discussion_0') + message = env['test_new_api.message'].new({'discussion': discussion}) + self.assertEqual(message.discussion, discussion) - # read the related field discussion_name - self.assertEqual(message.discussion.env, env) - self.assertEqual(message.discussion_name, discussion.name) - with self.assertRaises(AccessError): - message.discussion.name + # read the related field discussion_name + self.assertEqual(message.discussion.env, env) + self.assertEqual(message.discussion_name, discussion.name) + with self.assertRaises(AccessError): + message.discussion.name @mute_logger('odoo.addons.base.models.ir_model') def test_42_new_related(self): @@ -1139,15 +1138,14 @@ class TestFields(common.TransactionCase): env = self.env(user=self.env.ref('base.user_demo')) self.assertEqual(env.user.login, "demo") - with self.env.do_in_onchange(): - # create a new discussion and a new message as demo user - discussion = env['test_new_api.discussion'].new({'name': 'Stuff'}) - message = env['test_new_api.message'].new({'discussion': discussion}) - self.assertEqual(message.discussion, discussion) + # create a new discussion and a new message as demo user + discussion = env['test_new_api.discussion'].new({'name': 'Stuff'}) + message = env['test_new_api.message'].new({'discussion': discussion}) + self.assertEqual(message.discussion, discussion) - # read the related field discussion_name - self.assertNotEqual(message.sudo().env, message.env) - self.assertEqual(message.discussion_name, discussion.name) + # read the related field discussion_name + self.assertNotEqual(message.sudo().env, message.env) + self.assertEqual(message.discussion_name, discussion.name) def test_43_new_related(self): """ test the behavior of one2many related fields """ diff --git a/odoo/addons/test_performance/tests/test_performance.py b/odoo/addons/test_performance/tests/test_performance.py index c05a62cc4d9..8385b97bbca 100644 --- a/odoo/addons/test_performance/tests/test_performance.py +++ b/odoo/addons/test_performance/tests/test_performance.py @@ -369,11 +369,6 @@ class TestPerformance(TransactionCase): new_recs = records.browse(records.new(origin=record).id for record in records) new_recs.mapped('value') - records.invalidate_cache(['value']) - with self.assertQueryCount(__system__=2, demo=2): - with self.env.do_in_onchange(): - records.mapped('value') - # clean up after each pass self.env.cr.execute( 'delete from test_performance_base where id not in %s', diff --git a/odoo/api.py b/odoo/api.py index 94b02961063..97e1f6b26c9 100644 --- a/odoo/api.py +++ b/odoo/api.py @@ -893,38 +893,24 @@ class Environment(Mapping): return self.context.get('lang') @contextmanager - def _do_in_mode(self, mode): - if self.all.mode: - yield - else: - try: - self.all.mode = mode - yield - finally: - self.all.mode = False - self.dirty.clear() - def do_in_draft(self): """ Context-switch to draft mode, where all field updates are done in cache only. """ - return self._do_in_mode(True) + if self.all.in_draft: + yield + else: + try: + self.all.in_draft = True + yield + finally: + self.all.in_draft = False + self.dirty.clear() @property def in_draft(self): """ Return whether we are in draft mode. """ - return bool(self.all.mode) - - def do_in_onchange(self): - """ Context-switch to 'onchange' draft mode, which is a specialized - draft mode used during execution of onchange methods. - """ - return self._do_in_mode('onchange') - - @property - def in_onchange(self): - """ Return whether we are in 'onchange' draft mode. """ - return self.all.mode == 'onchange' + return self.all.in_draft def clear(self): """ Clear all record caches, and discard all fields to recompute. @@ -1038,7 +1024,7 @@ class Environments(object): self.envs = WeakSet() # weak set of environments self.cache = Cache() # cache for all records self.todo = {} # recomputations {field: [records]} - self.mode = False # flag for draft/onchange + self.in_draft = False # flag for draft self.recompute = True def add(self, env): diff --git a/odoo/fields.py b/odoo/fields.py index 1267e33a217..c1d0de756b3 100644 --- a/odoo/fields.py +++ b/odoo/fields.py @@ -576,7 +576,7 @@ class Field(MetaField('DummyField', (object,), {})): # when related_sudo, bypass access rights checks when reading values others = records.sudo() if self.related_sudo else records # copy the cache of draft records into others' cache - if records.env.in_onchange and records.env != others.env: + if not all(records._ids) and records.env != others.env: copy_cache(records - records.filtered('id'), others.env) # # Traverse fields one by one for all records, in order to take advantage @@ -1022,7 +1022,7 @@ class Field(MetaField('DummyField', (object,), {})): # set value in cache record.env.cache.set(record, self, value) - if not record.id or env.in_onchange: + if not record.id: # set inverse fields on new records in the comodel if self.relational: inv_recs = record[self.name].filtered(lambda r: not r.id) @@ -1083,7 +1083,7 @@ class Field(MetaField('DummyField', (object,), {})): """ Determine the value of ``self`` for ``record``. """ env = record.env - if self.store and not (self.compute and env.in_onchange): + if self.store: # this is a stored field or an old-style function field if self.compute: # this is a stored computed field, check for recomputation diff --git a/odoo/models.py b/odoo/models.py index c08019216bb..f8e96406a13 100644 --- a/odoo/models.py +++ b/odoo/models.py @@ -2887,7 +2887,7 @@ Fields: records -= self.env.field_todo(field) # in onchange mode, discard computed fields and fields in cache - if self.env.in_onchange: + if not self.id: for f in list(fs): if f.compute or self.env.cache.contains(self, f): fs.discard(f) @@ -5535,28 +5535,25 @@ Fields: new_lines.mapped(subname) # create a new record with values, and attach ``self`` to it - with env.do_in_onchange(): - record = self.new(values, origin=self) + record = self.new(values, origin=self) # make a snapshot based on the initial values of record - with env.do_in_onchange(): - snapshot0 = snapshot1 = Snapshot(record, nametree) + snapshot0 = snapshot1 = Snapshot(record, nametree) # determine which field(s) should be triggered an onchange todo = list(names or nametree) done = set() # dummy assignment: trigger invalidations on the record - with env.do_in_onchange(): - for name in todo: - if name == 'id': - continue - value = record[name] - field = self._fields[name] - if field.type == 'many2one' and field.delegate and not value: - # do not nullify all fields of parent record for new records - continue - record[name] = value + for name in todo: + if name == 'id': + continue + value = record[name] + field = self._fields[name] + if field.type == 'many2one' and field.delegate and not value: + # do not nullify all fields of parent record for new records + continue + record[name] = value result = {'warnings': OrderedSet()} @@ -5567,18 +5564,17 @@ Fields: continue done.add(name) - with env.do_in_onchange(): - # apply field-specific onchange methods - if field_onchange.get(name): - record._onchange_eval(name, field_onchange[name], result) + # apply field-specific onchange methods + if field_onchange.get(name): + record._onchange_eval(name, field_onchange[name], result) - # make a snapshot (this forces evaluation of computed fields) - snapshot1 = Snapshot(record, nametree) + # make a snapshot (this forces evaluation of computed fields) + snapshot1 = Snapshot(record, nametree) - # determine which fields have been modified - for name in nametree: - if snapshot1[name] != snapshot0[name]: - todo.append(name) + # determine which fields have been modified + for name in nametree: + if snapshot1[name] != snapshot0[name]: + todo.append(name) # determine values that have changed by comparing snapshots self.invalidate_cache()