Skip to content
Permalink
Browse files

remove toflush and use precommit instead

  • Loading branch information
rco-odoo committed Jan 14, 2020
1 parent 44d3f16 commit 0ffe9182874b1966f965c574834a720e348f6406
Showing with 26 additions and 34 deletions.
  1. +2 −2 addons/mail/models/mail_thread.py
  2. +20 −15 addons/test_mail/tests/test_message_track.py
  3. +0 −3 odoo/api.py
  4. +4 −14 odoo/models.py
@@ -429,7 +429,7 @@ def _prepare_tracking(self, fields):
if not fields:
return
func = self.browse()._finalize_tracking
[initial_values] = self.env.toflush.register(func, dict)
[initial_values] = self.env.cr.precommit(func, dict)
for record in self:
if not record.id:
continue
@@ -443,7 +443,7 @@ def _discard_tracking(self):
if not self._get_tracked_fields():
return
func = self.browse()._finalize_tracking
[initial_values] = self.env.toflush.register(func, dict)
[initial_values] = self.env.cr.precommit(func, dict)
# disable tracking by setting initial values to None
for id_ in self.ids:
initial_values[id_] = None
@@ -14,16 +14,20 @@ def setUp(self):
record = self.env['mail.test.full'].with_user(self.user_employee).with_context(self._test_context).create({
'name': 'Test',
})
record.flush()
self.flush()
self.record = record.with_context(mail_notrack=False)

def flush(self):
""" Force the creation of tracking values. """
self.cr._precommit()

def test_message_track_no_tracking(self):
""" Update a set of non tracked fields -> no message, no tracking """
self.record.write({
'name': 'Tracking or not',
'count': 32,
})
self.record.flush()
self.flush()
self.assertEqual(self.record.message_ids, self.env['mail.message'])

def test_message_track_no_subtype(self):
@@ -34,7 +38,7 @@ def test_message_track_no_subtype(self):
'name': 'Test2',
'customer_id': customer.id,
})
self.record.flush()
self.flush()

# one new message containing tracking; without subtype linked to tracking, a note is generated
self.assertEqual(len(self.record.message_ids), 1)
@@ -64,7 +68,7 @@ def test_message_track_subtype(self):
'email_from': 'noone@example.com',
'umbrella_id': umbrella.id,
})
self.record.flush()
self.flush()
# one new message containing tracking; subtype linked to tracking
self.assertEqual(len(self.record.message_ids), 1)
self.assertEqual(self.record.message_ids.subtype_id, self.env.ref('test_mail.st_mail_test_full_umbrella_upd'))
@@ -89,7 +93,7 @@ def test_message_track_template(self):
'name': 'Test2',
'customer_id': self.user_admin.partner_id.id,
})
self.record.flush()
self.flush()

self.assertEqual(len(self.record.message_ids), 2, 'should have 2 new messages: one for tracking, one for template')

@@ -118,7 +122,7 @@ def test_message_track_template_at_create(self):
'customer_id': self.user_admin.partner_id.id,
'mail_template': self.env.ref('test_mail.mail_test_full_tracking_tpl').id,
})
record.flush()
self.flush()

self.assertEqual(len(record.message_ids), 1, 'should have 1 new messages for template')
# one new message containing the template linked to tracking
@@ -135,7 +139,7 @@ def test_message_tracking_sequence(self):
'user_id': self.user_admin.id,
'umbrella_id': self.env['mail.test'].with_context(mail_create_nosubscribe=True).create({'name': 'Umbrella'}).id
})
self.record.flush()
self.flush()
self.assertEqual(len(self.record.message_ids), 1, 'should have 1 tracking message')

tracking_values = self.env['mail.tracking.value'].search([('mail_message_id', '=', self.record.message_ids.id)])
@@ -150,7 +154,7 @@ def test_track_groups(self):
field.groups = 'base.group_erp_manager'

self.record.sudo().write({'email_from': 'X'})
self.record.flush()
self.flush()

msg_emp = self.record.message_ids.message_format()
msg_sudo = self.record.sudo().message_ids.message_format()
@@ -164,7 +168,7 @@ def test_notify_track_groups(self):
field.groups = 'base.group_erp_manager'

self.record.sudo().write({'email_from': 'X'})
self.record.flush()
self.flush()

