From 5eca7b7378a0ce9cd96f8366091434d9d71f069b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Wed, 27 Oct 2021 14:46:48 +0000 Subject: [PATCH 1/4] [REF] hr_expense: cleanup alias management Purpose of this commit is to cleanup alias usage in hr_expense. Indeed it is currently used as a master data without being cleanly supported. In this commit we * globally avoid crashes if the alias data has been removed; * recreate Xml ID record if it has been removed as it is considered as being a master data; * avoid issues when trying to configure the expense alias in settings if alias has been manually removed. Setting a name now creates the alias and an XmlID if it has been removed; * rename a generic 'use_mail_gateway' field into 'expense_use_mail_gateway' in order to avoid conflicts with future changes in generic settings linked to email gateway and configuration * fix empty list helper: it is now translated and correctly encode parameters given to 'mailto'; Task-2710804 (Mail: Clean MailThread Posting API) Prepares Task-36879 (Mail: Support MultiCompany Aliases) Part-of: odoo/odoo#105992 --- addons/hr_expense/models/__init__.py | 1 + addons/hr_expense/models/hr_expense.py | 22 +++++++---- .../hr_expense/models/res_config_settings.py | 38 +++++++++++++------ .../views/res_config_settings_views.xml | 14 +++---- 4 files changed, 49 insertions(+), 26 deletions(-) diff --git a/addons/hr_expense/models/__init__.py b/addons/hr_expense/models/__init__.py index e0f6b9318ec..e52cfb4506a 100644 --- a/addons/hr_expense/models/__init__.py +++ b/addons/hr_expense/models/__init__.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. from . import hr_employee from . import account_move diff --git a/addons/hr_expense/models/hr_expense.py b/addons/hr_expense/models/hr_expense.py index 6452a4ac736..17ba2c82c35 100644 --- a/addons/hr_expense/models/hr_expense.py +++ b/addons/hr_expense/models/hr_expense.py @@ -3,6 +3,8 @@ import re from markupsafe import Markup +import werkzeug + from odoo import api, fields, Command, models, _ from odoo.tools import float_round from odoo.exceptions import UserError, ValidationError @@ -403,17 +405,23 @@ class HrExpense(models.Model): @api.model def get_empty_list_help(self, help_message): - return super(HrExpense, self).get_empty_list_help(help_message or '' + self._get_empty_list_mail_alias()) + return super().get_empty_list_help((help_message or '') + self._get_empty_list_mail_alias()) @api.model def _get_empty_list_mail_alias(self): use_mailgateway = self.env['ir.config_parameter'].sudo().get_param('hr_expense.use_mailgateway') - alias_record = use_mailgateway and self.env.ref('hr_expense.mail_alias_expense') or False - if alias_record and alias_record.alias_domain and alias_record.alias_name: - return Markup(""" -

-Or send your receipts at %(email)s. -

""") % {'email': '%s@%s' % (alias_record.alias_name, alias_record.alias_domain)} + expense_alias = self.env.ref('hr_expense.mail_alias_expense') if use_mailgateway else False + if expense_alias and expense_alias.alias_domain and expense_alias.alias_name: + alias_email = f'{expense_alias.alias_name}@{expense_alias.alias_domain}' + # encode, but force %20 encoding for space instead of a + (URL / mailto difference) + params = werkzeug.urls.url_encode({'subject': _("Lunch with customer $12.32")}).replace('+', '%20') + return Markup( + """

%(send_string)s %(alias_email)s

""" + ) % { + 'alias_email': alias_email, + 'params': params, + 'send_string': _("Or send your receipts at"), + } return "" # ---------------------------------------- diff --git a/addons/hr_expense/models/res_config_settings.py b/addons/hr_expense/models/res_config_settings.py index b9db4a03fdc..20977d87352 100644 --- a/addons/hr_expense/models/res_config_settings.py +++ b/addons/hr_expense/models/res_config_settings.py @@ -6,14 +6,13 @@ from odoo import api, fields, models class ResConfigSettings(models.TransientModel): _inherit = 'res.config.settings' - expense_alias_prefix = fields.Char( + hr_expense_alias_prefix = fields.Char( 'Default Alias Name for Expenses', - compute='_compute_expense_alias_prefix', + compute='_compute_hr_expense_alias_prefix', store=True, readonly=False) - use_mailgateway = fields.Boolean(string='Let your employees record expenses by email', - config_parameter='hr_expense.use_mailgateway') - + hr_expense_use_mailgateway = fields.Boolean(string='Let your employees record expenses by email', + config_parameter='hr_expense.use_mailgateway') module_hr_payroll_expense = fields.Boolean(string='Reimburse Expenses in Payslip') module_hr_expense_extract = fields.Boolean(string='Send bills to OCR to generate expenses') expense_journal_id = fields.Many2one('account.journal', related='company_id.expense_journal_id', readonly=False) @@ -22,17 +21,32 @@ class ResConfigSettings(models.TransientModel): @api.model def get_values(self): res = super(ResConfigSettings, self).get_values() + expense_alias = self.env.ref('hr_expense.mail_alias_expense', raise_if_not_found=False) res.update( - expense_alias_prefix=self.env.ref('hr_expense.mail_alias_expense').alias_name, + hr_expense_alias_prefix=expense_alias.alias_name if expense_alias else False, ) return res def set_values(self): super().set_values() - alias = self.env.ref('hr_expense.mail_alias_expense', raise_if_not_found=False) - if alias and alias.alias_name != self.expense_alias_prefix: - alias.alias_name = self.expense_alias_prefix + expense_alias = self.env.ref('hr_expense.mail_alias_expense', raise_if_not_found=False) + if not expense_alias and self.hr_expense_alias_prefix: + # create data again + alias = self.env['mail.alias'].sudo().create({ + 'alias_contact': 'employees', + 'alias_model_id': self.env['ir.model']._get_id('hr.expense'), + 'alias_name': self.hr_expense_alias_prefix, + }) + self.env['ir.model.data'].sudo().create({ + 'name': 'mail_alias_expense', + 'module': 'hr_expense', + 'model': 'mail.alias', + 'noupdate': True, + 'res_id': alias.id, + }) + elif expense_alias and expense_alias.alias_name != self.hr_expense_alias_prefix: + expense_alias.alias_name = self.hr_expense_alias_prefix - @api.depends('use_mailgateway') - def _compute_expense_alias_prefix(self): - self.filtered(lambda w: not w.use_mailgateway).update({'expense_alias_prefix': False}) + @api.depends('hr_expense_use_mailgateway') + def _compute_hr_expense_alias_prefix(self): + self.filtered(lambda w: not w.hr_expense_use_mailgateway).hr_expense_alias_prefix = False diff --git a/addons/hr_expense/views/res_config_settings_views.xml b/addons/hr_expense/views/res_config_settings_views.xml index f2b16ea85ca..d1cf962c873 100644 --- a/addons/hr_expense/views/res_config_settings_views.xml +++ b/addons/hr_expense/views/res_config_settings_views.xml @@ -15,22 +15,22 @@ id="create_expense_setting" title="Send an email to this email alias with the receipt in attachment to create an expense in one click. If the first word of the mail subject contains the category's internal reference or the category name, the corresponding category will automatically be set. Type the expense amount in the mail subject to set it on the expense too.">
- +
-