diff --git a/mail_inline_css/README.rst b/mail_inline_css/README.rst index 3b362ebe..5e9b5f7a 100644 --- a/mail_inline_css/README.rst +++ b/mail_inline_css/README.rst @@ -1,109 +1,80 @@ -.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg - :target: http://www.gnu.org/licenses/agpl - :alt: License: AGPL-3 - =============== Mail Inline CSS =============== -When you send HTML emails you can't use style tags but instead you have -to put inline ``style`` attributes on every element. So from this: - -.. code:: html - - - -

Peter

-

Hej

- - -You want this: - -.. code:: html - - -

Peter

-

Hej

- - -This module use premailer library to do this. - -It parses an HTML page, looks up ``style`` blocks -and parses the CSS. It then uses the ``lxml.html`` parser to modify the -DOM tree of the page accordingly. - -Installation -============ - -To install this module, you need first to install `premailer` python library with: - -.. code:: bash - - pip install premailer - - -Usage -===== +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -Just use any mail template as Odoo standard feature +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fsocial-lightgray.png?logo=github + :target: https://github.com/OCA/social/tree/11.0/mail_inline_css + :alt: OCA/social +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/social-11-0/social-11-0-mail_inline_css + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/205/11.0 + :alt: Try me on Runbot -.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas - :alt: Try me on Runbot - :target: https://runbot.odoo-community.org/runbot/205/11 +|badge1| |badge2| |badge3| |badge4| |badge5| +Convert styles to inline styles and make them play nice with emails. -Note: - - Odoo with module web_editor already implements this feature on the client side (js). - This module brings this server side feature for cases without js part. It could the more stable way over the Odoo versions with a stable api in a dedicated library - with adhoc python unit tests. +This module sole usage is to provide the same parsing functionality as in +the web editor but for the templates imported directly in database. +**Table of contents** +.. contents:: + :local: Bug Tracker =========== -Bugs are tracked on `GitHub Issues -`_. In case of trouble, please -check there if your issue has already been reported. If you spotted it first, -help us smash it by providing detailed and welcomed feedback. +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. Credits ======= -Images ------- +Authors +~~~~~~~ -* Odoo Community Association: `Icon `_. +* Akretion +* camptocamp Contributors ------------- +~~~~~~~~~~~~ * David BEAL +* Akim Juillerat +* Simone Orsi +* Patrick Tombez -Do not contact contributors directly about support or help with technical issues. - -Funders -------- - -The development of this module has been financially supported by: +Maintainers +~~~~~~~~~~~ -* Akretion - -Maintainer ----------- +This module is maintained by the OCA. .. image:: https://odoo-community.org/logo.png :alt: Odoo Community Association :target: https://odoo-community.org -This module is maintained by the OCA. - OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use. -To contribute to this module, please visit https://odoo-community.org. +This module is part of the `OCA/social `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/mail_inline_css/__manifest__.py b/mail_inline_css/__manifest__.py index c4e87d31..32da0bb9 100644 --- a/mail_inline_css/__manifest__.py +++ b/mail_inline_css/__manifest__.py @@ -1,17 +1,26 @@ # Copyright 2017 David BEAL @ Akretion -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +# Copyright 2019 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). { 'name': 'Mail Inline CSS', "summary": "Convert style tags in inline style in your mails", 'version': '11.0.1.0.0', - 'author': 'Akretion, Odoo Community Association (OCA)', + 'author': 'Akretion, camptocamp, Odoo Community Association (OCA)', 'website': 'https://github.com/OCA/social', 'license': 'AGPL-3', 'category': 'Tools', - 'depends': ['mail'], 'installable': True, "external_dependencies": { "python": ['premailer'], }, + "depends": [ + "email_template_qweb", + ], + "data": [ + ], + "demo": [ + "demo/demo_template.xml", + "demo/demo_mail_template.xml", + ], } diff --git a/mail_inline_css/demo/demo_mail_template.xml b/mail_inline_css/demo/demo_mail_template.xml new file mode 100644 index 00000000..2a6d5f70 --- /dev/null +++ b/mail_inline_css/demo/demo_mail_template.xml @@ -0,0 +1,10 @@ + + + + Inline styles demo + qweb + + + Demo email inline styles + + diff --git a/mail_inline_css/demo/demo_template.xml b/mail_inline_css/demo/demo_template.xml new file mode 100644 index 00000000..3b80f533 --- /dev/null +++ b/mail_inline_css/demo/demo_template.xml @@ -0,0 +1,84 @@ + + + + diff --git a/mail_inline_css/models/__init__.py b/mail_inline_css/models/__init__.py index 37a9875b..44e83956 100644 --- a/mail_inline_css/models/__init__.py +++ b/mail_inline_css/models/__init__.py @@ -1 +1 @@ -from . import mail +from . import mail_template diff --git a/mail_inline_css/models/mail.py b/mail_inline_css/models/mail.py deleted file mode 100644 index a941a939..00000000 --- a/mail_inline_css/models/mail.py +++ /dev/null @@ -1,22 +0,0 @@ -# Copyright 2017 David BEAL @ Akretion -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). - -import logging -from odoo import models - -_logger = logging.getLogger(__name__) - -try: - from premailer import transform -except (ImportError, IOError) as err: - _logger.debug(err) - - -class MailTemplate(models.Model): - _inherit = 'mail.template' - - def generate_email(self, res_ids, fields=None): - res = super().generate_email(res_ids, fields=fields) - if 'body_html' in res: - res['body_html'] = transform(res['body_html']) - return res diff --git a/mail_inline_css/models/mail_template.py b/mail_inline_css/models/mail_template.py new file mode 100644 index 00000000..9bb40311 --- /dev/null +++ b/mail_inline_css/models/mail_template.py @@ -0,0 +1,28 @@ +# Copyright 2017 David BEAL @ Akretion +# Copyright 2019 Camptocamp SA + +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from odoo import api, models + +try: + from premailer import transform +except (ImportError, IOError) as err: + import logging + _logger = logging.getLogger(__name__) + _logger.debug(err) + + +class MailTemplate(models.Model): + _inherit = 'mail.template' + + @api.multi + def generate_email(self, res_ids, fields=None): + """Use `premailer` to convert styles to inline styles.""" + result = super().generate_email(res_ids, fields=fields) + if isinstance(res_ids, int): + result['body_html'] = transform(result['body_html']) + else: + for __, data in result.items(): + data['body_html'] = transform(data['body_html']) + return result diff --git a/mail_inline_css/readme/CONTRIBUTORS.rst b/mail_inline_css/readme/CONTRIBUTORS.rst new file mode 100644 index 00000000..fcd33779 --- /dev/null +++ b/mail_inline_css/readme/CONTRIBUTORS.rst @@ -0,0 +1,4 @@ +* David BEAL +* Akim Juillerat +* Simone Orsi +* Patrick Tombez diff --git a/mail_inline_css/readme/DESCRIPTION.rst b/mail_inline_css/readme/DESCRIPTION.rst new file mode 100644 index 00000000..30957362 --- /dev/null +++ b/mail_inline_css/readme/DESCRIPTION.rst @@ -0,0 +1,4 @@ +Convert styles to inline styles and make them play nice with emails. + +This module sole usage is to provide the same parsing functionality as in +the web editor but for the templates imported directly in database. diff --git a/mail_inline_css/static/description/index.html b/mail_inline_css/static/description/index.html new file mode 100644 index 00000000..533c0879 --- /dev/null +++ b/mail_inline_css/static/description/index.html @@ -0,0 +1,425 @@ + + + + + + +Mail Inline CSS + + + +
+