msg_emp = self.record._notify_prepare_template_context(self.record.message_ids, {})
msg_sudo = self.record.sudo()._notify_prepare_template_context(self.record.message_ids, {})
@@ -174,7 +178,7 @@ def test_notify_track_groups(self):
def test_unlinked_field(self):
record_sudo = self.record.sudo()
record_sudo.write({'email_from': 'X'}) # create a tracking value
record_sudo.flush()
self.flush()
self.assertEqual(len(record_sudo.message_ids.tracking_value_ids), 1)
ir_model_field = self.env['ir.model.fields'].search([
('model', '=', 'mail.test.full'),
@@ -211,6 +215,7 @@ def patched_message_track_post_template(*args, **kwargs):
'company_id': company1.id,
'user_id': self.env.user.id, # trigger track template
})
self.flush()

new_partner = Partner.search([('email', '=', email_new_partner)])
self.assertTrue(new_partner)
@@ -223,7 +228,7 @@ def test_message_track_multiple(self):
self.record.customer_id = self.user_admin.partner_id
self.record.user_id = self.user_admin
self.record.umbrella_id = umbrella
self.record.flush()
self.flush()

# should have a single message with all tracked fields
self.assertEqual(len(self.record.message_ids), 1, 'should have 1 tracking message')
@@ -236,7 +241,7 @@ def test_message_track_multiple(self):
def test_tracked_compute(self):
# no tracking at creation
record = self.env['mail.test.compute'].create({})
record.flush()
self.flush()
self.assertEqual(len(record.message_ids), 1)
self.assertEqual(len(record.message_ids[0].tracking_value_ids), 0)

@@ -248,7 +253,7 @@ def test_tracked_compute(self):
'phone': '1234567890',
})
record.partner_id = partner
record.flush()
self.flush()
self.assertEqual(len(record.message_ids), 2)
self.assertEqual(len(record.message_ids[0].tracking_value_ids), 4)
self.assertTracking(record.message_ids[0], [
@@ -260,7 +265,7 @@ def test_tracked_compute(self):

# modify partner: one tracking message for the only recomputed field
partner.write({'name': 'Fool'})
partner.flush()
self.flush()
self.assertEqual(len(record.message_ids), 3)
self.assertEqual(len(record.message_ids[0].tracking_value_ids), 1)
self.assertTracking(record.message_ids[0], [
@@ -277,7 +282,7 @@ def test_tracked_compute(self):
# force recomputation of 'partner_phone' to make sure it does not
# generate tracking values
self.assertEqual(record.partner_phone, '0987654321')
partner.flush()
self.flush()
self.assertEqual(len(record.message_ids), 4)
self.assertEqual(len(record.message_ids[0].tracking_value_ids), 2)
self.assertTracking(record.message_ids[0], [
@@ -453,7 +453,6 @@ def __new__(cls, cr, uid, context, su=False):
self.registry = Registry(cr.dbname)
self.cache = envs.cache
self._protected = envs.protected # proxy to shared data structure
self.toflush = envs.toflush
self.all = envs
envs.add(self)

@@ -623,7 +622,6 @@ def clear(self):
self.cache.invalidate()
self.all.tocompute.clear()
self.all.towrite.clear()
self.toflush.clear()

@contextmanager
def clear_upon_failure(self):
@@ -730,7 +728,6 @@ def __init__(self):
self.tocompute = defaultdict(set) # recomputations {field: ids}
# updates {model: {id: {field: value}}}
self.towrite = defaultdict(lambda: defaultdict(dict))
self.toflush = AggCallbacks()

def add(self, env):
""" Add the environment ``env``. """
@@ -225,9 +225,6 @@ def origin_ids(ids):
# maximum number of prefetched records
PREFETCH_MAX = 1000

# maximum number of loops in flush()
FLUSH_MAX_ITERATIONS = 10

# special columns automatically created by the ORM
LOG_ACCESS_COLUMNS = ['create_uid', 'create_date', 'write_uid', 'write_date']
MAGIC_COLUMNS = ['id'] + LOG_ACCESS_COLUMNS
@@ -5418,17 +5415,10 @@ def process(model, id_vals):

if fnames is None:
# flush everything
for _index in range(FLUSH_MAX_ITERATIONS):
self.recompute()
while self.env.all.towrite:
model_name, id_vals = self.env.all.towrite.popitem()
process(self.env[model_name], id_vals)
self.env.toflush()
if not (self.env.all.tocompute or self.env.all.towrite):
break
else:
raise Exception("Too many recursive recomputations")

self.recompute()
while self.env.all.towrite:
model_name, id_vals = self.env.all.towrite.popitem()
process(self.env[model_name], id_vals)
else:
# flush self's model if any of the fields must be flushed
self.recompute(fnames, records=records)

0 comments on commit 0ffe918

Please sign in to comment.
You can’t perform that action at this time.