Warn user and alias responsible when model creation fails on incoming message due to alias mis-configuration. Alias custom default values that references archived or deleted records can prevent the record to be created when receiving an email. Unfortunately, correcting values on the fly has too many downsides: - as the value cannot come from nowhere, we would probably end up with corrupted record like a task without a project - as errors are silent, lots of suboptimal record could be created before it is corrected - as the code would have to deal with different kind of updates, it would be heavily depend on the framework meaning additional maintenance cost - the attempt made had also a performance cost by using savepoint which we want to avoid For all those reasons, we have decided to warm the user rather than trying to solve the problem automatically. The users are warned: - through a bounce email sent to the sender and alias responsible - in the alias interface through an alias status present in the list and the form view Technical notes: - They are multiple kind of errors: error in the message (ex.: user not authorized to send to a specific alias), error in the alias (ex.: dangling reference in alias_defaults), other error (ex: technical error like a service unavailable when receiving the message). Here, we want only to detect alias error and skip other errors. So rather than doing a big try except around _message_route_process, we have selected the spot where we should detect alias error: -- in _alias_get_error_message where we distinct a message error from an alias error -- in inside _message_route_process where record are created using alias_defaults (custom values with reference to record that might have been deleted since the alias creation) - In test, we call mail.thread message_process with sudo as it is the case in real use case (processed when fetching mail). It is needed as in some test alias status are modified. Task-2675209 closes odoo/odoo#101019 Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
22 lines
977 B
Python
22 lines
977 B
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
from odoo import models, tools, _
|
|
from odoo.addons.mail.tools.alias_error import AliasError
|
|
|
|
|
|
class BaseModel(models.AbstractModel):
|
|
_inherit = 'base'
|
|
|
|
def _alias_get_error(self, message, message_dict, alias):
|
|
if alias.alias_contact == 'employees':
|
|
email_from = tools.decode_message_header(message, 'From')
|
|
email_address = tools.email_split(email_from)[0]
|
|
employee = self.env['hr.employee'].search([('work_email', 'ilike', email_address)], limit=1)
|
|
if not employee:
|
|
employee = self.env['hr.employee'].search([('user_id.email', 'ilike', email_address)], limit=1)
|
|
if not employee:
|
|
return AliasError('error_hr_employee_restricted', _('restricted to employees'))
|
|
return False
|
|
return super(BaseModel, self)._alias_get_error(message, message_dict, alias)
|