Mail Inline CSS

+ + +

Beta License: AGPL-3 OCA/social Translate me on Weblate Try me on Runbot

+

Convert styles to inline styles and make them play nice with emails.

+

This module sole usage is to provide the same parsing functionality as in +the web editor but for the templates imported directly in database.

+

Table of contents

+ +
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Akretion
  • +
  • camptocamp
  • +
+
+ +
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

This module is part of the OCA/social project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/mail_inline_css/tests/__init__.py b/mail_inline_css/tests/__init__.py new file mode 100644 index 00000000..46429763 --- /dev/null +++ b/mail_inline_css/tests/__init__.py @@ -0,0 +1 @@ +from . import test_mail_inline_styles diff --git a/mail_inline_css/tests/test_mail_inline_styles.py b/mail_inline_css/tests/test_mail_inline_styles.py new file mode 100644 index 00000000..96e73bc1 --- /dev/null +++ b/mail_inline_css/tests/test_mail_inline_styles.py @@ -0,0 +1,62 @@ +# Copyright 2019 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from lxml import html +from odoo.tests import SavepointCase + + +class TestMailInlineStyles(SavepointCase): + + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.mail_template = cls.env.ref( + 'mail_inline_css.email_template_demo') + cls.demo_user = cls.env.ref('base.user_demo') + + def to_xml_node(self, html_): + return html.fragments_fromstring(html_) + + def parse_node_style(self, node): + """ Convert node CSS string to Python dict""" + res = {} + for style in node.attrib.get('style', '').split(';'): + l = style.split(':') + res[l[0].strip()] = l[1].strip() + return res + + def find_by_id(self, node, html_id): + return node.xpath('//*[@id="{}"]'.format(html_id)) + + def assertNodeStyle(self, node, expected): + self.assertIn('style', node.attrib) + self.assertEqual(self.parse_node_style(node), expected) + + def test_generate_mail(self): + res = self.mail_template.generate_email( + [self.demo_user.id], fields=['body_html'] + ) + body_html_string = res[self.demo_user.id].get('body_html') + html_node = self.to_xml_node(body_html_string)[0] + + expected = { + 'main_logo': { + 'max-width': '300px' + }, + 'main_wrapper': { + 'max-width': '620px', + 'margin': '0 auto', + 'border': '1px solid #ccc', + 'font-size': '18px', + 'font-family': 'verdana', + 'color': '#6B6E71' + }, + 'main_footer': { + 'padding-top': '0', + 'font-size': '120%', + 'padding': '30px 40px' + } + } + + for html_id, expected_style in expected.items(): + node = self.find_by_id(html_node, html_id)[0] + self.assertNodeStyle(node, expected_style)