[REF] api: remove deprecated env.in_onchange

This commit is contained in:
Raphael Collet
2019-05-23 05:47:11 +00:00
parent e70abb63d4
commit 76ee0afb29
7 changed files with 53 additions and 78 deletions
+1 -1
View File
@@ -757,7 +757,7 @@ class HolidaysRequest(models.Model):
current_employee = self.env['hr.employee'].search([('user_id', '=', self.env.uid)], limit=1) 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_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') is_manager = self.env.user.has_group('hr_holidays.group_hr_holidays_manager')
if self.env.in_onchange: if not all(self._ids):
return return
for holiday in self: for holiday in self:
val_type = holiday.holiday_status_id.validation_type val_type = holiday.holiday_status_id.validation_type
@@ -522,7 +522,7 @@ class HolidaysAllocation(models.Model):
def _check_approval_update(self, state): def _check_approval_update(self, state):
""" Check if target state is achievable. """ """ Check if target state is achievable. """
current_employee = self.env['hr.employee'].search([('user_id', '=', self.env.uid)], limit=1) 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 return
is_officer = self.env.user.has_group('hr_holidays.group_hr_holidays_user') 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') is_manager = self.env.user.has_group('hr_holidays.group_hr_holidays_manager')
@@ -1116,17 +1116,16 @@ class TestFields(common.TransactionCase):
env = self.env(user=self.env.ref('base.user_demo')) env = self.env(user=self.env.ref('base.user_demo'))
self.assertEqual(env.user.login, "demo") self.assertEqual(env.user.login, "demo")
with self.env.do_in_onchange(): # create a new message as demo user
# create a new message as demo user discussion = self.env.ref('test_new_api.discussion_0')
discussion = self.env.ref('test_new_api.discussion_0') message = env['test_new_api.message'].new({'discussion': discussion})
message = env['test_new_api.message'].new({'discussion': discussion}) self.assertEqual(message.discussion, discussion)
self.assertEqual(message.discussion, discussion)
# read the related field discussion_name # read the related field discussion_name
self.assertEqual(message.discussion.env, env) self.assertEqual(message.discussion.env, env)
self.assertEqual(message.discussion_name, discussion.name) self.assertEqual(message.discussion_name, discussion.name)
with self.assertRaises(AccessError): with self.assertRaises(AccessError):
message.discussion.name message.discussion.name
@mute_logger('odoo.addons.base.models.ir_model') @mute_logger('odoo.addons.base.models.ir_model')
def test_42_new_related(self): def test_42_new_related(self):
@@ -1139,15 +1138,14 @@ class TestFields(common.TransactionCase):
env = self.env(user=self.env.ref('base.user_demo')) env = self.env(user=self.env.ref('base.user_demo'))
self.assertEqual(env.user.login, "demo") self.assertEqual(env.user.login, "demo")
with self.env.do_in_onchange(): # create a new discussion and a new message as demo user
# create a new discussion and a new message as demo user discussion = env['test_new_api.discussion'].new({'name': 'Stuff'})
discussion = env['test_new_api.discussion'].new({'name': 'Stuff'}) message = env['test_new_api.message'].new({'discussion': discussion})
message = env['test_new_api.message'].new({'discussion': discussion}) self.assertEqual(message.discussion, discussion)
self.assertEqual(message.discussion, discussion)
# read the related field discussion_name # read the related field discussion_name
self.assertNotEqual(message.sudo().env, message.env) self.assertNotEqual(message.sudo().env, message.env)
self.assertEqual(message.discussion_name, discussion.name) self.assertEqual(message.discussion_name, discussion.name)
def test_43_new_related(self): def test_43_new_related(self):
""" test the behavior of one2many related fields """ """ test the behavior of one2many related fields """
@@ -369,11 +369,6 @@ class TestPerformance(TransactionCase):
new_recs = records.browse(records.new(origin=record).id for record in records) new_recs = records.browse(records.new(origin=record).id for record in records)
new_recs.mapped('value') 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 # clean up after each pass
self.env.cr.execute( self.env.cr.execute(
'delete from test_performance_base where id not in %s', 'delete from test_performance_base where id not in %s',
+11 -25
View File
@@ -893,38 +893,24 @@ class Environment(Mapping):
return self.context.get('lang') return self.context.get('lang')
@contextmanager @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): def do_in_draft(self):
""" Context-switch to draft mode, where all field updates are done in """ Context-switch to draft mode, where all field updates are done in
cache only. 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 @property
def in_draft(self): def in_draft(self):
""" Return whether we are in draft mode. """ """ Return whether we are in draft mode. """
return bool(self.all.mode) return self.all.in_draft
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'
def clear(self): def clear(self):
""" Clear all record caches, and discard all fields to recompute. """ 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.envs = WeakSet() # weak set of environments
self.cache = Cache() # cache for all records self.cache = Cache() # cache for all records
self.todo = {} # recomputations {field: [records]} self.todo = {} # recomputations {field: [records]}
self.mode = False # flag for draft/onchange self.in_draft = False # flag for draft
self.recompute = True self.recompute = True
def add(self, env): def add(self, env):
+3 -3
View File
@@ -576,7 +576,7 @@ class Field(MetaField('DummyField', (object,), {})):
# when related_sudo, bypass access rights checks when reading values # when related_sudo, bypass access rights checks when reading values
others = records.sudo() if self.related_sudo else records others = records.sudo() if self.related_sudo else records
# copy the cache of draft records into others' cache # 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) copy_cache(records - records.filtered('id'), others.env)
# #
# Traverse fields one by one for all records, in order to take advantage # 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 # set value in cache
record.env.cache.set(record, self, value) 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 # set inverse fields on new records in the comodel
if self.relational: if self.relational:
inv_recs = record[self.name].filtered(lambda r: not r.id) 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``. """ """ Determine the value of ``self`` for ``record``. """
env = record.env 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 # this is a stored field or an old-style function field
if self.compute: if self.compute:
# this is a stored computed field, check for recomputation # this is a stored computed field, check for recomputation
+21 -25
View File
@@ -2887,7 +2887,7 @@ Fields:
records -= self.env.field_todo(field) records -= self.env.field_todo(field)
# in onchange mode, discard computed fields and fields in cache # in onchange mode, discard computed fields and fields in cache
if self.env.in_onchange: if not self.id:
for f in list(fs): for f in list(fs):
if f.compute or self.env.cache.contains(self, f): if f.compute or self.env.cache.contains(self, f):
fs.discard(f) fs.discard(f)
@@ -5535,28 +5535,25 @@ Fields:
new_lines.mapped(subname) new_lines.mapped(subname)
# create a new record with values, and attach ``self`` to it # 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 # 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 # determine which field(s) should be triggered an onchange
todo = list(names or nametree) todo = list(names or nametree)
done = set() done = set()
# dummy assignment: trigger invalidations on the record # dummy assignment: trigger invalidations on the record
with env.do_in_onchange(): for name in todo:
for name in todo: if name == 'id':
if name == 'id': continue
continue value = record[name]
value = record[name] field = self._fields[name]
field = self._fields[name] if field.type == 'many2one' and field.delegate and not value:
if field.type == 'many2one' and field.delegate and not value: # do not nullify all fields of parent record for new records
# do not nullify all fields of parent record for new records continue
continue record[name] = value
record[name] = value
result = {'warnings': OrderedSet()} result = {'warnings': OrderedSet()}
@@ -5567,18 +5564,17 @@ Fields:
continue continue
done.add(name) done.add(name)
with env.do_in_onchange(): # apply field-specific onchange methods
# apply field-specific onchange methods if field_onchange.get(name):
if field_onchange.get(name): record._onchange_eval(name, field_onchange[name], result)
record._onchange_eval(name, field_onchange[name], result)
# make a snapshot (this forces evaluation of computed fields) # make a snapshot (this forces evaluation of computed fields)
snapshot1 = Snapshot(record, nametree) snapshot1 = Snapshot(record, nametree)
# determine which fields have been modified # determine which fields have been modified
for name in nametree: for name in nametree:
if snapshot1[name] != snapshot0[name]: if snapshot1[name] != snapshot0[name]:
todo.append(name) todo.append(name)
# determine values that have changed by comparing snapshots # determine values that have changed by comparing snapshots
self.invalidate_cache() self.invalidate_cache()