[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)
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
@@ -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')
@@ -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 """
@@ -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',
+11 -25
View File
@@ -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):
+3 -3
View File
@@ -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
+21 -25
View File
@@ -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()