From 47728ccd407f683ef22ac9095b88dedbacfb0ef2 Mon Sep 17 00:00:00 2001 From: Yannick Tivisse Date: Thu, 18 Aug 2016 13:56:42 +0200 Subject: [PATCH] [FIX] hr, hr_expense: Allow employees to send emails to aliases New feature in mail module ========================== Currently, an email can be sent to an alias from: - Everyone - Authenticated Partners - Followers This commit is intended to add another category: Employees The main purpose of this new feature is to allow employees to send an email to an 'expense' alias in order to create automatically their expenses with their mobile phones. Use this new mechanism in hr_expense ==================================== Currenlty, we're overriding message_new to make a security check and create an expense if the sender is an employee or bounce otherwise, which is not the correct way to achieve this. A better way is to use the alias mechanism now that it has been extended to employees too. Don't hardcode email_from ========================= The email_from of the bounce email is hardcoded to "help@odoo.com". I am sure they will be happy to receive all answers from any odoo instance Review the bounce email content =============================== content: Your expense has not been created because your email address is not set on an employee or on a employee's user. Configure your employee's information correctly and try again. -> this is a message for the admin, not an employee or anyone else Now the content is generical for aliase defined for employees. --- addons/hr/data/hr_data.xml | 11 +++++ addons/hr/models/__init__.py | 2 + addons/hr/models/mail_alias.py | 10 ++++ addons/hr/models/mail_thread.py | 55 ++++++++++++++++++++++ addons/hr_expense/data/hr_expense_data.xml | 11 +---- addons/hr_expense/models/hr_expense.py | 19 +++----- 6 files changed, 85 insertions(+), 23 deletions(-) create mode 100644 addons/hr/models/mail_alias.py create mode 100644 addons/hr/models/mail_thread.py diff --git a/addons/hr/data/hr_data.xml b/addons/hr/data/hr_data.xml index bb2cf4b5a08..2b031146e01 100644 --- a/addons/hr/data/hr_data.xml +++ b/addons/hr/data/hr_data.xml @@ -29,5 +29,16 @@ Leave Management (keep track of employee leaves), Expense Management (manage emp + + HR: unknown employee bounce + Your document has not been created + ${user.email} + ${ctx['email_to']|safe} + ${user.lang} + + + + + diff --git a/addons/hr/models/__init__.py b/addons/hr/models/__init__.py index c870236ee6c..ef90454a6d7 100644 --- a/addons/hr/models/__init__.py +++ b/addons/hr/models/__init__.py @@ -2,5 +2,7 @@ # Part of Odoo. See LICENSE file for full copyright and licensing details. import hr +import mail_alias +import mail_thread import res_partner import res_users diff --git a/addons/hr/models/mail_alias.py b/addons/hr/models/mail_alias.py new file mode 100644 index 00000000000..710b7efaff7 --- /dev/null +++ b/addons/hr/models/mail_alias.py @@ -0,0 +1,10 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import fields, models + + +class Alias(models.Model): + _inherit = 'mail.alias' + + alias_contact = fields.Selection(selection_add=[('employees', 'Authenticated Employees')]) diff --git a/addons/hr/models/mail_thread.py b/addons/hr/models/mail_thread.py new file mode 100644 index 00000000000..d27cbbd2afc --- /dev/null +++ b/addons/hr/models/mail_thread.py @@ -0,0 +1,55 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +import logging + +from odoo import api, models +from odoo.addons.mail.models.mail_thread import decode_header +from odoo.tools import email_split + +_logger = logging.getLogger(__name__) + +class MailThread(models.AbstractModel): + _inherit = "mail.thread" + + @api.model + def message_route_verify(self, message, message_dict, route, update_author=True, assert_model=True, create_fallback=True, allow_private=False): + res = super(MailThread, self).message_route_verify(message, message_dict, route, update_author, assert_model, create_fallback, allow_private) + + if res: + alias = route[4] + email_from = decode_header(message, 'From') + message_id = message.get('Message-Id') + + # Identically equal to the definition in mail module because sub methods are local + # variables and cannot be called with super + def _create_bounce_email(body_html): + bounce_to = decode_header(message, 'Return-Path') or email_from + bounce_mail_values = { + 'body_html': body_html, + 'subject': 'Re: %s' % message.get('subject'), + 'email_to': bounce_to, + 'auto_delete': True, + } + bounce_from = self.env['ir.mail_server']._get_default_bounce_address() + if bounce_from: + bounce_mail_values['email_from'] = 'MAILER-DAEMON <%s>' % bounce_from + self.env['mail.mail'].create(bounce_mail_values).send() + + def _warn(message): + _logger.info('Routing mail with Message-Id %s: route %s: %s', + message_id, route, message) + + # Alias: check alias_contact settings for employees + + if alias and alias.alias_contact == 'employees': + email_address = 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: + mail_template = self.env.ref('hr.mail_template_data_unknown_employee_email_address') + _warn('alias %s does not accept unknown employees, skipping' % alias.alias_name) + _create_bounce_email(mail_template.body_html) + return False + return res diff --git a/addons/hr_expense/data/hr_expense_data.xml b/addons/hr_expense/data/hr_expense_data.xml index 3ca4fed609c..425fb3a6e79 100644 --- a/addons/hr_expense/data/hr_expense_data.xml +++ b/addons/hr_expense/data/hr_expense_data.xml @@ -17,6 +17,7 @@ expense + employees @@ -65,15 +66,5 @@ - - HrExpense: unknown employee bounce - Your expense has not been created - help@odoo.com - ${ctx['email_to']|safe} - ${user.lang} - - - - diff --git a/addons/hr_expense/models/hr_expense.py b/addons/hr_expense/models/hr_expense.py index 5ce0b3edfe9..e38a2537873 100644 --- a/addons/hr_expense/models/hr_expense.py +++ b/addons/hr_expense/models/hr_expense.py @@ -289,21 +289,14 @@ class HrExpense(models.Model): def message_new(self, msg_dict, custom_values=None): if custom_values is None: custom_values = {} - # Retrieve the email address from the email field. The string is constructed like - # 'foo '. We will extract 'bar' from this + email_address = email_split(msg_dict.get('email_from', False))[0] - # Look after an employee who has this email address or an employee for whom the related - # user has this email address. In the case not employee is found, we send back an email - # to explain that the expense will not be created. - 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: - # Send back an email to explain why the expense has not been created - mail_template = self.env.ref('hr_expense.mail_template_data_expense_unknown_email_address') - mail_template.with_context(email_to=email_address).send_mail(self.env.ref('base.module_hr_expense').id) - return False + employee = self.env['hr.employee'].search([ + '|', + ('work_email', 'ilike', email_address), + ('user_id.email', 'ilike', email_address) + ], limit=1) expense_description = msg_dict.get('subject', '')