diff options
| author | Yannick Tivisse <yti@odoo.com> | 2016-08-18 13:56:42 +0200 |
|---|---|---|
| committer | Yannick Tivisse <yti@odoo.com> | 2016-08-25 14:55:08 +0200 |
| commit | 47728ccd407f683ef22ac9095b88dedbacfb0ef2 (patch) | |
| tree | 7cb3ae837be334dc24491d4b17b1740a6681cf15 | |
| parent | 920b791ac5751fe5c4c71403b616d9c33b5f19d9 (diff) | |
[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.
| -rw-r--r-- | addons/hr/data/hr_data.xml | 11 | ||||
| -rw-r--r-- | addons/hr/models/__init__.py | 2 | ||||
| -rw-r--r-- | addons/hr/models/mail_alias.py | 10 | ||||
| -rw-r--r-- | addons/hr/models/mail_thread.py | 55 | ||||
| -rw-r--r-- | addons/hr_expense/data/hr_expense_data.xml | 11 | ||||
| -rw-r--r-- | addons/hr_expense/models/hr_expense.py | 19 |
6 files changed, 85 insertions, 23 deletions
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 <field name="image" eval="obj(ref('base.partner_root')).image" model="res.partner"/> </record> + <record id="mail_template_data_unknown_employee_email_address" model="mail.template"> + <field name="name">HR: unknown employee bounce</field> + <field name="subject">Your document has not been created</field> + <field name="email_from">${user.email}</field> + <field name="email_to">${ctx['email_to']|safe}</field> + <field name="lang">${user.lang}</field> + <field name="model_id" ref="base.model_ir_module_module"/> + <field name="auto_delete" eval="True"/> + <field name="body_html" ><![CDATA[Your document has not been created because your email address is not recognized. Please send emails with the email address recorded on your employee information, or contact your HR manager.]]></field> + </record> + </data> </odoo> 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 @@ <field name="alias_name">expense</field> <field name="alias_model_id" ref="model_hr_expense"/> <field name="alias_user_id" ref="base.user_root"/> + <field name="alias_contact">employees</field> </record> @@ -65,15 +66,5 @@ <field name="sequence" eval="10"/> </record> - <record id="mail_template_data_expense_unknown_email_address" model="mail.template"> - <field name="name">HrExpense: unknown employee bounce</field> - <field name="subject">Your expense has not been created</field> - <field name="email_from">help@odoo.com</field> - <field name="email_to">${ctx['email_to']|safe}</field> - <field name="lang">${user.lang}</field> - <field name="model_id" ref="base.model_ir_module_module"/> - <field name="auto_delete" eval="True"/> - <field name="body_html" ><![CDATA[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.]]></field> - </record> </data> </odoo> 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 <bar>'. 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', '') |
