k also send mail

This commit is contained in:
teodoralexandru@nexterp.ro 2021-01-12 16:26:10 +02:00 committed by AlvaroTForgeFlow
parent d5a677dd55
commit 7e70ceb67b
5 changed files with 104 additions and 144 deletions

View File

@ -1 +1 @@
from . import mail_render_mixin from . import mail_render_mixinANDmail_mail

View File

@ -1,78 +0,0 @@
# Copyright 2019 O4SB - Graeme Gellatly
# Copyright 2019 Tecnativa - Ernesto Tejeda
# Copyright 2020 Onestein - Andrea Stirpe
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import re
from lxml import etree
from odoo import api, models
class MailRenderMixin(models.AbstractModel):
_inherit = "mail.render.mixin"
@api.model
def _render_template(
self,
template_src,
model,
res_ids,
engine="jinja",
add_context=None,
post_process=False,
):
"""replace anything that is with odoo in templates
if is a <a that contains odoo will delete it completly
original:
Render the given string on records designed by model / res_ids using
the given rendering engine. Currently only jinja is supported.
:param str template_src: template text to render (jinja) or (qweb)
this could be cleaned but hey, we are in a rush
:param str model: model name of records on which we want to perform rendering
:param list res_ids: list of ids of records (all belonging to same model)
:param string engine: jinja
:param post_process: perform rendered str / html post processing (see
``_render_template_postprocess``)
:return dict: {res_id: string of rendered template based on record}"""
orginal_rendered = super()._render_template(
template_src,
model,
res_ids,
engine="jinja",
add_context=None,
post_process=False,
)
for key in res_ids:
value = orginal_rendered[key]
if len(value) < 20:
continue
has_odoo_link = re.search(r"<a\s(.*)odoo\.com", value, flags=re.IGNORECASE)
if has_odoo_link:
tree = etree.HTML(value) # html with brlken links
# tree = etree.fromstring(value) just xml
odoo_achors = tree.xpath('//a[contains(@href,"odoo.com")]')
for elem in odoo_achors:
parent = elem.getparent()
if (
parent.getparent()
): # anchor <a href odoo has a parent and another one
parent.getparent().remove(parent)
else:
parent.remove(elem)
orginal_rendered[key] = etree.tostring(
tree, pretty_print=True, method="html"
)
if type(orginal_rendered[key]) is str:
orginal_rendered[key] = re.sub(
"[^(<)(</)]odoo", "", orginal_rendered[key], flags=re.IGNORECASE
)
elif type(orginal_rendered[key]) is bytes:
orginal_rendered[key] = re.sub(
b"[^(<)(</)]odoo", b"", orginal_rendered[key], flags=re.IGNORECASE
)
return orginal_rendered

View File

