forked from OCA/website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathir_ui_view.py
More file actions
60 lines (50 loc) · 1.84 KB
/
Copy pathir_ui_view.py
File metadata and controls
60 lines (50 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Copyright 2026 Hunki Enterprises BV
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3.0)
from lxml import html
from odoo import models
class IrUiView(models.Model):
_inherit = "ir.ui.view"
def save(self, value, xpath=None):
arch = html.fromstring(value, parser=html.HTMLParser(encoding="utf-8"))
value = html.tostring(self._web_editor_clean_url_clean_arch(arch))
return super().save(value, xpath)
def _web_editor_clean_url_clean_arch(self, arch, configurations=None):
"""
Clean all urls in given arch
"""
for xpath, attributes in self._web_editor_clean_url_selectors():
for element in arch.xpath(xpath):
self._web_editor_clean_url_clean_element(
element, attributes, configurations=configurations
)
return arch
def _web_editor_clean_url_selectors(self):
"""
Return xpaths for to find elements with urls to be cleaned and the attributes
containing them
"""
return [
("//a[@href]", ("href",)),
]
def _web_editor_clean_url_clean_element(
self, element, attributes, configurations=None
):
"""
Clean urls of element found in attributes
"""
for attribute in attributes:
if not element.get(attribute):
continue
element.attrib[attribute] = self._web_editor_clean_url(
element.attrib[attribute],
configurations=configurations,
)
def _web_editor_clean_url(self, url, configurations=None):
"""
Clean url
"""
for configuration in configurations or self.env[
"web.editor.clean.url.configuration"
].sudo().search([]):
url = configuration.clean_url(url)
return url