diff --git a/mail_layout_preview/README.rst b/mail_layout_preview/README.rst new file mode 100644 index 000000000..293342193 --- /dev/null +++ b/mail_layout_preview/README.rst @@ -0,0 +1,80 @@ +============ +Mail Preview +============ + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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/12.0/mail_layout_preview + :alt: OCA/social +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/social-12-0/social-12-0-mail_layout_preview + :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/12.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Allow full layout preview of email templates. + +Odoo alreday allows you do preview emails but they are wrapped into Odoo backend theme. + +This module adds a controller to render only the full layout of the email. + + +NOTE: to make work properly your emails it's recommended to use `mail_inline_css` to include all styles in the body of the email. + +**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 smashing it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Camptocamp SA + +Contributors +~~~~~~~~~~~~ + +* Simone Orsi + +Maintainers +~~~~~~~~~~~ + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +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_layout_preview/__init__.py b/mail_layout_preview/__init__.py new file mode 100644 index 000000000..9b7fdc981 --- /dev/null +++ b/mail_layout_preview/__init__.py @@ -0,0 +1,2 @@ +from . import controllers +from . import wizard diff --git a/mail_layout_preview/__manifest__.py b/mail_layout_preview/__manifest__.py new file mode 100644 index 000000000..a63cb3818 --- /dev/null +++ b/mail_layout_preview/__manifest__.py @@ -0,0 +1,15 @@ +# Copyright 2020 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + "name": "Mail Preview", + "summary": """ + Preview email templates in the browser""", + "version": "13.0.1.0.0", + "license": "AGPL-3", + "author": "Camptocamp SA,Odoo Community Association (OCA)", + "website": "https://github.com/OCA/social", + "depends": ["mail"], + "data": ["templates/email_preview.xml", "wizard/email_template_preview.xml"], + "development_status": "Beta", +} diff --git a/mail_layout_preview/controllers/__init__.py b/mail_layout_preview/controllers/__init__.py new file mode 100644 index 000000000..07dab3c8e --- /dev/null +++ b/mail_layout_preview/controllers/__init__.py @@ -0,0 +1 @@ +from . import layout_preview diff --git a/mail_layout_preview/controllers/layout_preview.py b/mail_layout_preview/controllers/layout_preview.py new file mode 100644 index 000000000..b2e72d85d --- /dev/null +++ b/mail_layout_preview/controllers/layout_preview.py @@ -0,0 +1,47 @@ +# Copyright 2020 Simone Orsi - Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) + +from odoo import http +from odoo.http import request + + +class Preview(http.Controller): + + _list_template = "mail_layout_preview.email_templates_list" + + @http.route( + ["/email-preview/"], type="http", auth="user", website=True + ) + def template_list(self, model, **kw): + env = request.env + templates = env["mail.template"].search([("model_id.model", "=", model)]) + xids = templates.get_external_id() + return request.render( + self._list_template, {"model": model, "templates": templates, "xids": xids} + ) + + @http.route( + ["/email-preview///"], + type="http", + auth="user", + website=True, + ) + def preview(self, model, templ_id, rec_id, **kw): + """Render an email template to verify look and feel. + + Provide model, record id and an email template to render. + + Example for event registration email: + + /email-preview/event.registration/event.event_subscription/5 + """ + env = request.env + record = env[model].browse(rec_id) + if templ_id.isdigit(): + # got an ID + template = env["mail.template"].browse(int(templ_id)) + else: + # got a XID + template = env.ref(templ_id.strip()) + result = template.generate_email(record.id) + return request.make_response(result["body_html"]) diff --git a/mail_layout_preview/readme/CONTRIBUTORS.rst b/mail_layout_preview/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..f583948be --- /dev/null +++ b/mail_layout_preview/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Simone Orsi diff --git a/mail_layout_preview/readme/DESCRIPTION.rst b/mail_layout_preview/readme/DESCRIPTION.rst new file mode 100644 index 000000000..bf89037bb --- /dev/null +++ b/mail_layout_preview/readme/DESCRIPTION.rst @@ -0,0 +1,6 @@ +The purpose of this module is to help development of email templates +allowing to render the full layout preview of each of them. + +Odoo already allows you do preview emails but they are wrapped into Odoo backend theme. + +NOTE: to make work properly your emails it's strongly recommended to use `mail_inline_css` to include all styles in the body of the email. diff --git a/mail_layout_preview/readme/USAGE.rst b/mail_layout_preview/readme/USAGE.rst new file mode 100644 index 000000000..26448e954 --- /dev/null +++ b/mail_layout_preview/readme/USAGE.rst @@ -0,0 +1,10 @@ +* Go to a template and click on the preview button +* Select desired params and click on "Full layout preview" link +* You'll get to the preview + +As a developer, you can see a list of all templates for a model by going to: + + /email-preview/$model.name + +You'll get a list of all the templates. +Follow the instructions there on how to use the links. diff --git a/mail_layout_preview/static/description/icon.png b/mail_layout_preview/static/description/icon.png new file mode 100644 index 000000000..3a0328b51 Binary files /dev/null and b/mail_layout_preview/static/description/icon.png differ diff --git a/mail_layout_preview/static/description/index.html b/mail_layout_preview/static/description/index.html new file mode 100644 index 000000000..00ad7d03b --- /dev/null +++ b/mail_layout_preview/static/description/index.html @@ -0,0 +1,422 @@ + + + + + + +Mail Preview + + + +
+

Mail Preview

+ + +

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

+

Allow full layout preview of email templates.

+

Odoo alreday allows you do preview emails but they are wrapped into Odoo backend theme.

+

This module adds a controller to render only the full layout of the email.

+

