[FIX] web: Allow XML template attachments

Before this commit: static XML templates could only be defined on the
file system, and called by manifest assets or ir.asset records.
Attachments were not taken into account when evaluating static
templates.

Now, if a given path does not match a file on the system, an
additional check is run on ir.attachment records instead of failing
directly.

Task 2715333

closes odoo/odoo#83438

X-original-commit: e022c4bfafa77f1a3433c3b980631510af696ade
Signed-off-by: Lucas Perais (lpe) <lpe@odoo.com>
Signed-off-by: Julien Mougenot (jum) <jum@odoo.com>
This commit is contained in:
Julien Mougenot
2022-01-26 19:22:57 +00:00
parent 51e6b349e3
commit 0dc347370f
2 changed files with 50 additions and 8 deletions
+20 -7
View File
@@ -491,14 +491,27 @@ class HomeStaticTemplateHelpers(object):
if re.match(COMMENT_PATTERN, comment.text.strip()):
comment.getparent().remove(comment)
def _read_addon_file(self, file_path):
"""Reads the content of a file given by file_path
Usefull to make 'self' testable
:param str file_path:
:returns: str
def _read_addon_file(self, path_or_url):
"""Read the content of a file or an ``ir.attachment`` record given by
``path_or_url``.
:param str path_or_url:
:returns: bytes
:raises FileNotFoundError: if the path does not match a module file
or an attachment
"""
with file_open(file_path, 'rb') as fp:
contents = fp.read()
try:
with file_open(path_or_url, 'rb') as fp:
contents = fp.read()
except FileNotFoundError as e:
attachment = request.env['ir.attachment'].sudo().search([
('url', '=', path_or_url),
('type', '=', 'binary'),
], limit=1)
if attachment:
contents = attachment.raw
else:
raise e
return contents
def _concat_xml(self, file_dict):
+30 -1
View File
@@ -9,7 +9,7 @@ from datetime import datetime, timedelta
from lxml import etree
import logging
from odoo.tests.common import BaseCase, tagged
from odoo.tests.common import BaseCase, HttpCase, tagged
from odoo.tools import topological_sort
from odoo.addons.web.controllers.main import HomeStaticTemplateHelpers
@@ -900,6 +900,35 @@ class TestStaticInheritance(TestStaticInheritanceCommon):
self.assertXMLEqual(contents, expected)
@tagged('static_templates')
class TestHttpStaticInheritance(HttpCase):
def test_static_attachments(self):
url = '/test_module/test_file.xml'
self.env['ir.attachment'].create({
'name': 'test_attachment',
'url': url,
'res_model': 'ir.ui.view',
'type': 'binary',
'raw': b"""
<templates>
<t t-name="test_template">
<div class="test_div" />
</t>
</templates>
"""
})
self.env['ir.asset'].create({
'name': 'test_asset',
'path': url,
'bundle': 'test.bundle',
})
res = self.url_open('/web/webclient/qweb/HASH_BIDON?bundle=test.bundle')
[template] = etree.fromstring(res.text)
self.assertEqual(template.get('t-name'), 'test_template')
self.assertEqual(template[0].get('class'), 'test_div')
@tagged('-standard', 'static_templates_performance')
class TestStaticInheritancePerformance(TestStaticInheritanceCommon):