Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADD] mail_brand: add brand to mails #138

Open
wants to merge 11 commits into
base: 15.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ addon | version | maintainers | summary
[brand_external_report_layout](brand_external_report_layout/) | 15.0.1.0.0 | [![sbejaoui](https://github.com/sbejaoui.png?size=30px)](https://github.com/sbejaoui) | This module allows you to have a different layout by brand for your external reports.
[contract_brand](contract_brand/) | 15.0.1.0.0 | [![sbejaoui](https://github.com/sbejaoui.png?size=30px)](https://github.com/sbejaoui) | This module allows you to manage branded contracts. It adds a brand field on the contract and propagate the value on the invoices.
[product_brand](product_brand/) | 15.0.1.0.0 | | Product Brand Manager
[sale_brand](sale_brand/) | 15.0.1.0.2 | [![osi-scampbell](https://github.com/osi-scampbell.png?size=30px)](https://github.com/osi-scampbell) [![sbejaoui](https://github.com/sbejaoui.png?size=30px)](https://github.com/sbejaoui) | Send branded sales orders
[sale_brand](sale_brand/) | 15.0.1.0.3 | [![osi-scampbell](https://github.com/osi-scampbell.png?size=30px)](https://github.com/osi-scampbell) [![sbejaoui](https://github.com/sbejaoui.png?size=30px)](https://github.com/sbejaoui) | Send branded sales orders

[//]: # (end addons)

Expand Down
84 changes: 84 additions & 0 deletions mail_brand/README.rst
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/OCA/brand/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 <https://github.com/OCA/brand/issues/new?body=module:%20mail_brand%0Aversion:%2015.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

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

Credits
=======

Authors
~~~~~~~

* Gert Pellin / Snakebyte Development

Contributors
~~~~~~~~~~~~

* Gert Pellin <[email protected]>

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 <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-switch87|

This module is part of the `OCA/brand <https://github.com/OCA/brand/tree/15.0/mail_brand>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
2 changes: 2 additions & 0 deletions mail_brand/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import controllers
from . import models
20 changes: 20 additions & 0 deletions mail_brand/__manifest__.py
Original file line number Diff line number Diff line change
@@ -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"],
}
1 change: 1 addition & 0 deletions mail_brand/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import main
69 changes: 69 additions & 0 deletions mail_brand/controllers/main.py
Original file line number Diff line number Diff line change
@@ -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)
58 changes: 58 additions & 0 deletions mail_brand/data/mail_template.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<template
id="message_notification_email_brand"
inherit_id="mail.message_notification_email"
priority="100"
>
<xpath expr="//tbody//img" position="replace">
<img
t-att-src="'/logo.png?company=%s&amp;has_brand=%i' % (company.id or 0, has_brand or 0)"
style="padding: 0px; margin: 0px; height: auto; max-width: 200px; max-height: 36px;"
t-att-alt="'%s' % company.name"
/>
</xpath>
</template>

<template
id="mail_notification_borders_brand"
inherit_id="mail.mail_notification_borders"
priority="100"
>
<xpath expr="//tbody//img" position="replace">
<img
t-att-src="'/logo.png?company=%s&amp;has_brand=%i' % (company.id or 0, has_brand or 0)"
style="padding: 0px; margin: 0px; height: auto; width: 80px;"
t-att-alt="'%s' % company.name"
/>
</xpath>
</template>

<template
id="mail_notification_light_brand"
inherit_id="mail.mail_notification_light"
priority="100"
>
<xpath expr="//tbody//img" position="replace">
<img
t-att-src="'/logo.png?company=%s&amp;has_brand=%i' % (company.id or 0, has_brand or 0)"
style="padding: 0px; margin: 0px; height: 48px;"
t-att-alt="'%s' % company.name"
/>
</xpath>
</template>

<template
id="mail_notification_paynow_brand"
inherit_id="mail.mail_notification_paynow"
priority="100"
>
<xpath expr="//tbody//img" position="replace">
<img
t-att-src="'/logo.png?company=%s&amp;has_brand=%i' % (company.id or 0, has_brand or 0)"
style="padding: 0px; margin: 0px; height: 48px;"
t-att-alt="'%s' % company.name"
/>
</xpath>
</template>
</odoo>
3 changes: 3 additions & 0 deletions mail_brand/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import ir_model
from . import res_brand
from . import mail_thread
16 changes: 16 additions & 0 deletions mail_brand/models/ir_model.py
Original file line number Diff line number Diff line change
@@ -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()
29 changes: 29 additions & 0 deletions mail_brand/models/mail_thread.py
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions mail_brand/models/res_brand.py
Original file line number Diff line number Diff line change
@@ -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))
1 change: 1 addition & 0 deletions mail_brand/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Gert Pellin <[email protected]>
Empty file added mail_brand/readme/CREDITS.rst
Empty file.
4 changes: 4 additions & 0 deletions mail_brand/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -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
Empty file added mail_brand/readme/USAGE.rst
Empty file.
Loading