NOTE: to make work properly your emails it’s recommended to use mail_inline_css to include all styles in the body of the email.

+

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

+
    +
  • Camptocamp SA
  • +
+
+ +
+

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_layout_preview/templates/email_preview.xml b/mail_layout_preview/templates/email_preview.xml new file mode 100644 index 000000000..bf6d311e7 --- /dev/null +++ b/mail_layout_preview/templates/email_preview.xml @@ -0,0 +1,47 @@ + + + + + + diff --git a/mail_layout_preview/tests/__init__.py b/mail_layout_preview/tests/__init__.py new file mode 100644 index 000000000..365703cfa --- /dev/null +++ b/mail_layout_preview/tests/__init__.py @@ -0,0 +1 @@ +from . import test_layout_preview diff --git a/mail_layout_preview/tests/test_layout_preview.py b/mail_layout_preview/tests/test_layout_preview.py new file mode 100644 index 000000000..bdfa1ef78 --- /dev/null +++ b/mail_layout_preview/tests/test_layout_preview.py @@ -0,0 +1,77 @@ +# Copyright 2020 Camptocamp SA (http://www.camptocamp.com) +# Simone Orsi +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from lxml import etree + +from odoo import tools +from odoo.tests.common import HttpCase, SavepointCase, tagged + + +class TestLayoutMixin(object): + @staticmethod + def _create_template(env, model, **kw): + vals = { + "name": "Test Preview Template", + "subject": "Preview ${object.name}", + "body_html": "

Hello ${object.name}

", + "model_id": env["ir.model"]._get(model).id, + "user_signature": False, + } + vals.update(kw) + return env["mail.template"].create(vals) + + +@tagged("-at_install", "post_install") +class TestLayoutPreview(SavepointCase, TestLayoutMixin): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True)) + cls.wiz_model = cls.env["email_template.preview"] + cls.partner = cls.env.ref("base.res_partner_4") + cls.tmpl = cls._create_template(cls.env, cls.partner._name) + + def test_wizard_preview_url(self): + wiz = self.wiz_model.with_context( + template_id=self.tmpl.id, default_res_id=self.partner.id + ).create({}) + self.assertEqual( + wiz.layout_preview_url, + "/email-preview/res.partner/{}/{}/".format(self.tmpl.id, self.partner.id), + ) + + +@tagged("-at_install", "post_install") +class TestController(HttpCase, TestLayoutMixin): + @classmethod + def setUpClass(cls): + super().setUpClass() + host = "127.0.0.1" + port = tools.config["http_port"] + cls.base_url = "http://%s:%d/email-preview/" % (host, port) + + def test_controller1(self): + self.authenticate("admin", "admin") + model = "res.partner" + response = self.url_open(self.base_url + model) + content = response.content + tree = etree.fromstring(content) + list_items = tree.xpath("//ol[@class='email-template-list']/li/a") + templates = self.env["mail.template"].search([("model_id.model", "=", model)]) + url_pattern = "/email-preview/res.partner/mail.email_template_partner/{}" + for el, tmpl in zip(list_items, templates): + self.assertEqual( + el.attrib, {"class": "preview", "href": url_pattern.format(tmpl.id)} + ) + + def test_controller2(self): + self.authenticate("admin", "admin") + partner = self.env.ref("base.res_partner_4") + model = partner._name + tmpl = self._create_template(self.env, model) + response = self.url_open( + self.base_url + "{}/{}/{}/".format(model, tmpl.id, partner.id) + ) + content = response.content.decode() + self.assertIn("

Hello {}

".format(partner.name), content) diff --git a/mail_layout_preview/wizard/__init__.py b/mail_layout_preview/wizard/__init__.py new file mode 100644 index 000000000..134743a98 --- /dev/null +++ b/mail_layout_preview/wizard/__init__.py @@ -0,0 +1 @@ +from . import email_template_preview diff --git a/mail_layout_preview/wizard/email_template_preview.py b/mail_layout_preview/wizard/email_template_preview.py new file mode 100644 index 000000000..ee22b15c4 --- /dev/null +++ b/mail_layout_preview/wizard/email_template_preview.py @@ -0,0 +1,26 @@ +# Copyright 2020 Simone Orsi - Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) + +from odoo import api, fields, models + + +class TemplatePreview(models.TransientModel): + _inherit = "email_template.preview" + + _url_pattern = "/email-preview/{model}/{templ_id}/{rec_id}/" + + layout_preview_url = fields.Char( + string="Full layout preview", compute="_compute_layout_preview_url" + ) + + @api.depends("res_id") + def _compute_layout_preview_url(self): + for rec in self: + if self.env.context.get("template_id"): + rec.layout_preview_url = self._url_pattern.format( + model=rec.model_id.model, + templ_id=self.env.context["template_id"], + rec_id=rec.res_id, + ) + else: + rec.layout_preview_url = "" diff --git a/mail_layout_preview/wizard/email_template_preview.xml b/mail_layout_preview/wizard/email_template_preview.xml new file mode 100644 index 000000000..8070a1a0a --- /dev/null +++ b/mail_layout_preview/wizard/email_template_preview.xml @@ -0,0 +1,14 @@ + + + + email_template.preview + + + + + + diff --git a/setup/mail_layout_preview/odoo/addons/mail_layout_preview b/setup/mail_layout_preview/odoo/addons/mail_layout_preview new file mode 120000 index 000000000..d42a97eb0 --- /dev/null +++ b/setup/mail_layout_preview/odoo/addons/mail_layout_preview @@ -0,0 +1 @@ +../../../../mail_layout_preview \ No newline at end of file diff --git a/setup/mail_layout_preview/setup.py b/setup/mail_layout_preview/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/mail_layout_preview/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)