[FIX] mail_debrand: remove_href_odoo function

- FIX bug when split email message by `to_keep` parameter. When the html code is split, it is broken. As a result of this, when it use `fromstring`,
it try to fix this by closing tags.
Example about fixed functionality:

message:
`
<table>
   <tr>
      <td>
         {to_keep}
      </td>
   </tr>
</table>
`
Split messsage inside `remove_href_odoo` function:

part[0]

`
<table>
   <tr>
      <td>
`
--------
part[1]
`
      </td>
   </tr>
</table>
`
-----------------
Result when join by `to_keep`

`
<table>
<tr>
<td>
{to_keep}
</td>
</tr>
</table>
`
This commit is contained in:
miguels73 2022-05-23 18:29:08 +02:00 committed by Jairo Llopis
parent c1503fb3f0
commit af53ccf57c
No known key found for this signature in database
GPG Key ID: E47E3BE44B940490

View File

@ -26,37 +26,28 @@ class MailRenderMixin(models.AbstractModel):
# We don't want to change what was explicitly added in the message body, # We don't want to change what was explicitly added in the message body,
# so we will only change what is before and after it. # so we will only change what is before and after it.
if to_keep: if to_keep:
to_change = value.split(to_keep) value = value.replace(to_keep, "<body_msg></body_msg>")
else: tree = html.fromstring(value)
to_change = [value] odoo_anchors = tree.xpath('//a[contains(@href,"odoo.com")]')
to_keep = "" for elem in odoo_anchors:
new_parts = [] parent = elem.getparent()
for part in to_change: previous = elem.getprevious()
tree = html.fromstring(part) if remove_before and not remove_parent and previous is not None:
if tree is None: # remove 'using' that is before <a and after </span>
new_parts.append(part) previous.tail = ""
continue if remove_parent and len(parent.getparent()):
odoo_anchors = tree.xpath('//a[contains(@href,"odoo.com")]') # anchor <a href odoo has a parent powered by that must be removed
for elem in odoo_anchors: parent.getparent().remove(parent)
parent = elem.getparent() else:
previous = elem.getprevious() if parent.tag == "td": # also here can be powered by
if remove_before and not remove_parent and previous is not None:
# remove 'using' that is before <a and after </span>
previous.tail = ""
if remove_parent and len(parent.getparent()):
# anchor <a href odoo has a parent powered by that must be removed
parent.getparent().remove(parent) parent.getparent().remove(parent)
else: else:
if parent.tag == "td": # also here can be powered by parent.remove(elem)
parent.getparent().remove(parent) value = etree.tostring(
else: tree, pretty_print=True, method="html", encoding="unicode"
parent.remove(elem) )
part = etree.tostring( if to_keep:
tree, pretty_print=True, method="html", encoding="unicode" value = value.replace("<body_msg></body_msg>", to_keep)
)
new_parts.append(part)
value = str(to_keep).join(new_parts)
return value return value
@api.model @api.model