From 33bd1a951a8c97098f4a29ff3fbc5d967828640d Mon Sep 17 00:00:00 2001 From: Julien Castiaux Date: Thu, 6 Aug 2020 11:04:16 +0000 Subject: [PATCH] [FIX] account,mail: default purchase mail alias non-ascii MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Change the `addons/account/i18n/vi.po` PO file to replace the translation of `Vendor Bill` like msgids from `Công nợ nhà cung cấp` to ` by `Hóa đơn Nhà cung cấp`, there should be 3. What's matter is the `đ` character. * Create a brand new Vietnamese database (using `-l vi_VN` and `--load-language=vi_VN`) * Install Invoicing (`account`), an email alias constraint is raised. > You cannot use anything else than unaccented latin characters in the > alias address. During the installation of the invoicing module, an email alias is created for every journal account of purchase type. The alias is created using the current journal name. Here the journal name is `Hóa đơn Nhà cung cấp`, as mail aliases should only contain ascii characters, we attempt to remove the accents. The `tools.misc.remove_accents` successfully remove most of them but the `đ` character is left as-is, the final alias is `hoa-đon-nha-cung-cap` which is invalid as it contains some non-ascii characters. The `remove_accents` function uses the [NFKD] Unicode Compatibility Decomposition Normalization Form. This normalization process unify all abstract equivalent characters, i.e. characters only different by font or rotation. While this normalization process helps to transcript a lot of characters to their ascii compatibility equivalent, some 118,230 other non-ascii characters (mainly from the Chinese and Japanese scripts) are left as-is as they lack an ascii compatibility equivalent. The solution has been to not use the journal name but rather the journal code or journal type as email alias when the name is unsafe. The journal code is a short version of the name, the journal type is not translatable so is always ascii. One additional watchdog has been added to the sanitization function of email aliases so non-ascii characters are replaced with a '?' which is an authorized character in emails local-part. [NFKD] https://unicode.org/reports/tr15/ Closes #55492 Closes #55493 closes odoo/odoo#58279 X-original-commit: 8dc85be3821cf26226cd0bf4ce2b6739e68fc66f Signed-off-by: Simon Goffin (sig) Signed-off-by: Julien Castiaux Co-authored-by: Thong Le --- addons/account/models/account_journal.py | 17 ++++++++++++++++- addons/mail/models/mail_alias.py | 1 + 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/addons/account/models/account_journal.py b/addons/account/models/account_journal.py index dcaac32a951..249239f4dca 100644 --- a/addons/account/models/account_journal.py +++ b/addons/account/models/account_journal.py @@ -3,8 +3,12 @@ from odoo import api, fields, models, _ from odoo.osv import expression from odoo.exceptions import UserError, ValidationError from odoo.addons.base.models.res_bank import sanitize_account_number +from odoo.tools import remove_accents +import logging import re +_logger = logging.getLogger(__name__) + class AccountJournalGroup(models.Model): _name = 'account.journal.group' @@ -367,7 +371,18 @@ class AccountJournal(models.Model): if not alias_name: alias_name = self.name if self.company_id != self.env.ref('base.main_company'): - alias_name += '-' + re.sub("[^\w!#$%&'*+/=?^`{|}~\-]", '', str(self.company_id.name)) + alias_name += '-' + str(self.company_id.name) + try: + remove_accents(alias_name).encode('ascii') + except UnicodeEncodeError: + try: + remove_accents(self.code).encode('ascii') + safe_alias_name = self.code + except UnicodeEncodeError: + safe_alias_name = self.type + _logger.warning("Cannot use '%s' as email alias, fallback to '%s'", + alias_name, safe_alias_name) + alias_name = safe_alias_name return { 'alias_defaults': {'move_type': type == 'purchase' and 'in_invoice' or 'out_invoice', 'company_id': self.company_id.id, 'journal_id': self.id}, 'alias_parent_thread_id': self.id, diff --git a/addons/mail/models/mail_alias.py b/addons/mail/models/mail_alias.py index 3c08bb168d6..b41b267947f 100644 --- a/addons/mail/models/mail_alias.py +++ b/addons/mail/models/mail_alias.py @@ -142,6 +142,7 @@ class Alias(models.Model): name already exists an UserError is raised. """ sanitized_name = remove_accents(name).lower().split('@')[0] sanitized_name = re.sub(r'[^\w+.]+', '-', sanitized_name) + sanitized_name = sanitized_name.encode('ascii', errors='replace').decode() catchall_alias = self.env['ir.config_parameter'].sudo().get_param('mail.catchall.alias') bounce_alias = self.env['ir.config_parameter'].sudo().get_param('mail.bounce.alias')