[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
This commit is contained in:
Pierre-Yves Dufays
2023-10-12 16:12:09 +00:00
committed by Thibault Delavallée
parent 18819bdb4b
commit 0a66ff9283
3 changed files with 39 additions and 18 deletions
+1 -1
View File
@@ -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
+35
View File
@@ -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
+3 -17
View File
@@ -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