diff --git a/addons/web/controllers/main.py b/addons/web/controllers/main.py index 83b59fc60a1..47f487d1142 100644 --- a/addons/web/controllers/main.py +++ b/addons/web/controllers/main.py @@ -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): diff --git a/addons/web/tests/test_serving_base.py b/addons/web/tests/test_serving_base.py index 79fb256667b..05cb8630766 100644 --- a/addons/web/tests/test_serving_base.py +++ b/addons/web/tests/test_serving_base.py @@ -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""" + + +
+ + + """ + }) + 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):