Skip to content
Snippets Groups Projects
Commit 463b620e40e7 authored by Houzefa Abbasbhay's avatar Houzefa Abbasbhay :slight_smile:
Browse files

MailTemplate converter: Allow multiple records

parent 4eba42946578
No related branches found
No related tags found
3 merge requests!30Merge 13.0 (13.0.3.0.2 ➔ 13.0.3.1.0),!29Code formatting, add license / docs / badges,!28MailTemplate converter: Allow multiple records
...@@ -2,6 +2,13 @@ ...@@ -2,6 +2,13 @@
History History
******* *******
13.0.3.1.0
==========
(port from 11.0.1.2.0)
* MailTemplate converter: Allow multiple records.
3.0.2 3.0.2
===== =====
......
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
"name": "Converter", "name": "Converter",
"license": "AGPL-3", "license": "AGPL-3",
"summary": "Convert odoo records to/from plain data structures.", "summary": "Convert odoo records to/from plain data structures.",
"version": "13.0.3.0.2", "version": "13.0.3.1.0",
"category": "Hidden", "category": "Hidden",
"author": "XCG Consulting", "author": "XCG Consulting",
"website": "https://odoo.consulting/", "website": "https://odoo.consulting/",
......
...@@ -7,8 +7,13 @@ ...@@ -7,8 +7,13 @@
class MailTemplate(base.Converter): class MailTemplate(base.Converter):
"""This converter wraps ``mail.template::_render_template``.
Multiple records are allowed but ``mail.template::render_template`` still
runs once per record; to accomodate, we provide ``ctx["records"]``.
"""
def __init__(self, template: str, post_eval: bool = False): def __init__(self, template: str, post_eval: bool = False):
self.template = template self.template = template
self.post_eval = post_eval self.post_eval = post_eval
def odoo_to_message( def odoo_to_message(
...@@ -10,7 +15,7 @@ ...@@ -10,7 +15,7 @@
def __init__(self, template: str, post_eval: bool = False): def __init__(self, template: str, post_eval: bool = False):
self.template = template self.template = template
self.post_eval = post_eval self.post_eval = post_eval
def odoo_to_message( def odoo_to_message(
self, instance: models.Model, ctx: Optional[Dict] = None self, records: models.Model, ctx: Optional[Dict] = None
) -> Any: ) -> Any:
...@@ -16,5 +21,8 @@ ...@@ -16,5 +21,8 @@
) -> Any: ) -> Any:
template = instance.env["mail.template"].with_context(safe=True) multiple_records = len(records) > 1
value = template._render_template( record_ids_or_id = records.ids if multiple_records else records.id
self.template, instance._name, instance.id value = (
records.env["mail.template"]
.with_context(records=records, safe=True)
._render_template(self.template, records._name, record_ids_or_id)
) )
...@@ -20,4 +28,6 @@ ...@@ -20,4 +28,6 @@
) )
if multiple_records: # render_template outputs indexed by record ID
value = value[records[0].id]
if self.post_eval: if self.post_eval:
value = ast.literal_eval(value) value = ast.literal_eval(value)
return value return value
...@@ -2,5 +2,6 @@ ...@@ -2,5 +2,6 @@
from . import test_converters # noqa: F401 from . import test_converters # noqa: F401
from . import test_field # noqa: F401 from . import test_field # noqa: F401
from . import test_ir_model # noqa: F401 from . import test_ir_model # noqa: F401
from . import test_mail_template # noqa: F401
from . import test_relation # noqa: F401 from . import test_relation # noqa: F401
from . import test_switch # noqa: F401 from . import test_switch # noqa: F401
# TODO Copyright notice coming up in further commits.
from odoo import tests
from odoo.addons.converter import MailTemplate
class Test(tests.TransactionCase):
"""Test converter that wraps ``mail.template::_render_template``."""
def setUp(self):
super().setUp()
self.user_admin = self.env.ref("base.user_admin")
self.user_demo = self.env.ref("base.user_demo")
def test_mail_template_odoo_to_message(self):
converter = MailTemplate("hello ${ object.login }")
converted = converter.odoo_to_message(self.user_demo)
self.assertEqual(converted, "hello demo")
def test_mail_template_odoo_to_message_multiple_records(self):
converter = MailTemplate(
"hello "
"${ ' & '.join(ctx['records'].sorted('login').mapped('login')) }"
)
converted = converter.odoo_to_message(self.user_admin | self.user_demo)
self.assertEqual(converted, "hello admin & demo")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment