From 14f6fd73c47573029e4b4f9e7e1335001defda92 Mon Sep 17 00:00:00 2001 From: Gert Pellin Date: Mon, 10 Oct 2022 16:01:52 +0200 Subject: [PATCH] [ADD] mail_brand: add brand to mails If mails are send with a brand assigned (for instanse from a sales order) the brand info will be shown instead of the company info. --- mail_brand/README.rst | 84 +++++ mail_brand/__init__.py | 2 + mail_brand/__manifest__.py | 20 ++ mail_brand/controllers/__init__.py | 1 + mail_brand/controllers/main.py | 69 ++++ mail_brand/data/mail_template.xml | 56 +++ mail_brand/models/__init__.py | 3 + mail_brand/models/ir_model.py | 16 + mail_brand/models/mail_thread.py | 29 ++ mail_brand/models/res_brand.py | 17 + mail_brand/readme/CONTRIBUTORS.rst | 1 + mail_brand/readme/CREDITS.rst | 0 mail_brand/readme/DESCRIPTION.rst | 4 + mail_brand/readme/USAGE.rst | 0 mail_brand/static/description/index.html | 423 +++++++++++++++++++++++ 15 files changed, 725 insertions(+) create mode 100644 mail_brand/README.rst create mode 100644 mail_brand/__init__.py create mode 100644 mail_brand/__manifest__.py create mode 100644 mail_brand/controllers/__init__.py create mode 100644 mail_brand/controllers/main.py create mode 100644 mail_brand/data/mail_template.xml create mode 100644 mail_brand/models/__init__.py create mode 100644 mail_brand/models/ir_model.py create mode 100644 mail_brand/models/mail_thread.py create mode 100644 mail_brand/models/res_brand.py create mode 100644 mail_brand/readme/CONTRIBUTORS.rst create mode 100644 mail_brand/readme/CREDITS.rst create mode 100644 mail_brand/readme/DESCRIPTION.rst create mode 100644 mail_brand/readme/USAGE.rst create mode 100644 mail_brand/static/description/index.html diff --git a/mail_brand/README.rst b/mail_brand/README.rst new file mode 100644 index 000000000..c04ae3f8a --- /dev/null +++ b/mail_brand/README.rst @@ -0,0 +1,84 @@ +=========== +Email Brand +=========== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! 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%2Fbrand-lightgray.png?logo=github + :target: https://github.com/OCA/brand/tree/15.0/mail_brand + :alt: OCA/brand +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/brand-15-0/brand-15-0-mail_brand + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/284/15.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +If a model has a brand defined to it, emails send from this model will be branded accordingly. + +If the brand module gets implemented more broadly this module could need extension to work properly, because not all +odoo modules use the same mail templates + +**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 +~~~~~~~ + +* Gert Pellin / Snakebyte Development + +Contributors +~~~~~~~~~~~~ + +* Gert Pellin + +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. + +.. |maintainer-switch87| image:: https://github.com/switch87.png?size=40px + :target: https://github.com/switch87 + :alt: switch87 + +Current `maintainer `__: + +|maintainer-switch87| + +This module is part of the `OCA/brand `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/mail_brand/__init__.py b/mail_brand/__init__.py new file mode 100644 index 000000000..91c5580fe --- /dev/null +++ b/mail_brand/__init__.py @@ -0,0 +1,2 @@ +from . import controllers +from . import models diff --git a/mail_brand/__manifest__.py b/mail_brand/__manifest__.py new file mode 100644 index 000000000..d6b35ca4a --- /dev/null +++ b/mail_brand/__manifest__.py @@ -0,0 +1,20 @@ +# Copyright 2022 Snakebyte +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + "name": "Email Brand", + "summary": """ + If a model has a brand defined to it, emails send from this model will be + branded accordingly. If the brand module gets implemented more broadly this + module could need extension to work properly. + """, + "license": "AGPL-3", + "author": "Odoo Community Association (OCA), Gert Pellin / Snakebyte Development", + "website": "https://github.com/OCA/brand", + "version": "15.0.1.0.1", + "depends": ["mail", "brand"], + "data": [ + "data/mail_template.xml", + ], + "maintainers": ["switch87"], +} diff --git a/mail_brand/controllers/__init__.py b/mail_brand/controllers/__init__.py new file mode 100644 index 000000000..12a7e529b --- /dev/null +++ b/mail_brand/controllers/__init__.py @@ -0,0 +1 @@ +from . import main diff --git a/mail_brand/controllers/main.py b/mail_brand/controllers/main.py new file mode 100644 index 000000000..defa12076 --- /dev/null +++ b/mail_brand/controllers/main.py @@ -0,0 +1,69 @@ +# Copyright (C) 2022 Snakebyte +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +import base64 +import functools +import io + +import odoo +from odoo import http +from odoo.modules import get_resource_path +from odoo.tools.mimetypes import guess_mimetype + +from odoo.addons.web.controllers.main import Binary + + +class BrandBinary(Binary): + @http.route() + def company_logo(self, dbname=None, **kw): + response = False + imgname = "logo" + imgext = ".png" + uid = None + placeholder = functools.partial(get_resource_path, "web", "static", "img") + if http.request.session.db: + dbname = http.request.session.db + uid = http.request.session.uid + elif dbname is None: + dbname = http.db_monodb() + if not uid: + uid = odoo.SUPERUSER_ID + if not dbname: + response = http.send_file(placeholder(imgname + imgext)) + else: + try: + has_brand = ( + int(kw["has_brand"]) if kw and kw.get("has_brand") else False + ) + if has_brand: + registry = odoo.modules.registry.Registry(dbname) + with registry.cursor() as cr: + brand = int(kw["company"]) + if brand: + cr.execute( + """ + SELECT logo_web, write_date + FROM res_brand + WHERE id = %s + """, + (brand,), + ) + row = cr.fetchone() + if row and row[0]: + image_base64 = base64.b64decode(row[0]) + image_data = io.BytesIO(image_base64) + mimetype = guess_mimetype( + image_base64, default="image/png" + ) + imgext = "." + mimetype.split("/")[1] + if imgext == ".svg+xml": + imgext = ".svg" + response = http.send_file( + image_data, + filename=imgname + imgext, + mimetype=mimetype, + mtime=row[1], + ) + except Exception: + response = http.send_file(placeholder(imgname + imgext)) + return response or super().company_logo(dbname, **kw) diff --git a/mail_brand/data/mail_template.xml b/mail_brand/data/mail_template.xml new file mode 100644 index 000000000..3b737e117 --- /dev/null +++ b/mail_brand/data/mail_template.xml @@ -0,0 +1,56 @@ + + + + + + + + + + + + diff --git a/mail_brand/models/__init__.py b/mail_brand/models/__init__.py new file mode 100644 index 000000000..1fa1f3074 --- /dev/null +++ b/mail_brand/models/__init__.py @@ -0,0 +1,3 @@ +from . import ir_model +from . import res_brand +from . import mail_thread diff --git a/mail_brand/models/ir_model.py b/mail_brand/models/ir_model.py new file mode 100644 index 000000000..0e38a1123 --- /dev/null +++ b/mail_brand/models/ir_model.py @@ -0,0 +1,16 @@ +# Copyright (C) 2022 Snakebyte +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import models + + +class BaseModel(models.AbstractModel): + _inherit = "base" + + def get_base_url(self): + if not self: + return super().get_base_url() + if "brand_id" in self and "website_id" in self.brand_id: + return self.brand_id.website_website_id.domain + else: + return super().get_base_url() diff --git a/mail_brand/models/mail_thread.py b/mail_brand/models/mail_thread.py new file mode 100644 index 000000000..92fbc913a --- /dev/null +++ b/mail_brand/models/mail_thread.py @@ -0,0 +1,29 @@ +# Copyright (C) 2022 Snakebyte +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import api, models + + +class MailThread(models.AbstractModel): + _inherit = "mail.thread" + + @api.model + def _notify_prepare_template_context( + self, message, msg_vals, model_description=False, mail_auto_delete=True + ): + result = super()._notify_prepare_template_context( + message, msg_vals, model_description, mail_auto_delete + ) + + result["has_brand"] = hasattr(self, "brand_id") + if result["has_brand"]: + result["company"] = self.brand_id + if self.brand_id.website: + result["website_url"] = ( + "https://%s" % self.brand_id.website + if not self.brand_id.website.lower().startswith(("http:", "https:")) + else self.brand_id.website + ) + else: + result["website_url"] = False + return result diff --git a/mail_brand/models/res_brand.py b/mail_brand/models/res_brand.py new file mode 100644 index 000000000..bb2f14dc5 --- /dev/null +++ b/mail_brand/models/res_brand.py @@ -0,0 +1,17 @@ +# Copyright 2022 Snakebyte +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import api, fields, models +from odoo.tools import image_process + + +class ResBrand(models.Model): + + _inherit = "res.brand" + + logo_web = fields.Binary(compute="_compute_logo_web", store=True, attachment=False) + + @api.depends("partner_id.image_1920") + def _compute_logo_web(self): + for brand in self: + brand.logo_web = image_process(brand.partner_id.image_1920, size=(180, 0)) diff --git a/mail_brand/readme/CONTRIBUTORS.rst b/mail_brand/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..aeac835e7 --- /dev/null +++ b/mail_brand/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Gert Pellin diff --git a/mail_brand/readme/CREDITS.rst b/mail_brand/readme/CREDITS.rst new file mode 100644 index 000000000..e69de29bb diff --git a/mail_brand/readme/DESCRIPTION.rst b/mail_brand/readme/DESCRIPTION.rst new file mode 100644 index 000000000..5a12320b1 --- /dev/null +++ b/mail_brand/readme/DESCRIPTION.rst @@ -0,0 +1,4 @@ +If a model has a brand defined to it, emails send from this model will be branded accordingly. + +If the brand module gets implemented more broadly this module could need extension to work properly, because not all +odoo modules use the same mail templates diff --git a/mail_brand/readme/USAGE.rst b/mail_brand/readme/USAGE.rst new file mode 100644 index 000000000..e69de29bb diff --git a/mail_brand/static/description/index.html b/mail_brand/static/description/index.html new file mode 100644 index 000000000..7b938387b --- /dev/null +++ b/mail_brand/static/description/index.html @@ -0,0 +1,423 @@ + + + + + + +Email Brand + + + +
+

Email Brand

+ + +

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

+

If a model has a brand defined to it, emails send from this model will be branded accordingly.

+

If the brand module gets implemented more broadly this module could need extension to work properly, because not all +odoo modules use the same mail templates

+

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

+
    +
  • Gert Pellin / Snakebyte Development
  • +
+
+
+

Contributors

+ +
+
+

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.

+

Current maintainer:

+

switch87

+

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

+

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

+
+
+
+ +