[IMP] portal: add access token link in notification emails for customers

Purpose of this commit is to allow customer portal to have an access link
containing an access token. Portal mixin now override the default mail.thread
behavior when classifying recipients of a notification email in groups.

If the model has an access_token and a customer field correctly defined
the recipient that matches the customer will have an access link in its
notification email holding the access token. It will also contain an
auth signup token if it is enabled.
This commit is contained in:
Thibault Delavallée
2018-02-15 14:50:36 +01:00
parent 7176d04f3b
commit 0e22ccb721
+47 -1
View File
@@ -3,7 +3,7 @@
from werkzeug.urls import url_encode
from odoo import api, fields, models
from odoo import api, fields, models, _
class PortalMixin(models.AbstractModel):
@@ -18,6 +18,27 @@ class PortalMixin(models.AbstractModel):
for record in self:
record.portal_url = '#'
def _get_access_token_field(self):
""" Give the field used to fetch the customer portal access token, if
any. Override this method if the field holding the token is named
differently. """
return 'access_token' if 'access_token' in self else False
def _get_customer_field(self):
""" Give the field used to fetch the customer partner_id, if any.
Override this method if the field holding the token is named differently. """
return 'partner_id' if 'partner_id' in self else False
def _get_access_token(self):
""" Get the current record access token """
field = self._get_access_token_field()
return self[field] if field else False
def _get_customer(self):
""" Get the current record custome (res.partner record) """
field = self._get_customer_field()
return self[field] if field else self.env['res.partner']
def get_share_url(self):
self.ensure_one()
params = {
@@ -30,3 +51,28 @@ class PortalMixin(models.AbstractModel):
params.update(self.partner_id.signup_get_auth_param()[self.partner_id.id])
return '/mail/view?' + url_encode(params)
@api.multi
def _notification_recipients(self, message, groups):
access_token = self._get_access_token()
customer = self._get_customer()
if access_token and customer:
additional_params = {
'access_token': self.access_token,
}
additional_params.update(customer.signup_get_auth_param()[customer.id])
access_link = self._notification_link_helper('view', **additional_params)
new_group = [
('portal_customer', lambda partner: partner.id == customer.id, {
'has_button_access': False,
'button_access': {
'url': access_link,
'title': ('View %s') % self.env['ir.model']._get(message.model).display_name,
},
})
]
else:
new_group = []
return super(PortalMixin, self)._notification_recipients(message, new_group + groups)