Skip to content

Commit

Permalink
[IMP] mail: better error reporting to follow
Browse files Browse the repository at this point in the history
  • Loading branch information
xmo-odoo committed Nov 14, 2020
1 parent 07c5a44 commit 4435170
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
18 changes: 17 additions & 1 deletion addons/mail/models/mail_followers.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def _invalidate_documents(self):

@api.model_create_multi
def create(self, vals_list):
res = super(Followers, self).create(vals_list)
res = super(Followers, self).create(vals_list)._check_rights()
res._invalidate_documents()
return res

Expand All @@ -57,6 +57,7 @@ def write(self, vals):
if 'res_model' in vals or 'res_id' in vals:
self._invalidate_documents()
res = super(Followers, self).write(vals)
self._check_rights()
if any(x in vals for x in ['res_model', 'res_id', 'partner_id']):
self._invalidate_documents()
return res
Expand All @@ -66,6 +67,21 @@ def unlink(self):
self._invalidate_documents()
return super(Followers, self).unlink()

def _check_rights(self):
user_partner = self.env.user.partner_id
for record in self:
obj = self.env[record.res_model].browse(record.res_id)
if record.channel_id or record.partner_id != user_partner:
obj.check_access_rights('write')
obj.check_access_rule('write')
subject = record.channel_id or record.partner_id
subject.check_access_rights('read')
subject.check_access_rule('read ')
else:
obj.check_access_rights('read')
obj.check_access_rule('read')
return self

_sql_constraints = [
('mail_followers_res_partner_res_model_id_uniq', 'unique(res_model,res_id,partner_id)', 'Error, a partner cannot follow twice the same object.'),
('mail_followers_res_channel_res_model_id_uniq', 'unique(res_model,res_id,channel_id)', 'Error, a channel cannot follow twice the same object.'),
Expand Down
16 changes: 10 additions & 6 deletions addons/test_mail/tests/test_performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,22 @@ def test_write_mail(self):
records = self.env['test_performance.mail'].search([])
self.assertEqual(len(records), 5)

with self.assertQueryCount(__system__=3, demo=3): # test_mail only: 3 - 3
with self.assertQueryCount(__system__=3, demo=4): # test_mail only: 3 - 4
records.write({'name': 'X'})

@users('__system__', 'demo')
@warmup
def test_write_mail_with_recomputation(self):
""" Write records inheriting from 'mail.thread' (with recomputation). """
import logging
logging.getLogger(__name__).info("================================================")
records = self.env['test_performance.mail'].search([])
self.assertEqual(len(records), 5)

with self.assertQueryCount(__system__=5, demo=5): # test_mail only: 5 - 5
with self.assertQueryCount(__system__=5, demo=6): # test_mail only: 5 - 6
records._cr.sql_log = True
records.write({'value': 42})
records._cr.sql_log = False

@users('__system__', 'demo')
@warmup
Expand Down Expand Up @@ -96,13 +100,13 @@ def test_create_mail(self):
@warmup
def test_create_mail_with_tracking(self):
""" Create records inheriting from 'mail.thread' (with field tracking). """
with self.assertQueryCount(__system__=13, demo=13): # test_mail only: 13 - 13
with self.assertQueryCount(__system__=13, demo=14): # test_mail only: 13 - 14
self.env['test_performance.mail'].create({'name': 'X'})

@users('__system__', 'emp')
@warmup
def test_create_mail_simple(self):
with self.assertQueryCount(__system__=8, emp=8): # test_mail only: 8 - 8
with self.assertQueryCount(__system__=8, emp=9): # test_mail only: 8 - 9
self.env['mail.test.simple'].create({'name': 'Test'})

@users('__system__', 'emp')
Expand Down Expand Up @@ -158,7 +162,7 @@ def setUp(self):
def test_adv_activity(self):
model = self.env['mail.test.activity']

with self.assertQueryCount(__system__=9, emp=8): # test_mail only: 9 - 8
with self.assertQueryCount(__system__=9, emp=9): # test_mail only: 9 - 9
model.create({'name': 'Test'})

@users('__system__', 'emp')
Expand Down Expand Up @@ -263,7 +267,7 @@ def test_message_post_no_notification(self):
def test_message_post_one_email_notification(self):
record = self.env['mail.test.simple'].create({'name': 'Test'})

with self.assertQueryCount(__system__=52, emp=72): # com runbot: 52 - 72 // test_mail only: 48 - 68
with self.assertQueryCount(__system__=53, emp=72): # com runbot: 53 - 72 // test_mail only: 48 - 68
record.message_post(
body='<p>Test Post Performances with an email ping</p>',
partner_ids=self.customer.ids,
Expand Down

0 comments on commit 4435170

Please sign in to comment.