From 0a66ff9283f077a1dba03bf332752cb3850e6a13 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Dufays Date: Fri, 6 Oct 2023 15:22:59 +0200 Subject: [PATCH] [IMP] mail: move res_ids parser in tools Purpose is to make it more widely available, as it can be used in other wizards dealing with res_ids / active_ids. Task-3390865 Part-of: odoo/odoo#137969 --- addons/mail/models/mail_ice_server.py | 2 +- addons/mail/tools/parser.py | 35 ++++++++++++++++++++++ addons/mail/wizard/mail_compose_message.py | 20 ++----------- 3 files changed, 39 insertions(+), 18 deletions(-) create mode 100644 addons/mail/tools/parser.py diff --git a/addons/mail/models/mail_ice_server.py b/addons/mail/models/mail_ice_server.py index b45c04945b4..b17370cfc4d 100644 --- a/addons/mail/models/mail_ice_server.py +++ b/addons/mail/models/mail_ice_server.py @@ -2,7 +2,7 @@ # Part of Odoo. See LICENSE file for full copyright and licensing details. from odoo import fields, models -from odoo.addons.mail.tools.credentials import get_twilio_credentials +from odoo.addons.mail.tools.discuss import get_twilio_credentials import requests diff --git a/addons/mail/tools/parser.py b/addons/mail/tools/parser.py new file mode 100644 index 00000000000..9d0d6339452 --- /dev/null +++ b/addons/mail/tools/parser.py @@ -0,0 +1,35 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +import ast + +from odoo import _, tools +from odoo.exceptions import ValidationError + + +def parse_res_ids(res_ids): + """ Returns the already valid list/tuple of int or returns the literal eval + of the string as a list/tuple of int. Void strings / missing values are + evaluated as an empty list. + + :param str|tuple|list res_ids: a list of ids, tuple or list; + + :raise: ValidationError if the provided res_ids is an incorrect type or + invalid format; + + :return list: list of ids + """ + if tools.is_list_of(res_ids, int) or not res_ids: + return res_ids + error_msg = _("Invalid res_ids %(res_ids_str)s (type %(res_ids_type)s)", + res_ids_str=res_ids, + res_ids_type=type(res_ids)) + try: + res_ids = ast.literal_eval(res_ids) + except Exception as e: + raise ValidationError(error_msg) from e + + if not tools.is_list_of(res_ids, int): + raise ValidationError(error_msg) + + return res_ids diff --git a/addons/mail/wizard/mail_compose_message.py b/addons/mail/wizard/mail_compose_message.py index f5d4272833d..c7a342b9bf3 100644 --- a/addons/mail/wizard/mail_compose_message.py +++ b/addons/mail/wizard/mail_compose_message.py @@ -10,6 +10,7 @@ from odoo import _, api, fields, models, tools, Command from odoo.exceptions import UserError, ValidationError from odoo.osv import expression from odoo.tools import is_html_empty +from odoo.addons.mail.tools.parser import parse_res_ids def _reopen(self, res_id, model, context=None): @@ -379,7 +380,7 @@ class MailComposer(models.TransientModel): if composer.parent_id and composer.composition_mode == 'comment': composer.res_ids = f"{[composer.parent_id.res_id]}" else: - active_res_ids = self._parse_res_ids(self.env.context.get('active_ids')) + active_res_ids = parse_res_ids(self.env.context.get('active_ids')) # beware, field is limited in storage, usage of active_ids in context still required if active_res_ids and len(active_res_ids) <= self._batch_size: composer.res_ids = f"{self.env.context['active_ids']}" @@ -1329,27 +1330,12 @@ class MailComposer(models.TransientModel): :return: a list of IDs (empty list in case of falsy strings)""" self.ensure_one() - return self._parse_res_ids( + return parse_res_ids( self.env.context.get('composer_force_res_ids') or self.res_ids or self.env.context.get('active_ids') ) or [] - @api.model - def _parse_res_ids(self, res_ids): - if tools.is_list_of(res_ids, int) or not res_ids: - return res_ids - error_msg = _("Invalid res_ids %(res_ids_str)s (type %(res_ids_type)s)", - res_ids_str=res_ids, - res_ids_type=type(res_ids)) - try: - res_ids = ast.literal_eval(res_ids) - except Exception as e: - raise ValidationError(error_msg) from e - if not tools.is_list_of(res_ids, int): - raise ValidationError(error_msg) - return res_ids - def _set_value_from_template(self, template_fname, composer_fname=False): """ Set composer value from its template counterpart. In monorecord comment mode, we get directly the rendered value, giving the real