From 0dc347370f052d31351535a7d9f45ac38624b6e1 Mon Sep 17 00:00:00 2001 From: Julien Mougenot Date: Thu, 20 Jan 2022 10:00:03 +0000 Subject: [PATCH] [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) Signed-off-by: Julien Mougenot (jum) --- addons/web/controllers/main.py | 27 +++++++++++++++++------ addons/web/tests/test_serving_base.py | 31 ++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 8 deletions(-) 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):