Skip to content

Commit

Permalink
[FIX] portal: fix recipients computation override
Browse files Browse the repository at this point in the history
Portal defines the 'portal.mixin' that overrides a method from 'mail.thread'.
However no explicit inheritance is given between those two mixin. Some Odoo
models notably inherit from 'portal.mixin' and not from 'mail.thread'. It
makes no sense to override a method the model does not explicitly inherit.

In this commit we move the override on 'mail.thread' by checking the model
also inherits from 'portal.mixin' before doing portal-specific computation.
This fixes tests introduced previously.

Task-3175768 (Mail: check inheritances / overrides)

closes odoo#112095

Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
  • Loading branch information
tde-banana-odoo committed Feb 10, 2023
1 parent 784c37b commit cc26cea
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 40 deletions.
39 changes: 39 additions & 0 deletions addons/portal/models/mail_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,45 @@ class MailThread(models.AbstractModel):
domain=lambda self: [('model', '=', self._name), '|', ('message_type', '=', 'comment'), ('message_type', '=', 'email')], auto_join=True,
help="Website communication history")

def _notify_get_recipients_groups(self, msg_vals=None):
groups = super()._notify_get_recipients_groups(msg_vals=msg_vals)
if not self:
return groups

portal_enabled = isinstance(self, type(self.env['portal.mixin']))
if not portal_enabled:
return groups

customer = self._mail_get_partners()[self.id]
if customer:
access_token = self._portal_ensure_token()
local_msg_vals = dict(msg_vals or {})
local_msg_vals['access_token'] = access_token
local_msg_vals['pid'] = customer.id
local_msg_vals['hash'] = self._sign_token(customer.id)
local_msg_vals.update(customer.signup_get_auth_param()[customer.id])
access_link = self._notify_get_action_link('view', **local_msg_vals)

new_group = [
('portal_customer', lambda pdata: pdata['id'] == customer.id, {
'has_button_access': True,
'button_access': {
'url': access_link,
},
'notification_is_customer': True,
})
]
else:
new_group = []

# enable portal users that should have access through portal (if not access rights
# will do their duty)
portal_group = next(group for group in groups if group[0] == 'portal')
portal_group[2]['active'] = True
portal_group[2]['has_button_access'] = True

return new_group + groups

def _sign_token(self, pid):
"""Generate a secure hash for this record with the email of the recipient with whom the record have been shared.
Expand Down
36 changes: 0 additions & 36 deletions addons/portal/models/portal_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,42 +64,6 @@ def _get_share_url(self, redirect=False, signup_partner=False, pid=None, share_t

return '%s?%s' % ('/mail/view' if redirect else self.access_url, url_encode(params))

def _notify_get_recipients_groups(self, msg_vals=None):
groups = super(PortalMixin, self)._notify_get_recipients_groups(msg_vals=msg_vals)
if not self:
return groups

access_token = self._portal_ensure_token()
local_msg_vals = dict(msg_vals or {})

if access_token and 'partner_id' in self._fields and self['partner_id']:
customer = self['partner_id']
local_msg_vals['access_token'] = self.access_token
local_msg_vals['pid'] = customer.id
local_msg_vals['hash'] = self._sign_token(customer.id)
local_msg_vals.update(customer.signup_get_auth_param()[customer.id])
access_link = self._notify_get_action_link('view', **local_msg_vals)

new_group = [
('portal_customer', lambda pdata: pdata['id'] == customer.id, {
'has_button_access': True,
'button_access': {
'url': access_link,
},
'notification_is_customer': True,
})
]
else:
new_group = []

# enable portal users that should have access through portal (if not access rights
# will do their duty)
portal_group = next(group for group in groups if group[0] == 'portal')
portal_group[2]['active'] = True
portal_group[2]['has_button_access'] = True

return new_group + groups

def _get_access_action(self, access_uid=None, force_website=False):
""" Instead of the classic form view, redirect to the online document for
portal users or if force_website=True. """
Expand Down
15 changes: 11 additions & 4 deletions addons/test_mail_full/tests/test_mail_thread_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def test_notify_get_recipients_groups(self):
with self.subTest(test_record=test_record):
is_portal = test_record._name != 'mail.test.simple'
has_customer = test_record._name != 'mail.test.portal.no.partner'
partner_fnames = test_record._mail_get_partner_fields()

if is_portal:
self.assertFalse(
Expand All @@ -63,15 +64,21 @@ def test_notify_get_recipients_groups(self):

if is_portal and has_customer:
# should have generated the access token, required for portal links
self.assertFalse(
self.assertTrue(
test_record.access_token,
'TODO: Method not called, access token is not generated'
'Portal should generate access token'
)
# check portal_customer content and link
self.assertFalse(
self.assertTrue(
portal_customer_group,
'TODO: MEthod not called, group is not present'
'Portal Mixin should add portal customer notification group'
)
portal_url = portal_customer_group[2]['button_access']['url']
parameters = url_parse(portal_url).decode_query()
self.assertEqual(parameters['access_token'], test_record.access_token)
self.assertEqual(parameters['model'], test_record._name)
self.assertEqual(parameters['pid'], str(test_record[partner_fnames[0]].id))
self.assertEqual(parameters['res_id'], str(test_record.id))
else:
self.assertFalse(
portal_customer_group,
Expand Down

0 comments on commit cc26cea

Please sign in to comment.