@ -0,0 +1,103 @@
# Copyright 2019 O4SB - Graeme Gellatly
# Copyright 2019 Tecnativa - Ernesto Tejeda
# Copyright 2020 Onestein - Andrea Stirpe
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import re
from lxml import etree, html
from odoo import api, models
class MailRenderMixin(models.AbstractModel):
_inherit = "mail.render.mixin"
def remove_href_odoo(self, value, remove_parent=True, remove_before=False):
if len(value) < 20:
return value
has_odoo_link = re.search(r"<a\s(.*)odoo\.com", value, flags=re.IGNORECASE)
if has_odoo_link:
tree = etree.HTML(
value
) # html with brlken links tree = etree.fromstring(value) just xml
odoo_achors = tree.xpath('//a[contains(@href,"odoo.com")]')
for elem in odoo_achors:
parent = elem.getparent()
previous = elem.getprevious()
if remove_before and not remove_parent and previous:
# remove 'using' that is before <a and after </span>
bytes_text = etree.tostring(
previous, pretty_print=True, method="html"
)
only_what_is_in_tags = bytes_text[: bytes_text.rfind(b">") + 1]
data_formatted = html.fromstring(only_what_is_in_tags)
parent.replace(previous, data_formatted)
if parent.getparent() and remove_parent:
# anchor <a href odoo has a parent powered by that must be removed
parent.getparent().remove(parent)
else:
parent.remove(elem)
value = etree.tostring(tree, pretty_print=True, method="html")
if type(value) is str:
value = re.sub("[^(<)(</)]odoo", "", value, flags=re.IGNORECASE)
elif type(value) is bytes:
value = re.sub(b"[^(<)(</)]odoo", b"", value, flags=re.IGNORECASE)
return value
@api.model
def _render_template(
self,
template_src,
model,
res_ids,
engine="jinja",
add_context=None,
post_process=False,
):
"""replace anything that is with odoo in templates
if is a <a that contains odoo will delete it completly
original:
Render the given string on records designed by model / res_ids using
the given rendering engine. Currently only jinja is supported.
:param str template_src: template text to render (jinja) or (qweb)
this could be cleaned but hey, we are in a rush
:param str model: model name of records on which we want to perform rendering
:param list res_ids: list of ids of records (all belonging to same model)
:param string engine: jinja
:param post_process: perform rendered str / html post processing (see
``_render_template_postprocess``)
:return dict: {res_id: string of rendered template based on record}"""
orginal_rendered = super()._render_template(
template_src,
model,
res_ids,
engine="jinja",
add_context=None,
post_process=False,
)
for key in res_ids:
orginal_rendered[key] = self.remove_href_odoo(orginal_rendered[key])
return orginal_rendered
class MailMail(models.AbstractModel):
_inherit = "mail.mail"
# in messages from objects is adding using Odoo that we are going to remove
@api.model_create_multi
def create(self, values_list):
for index, _value in enumerate(values_list):
values_list[index]["body_html"] = self.env[
"mail.render.mixin"
].remove_href_odoo(
values_list[index]["body_html"], remove_parent=0, remove_before=1
)
return super().create(values_list)

View File

@ -1,51 +0,0 @@
# Copyright 2019 O4SB - Graeme Gellatly
# Copyright 2019 Tecnativa - Ernesto Tejeda
# Copyright 2020 Onestein - Andrea Stirpe
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import re
from lxml import html as htmltree
from odoo import _, api, models
class MailTemplate(models.Model):
_inherit = "mail.template"
@api.model
def _debrand_translated_words(self):
def _get_translated(word):
return self.env["ir.translation"]._get_source(
"ir.ui.view,arch_db", "model_terms", self.env.lang, word
)
odoo_word = _get_translated("Odoo") or _("Odoo")
powered_by = _get_translated("Powered by") or _("Powered by")
using_word = _get_translated("using") or _("using")
return odoo_word, powered_by, using_word
@api.model
def _debrand_body(self, html):
odoo_word, powered_by, using_word = self._debrand_translated_words()
html = re.sub(using_word + "(.*)[\r\n]*(.*)>" + odoo_word + r"</a>", "", html)
if powered_by not in html:
return html
root = htmltree.fromstring(html)
powered_by_elements = root.xpath("//*[text()[contains(.,'%s')]]" % powered_by)
for elem in powered_by_elements:
# make sure it isn't a spurious powered by
if any(
[
"www.odoo.com" in child.get("href", "")
for child in elem.getchildren()
]
):
for child in elem.getchildren():
elem.remove(child)
elem.text = None
return htmltree.tostring(root).decode("utf-8")
@api.model
def render_post_process(self, html):
html = super().render_post_process(html)
return self._debrand_body(html)

View File

@ -1,14 +0,0 @@
# Copyright 2019 Eficent Business and IT Consulting Services S.L.
# Lois Rilo <lois.rilo@eficent.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import models
class MailThread(models.AbstractModel):
_inherit = "mail.thread"
def _replace_local_links(self, html, base_url=None):
html = super()._replace_local_links(html, base_url=base_url)
html_debranded = self.env["mail.template"]._debrand_body(html)
return html_debranded