From 2eccc4366971b661a5031ee035aadbba8722ae26 Mon Sep 17 00:00:00 2001 From: Younn Olivier Date: Wed, 23 Feb 2022 10:12:16 +0000 Subject: [PATCH] [FIX] website: add scss variables to the disable snippets assets cron Commit [1] introduced a cron that disables outdated js and scss snippets assets. For example, if a new version of the masonry snippet is defined with new assets: data-vcss="001" and data-vjs="001", the previous assets have to be disabled (000.scss and 000.js). That cron was not handling the scss variables files at all, so when an outdated snippet was still used in the db and the cron was run, the 000.scss file was activated, but not the 000_variables.scss and it was crashing the assets. Now, the XXX_variables.scss assets files are handled alongside the XXX.scss files. Also, the regex used in the _is_snippet_used method was only returning the first match of a snippet in an html field. So if a view had a snippet up to date and then an outdated snippet of the same type, the old assets were not activated. The 'g' flag was added to return all the occurences of the snippet in an html field. [1]: https://github.com/odoo/odoo/commit/fa1c05e7bb2959c6025a806a4348f69ed225b4ec task-2775188 closes odoo/odoo#85360 X-original-commit: 644067e80d4ca3f41707cddd15ed8f2807a1e410 Signed-off-by: Quentin Smetz (qsm) --- addons/website/models/website.py | 11 +-- .../test_disable_unused_snippets_assets.py | 69 ++++++++++++++----- 2 files changed, 57 insertions(+), 23 deletions(-) diff --git a/addons/website/models/website.py b/addons/website/models/website.py index da17cdb23fa..1fe1d3c997d 100644 --- a/addons/website/models/website.py +++ b/addons/website/models/website.py @@ -1417,7 +1417,7 @@ class Website(models.Model): # As well as every snippet dropped in html fields self.env.cr.execute(sql.SQL(" UNION ").join( - sql.SQL('SELECT regexp_matches({}, {}) FROM {}').format( + sql.SQL("SELECT regexp_matches({}, {}, 'g') FROM {}").format( sql.Identifier(column), sql.Placeholder('snippet_regex'), sql.Identifier(table) @@ -1442,16 +1442,17 @@ class Website(models.Model): for snippet_module, snippet_id, asset_version, asset_type, _ in snippets_assets: is_snippet_used = self._is_snippet_used(snippet_module, snippet_id, asset_version, asset_type, html_fields) - filename_type = 'scss' if asset_type == 'css' else asset_type - assets_path = f'{snippet_id}/{asset_version}.{filename_type}' + + # The regex catches XXX.scss, XXX.js and XXX_variables.scss + assets_regex = f'{snippet_id}/{asset_version}.+{asset_type}' # The query will also set to active or inactive assets overrides, as they # share the same snippet_id, asset_version and filename_type as their parents self.env.cr.execute(""" UPDATE ir_asset SET active = %(active)s - WHERE path ~ %(assets_path)s - """, {"active": is_snippet_used, "assets_path": assets_path}) + WHERE path ~ %(assets_regex)s + """, {"active": is_snippet_used, "assets_regex": assets_regex}) def _search_build_domain(self, domain, search, fields, extra=None): """ diff --git a/addons/website/tests/test_disable_unused_snippets_assets.py b/addons/website/tests/test_disable_unused_snippets_assets.py index 1e968231700..1ad9fce76fe 100644 --- a/addons/website/tests/test_disable_unused_snippets_assets.py +++ b/addons/website/tests/test_disable_unused_snippets_assets.py @@ -16,24 +16,37 @@ class TestDisableSnippetsAssets(TransactionCase): self.homepage = self.View.create({ 'name': 'Home', 'type': 'qweb', - 'arch_db': HOMEPAGE_WITH_OUTDATED_S_WEBSITE_FORM, + 'arch_db': HOMEPAGE_OUTDATED, 'key': 'website.homepage', }) self.mega_menu = self.WebsiteMenu.create({ 'name': 'Image Gallery V001', - 'mega_menu_content': MEGA_MENU_CONTENT_IMAGE_GALLERY_V001, + 'mega_menu_content': MEGA_MENU_UP_TO_DATE, }) self.initial_active_snippets_assets = self._get_active_snippets_assets() - def test_homepage_with_outdated_s_website_form(self): + def test_homepage_outdated_and_mega_menu_up_to_date(self): self.Website._disable_unused_snippets_assets() - s_website_form_000 = self._get_snippet_asset('s_website_form', '000', 'scss') - s_website_form_001 = self._get_snippet_asset('s_website_form', '001', 'scss') + # Old snippet with scss and js + s_website_form_000_scss = self._get_snippet_asset('s_website_form', '000', 'scss') + s_website_form_001_scss = self._get_snippet_asset('s_website_form', '001', 'scss') + s_website_form_000_js = self._get_snippet_asset('s_website_form', '000', 'js') + self.assertEqual(s_website_form_000_scss.active, True) + self.assertEqual(s_website_form_001_scss.active, True) + self.assertEqual(s_website_form_000_js.active, True) + + # Old snippet with scss and scss variables + s_masonry_block_000_scss = self._get_snippet_asset('s_masonry_block', '000', 'scss') + s_masonry_block_000_variables_scss = self._get_snippet_asset('s_masonry_block', '000_variables', 'scss') + s_masonry_block_001_scss = self._get_snippet_asset('s_masonry_block', '001', 'scss') + self.assertEqual(s_masonry_block_000_scss.active, True) + self.assertEqual(s_masonry_block_000_variables_scss.active, True) + self.assertEqual(s_masonry_block_001_scss.active, True) + + # New snippet s_image_gallery_000 = self._get_snippet_asset('s_image_gallery', '000', 'scss') s_image_gallery_001 = self._get_snippet_asset('s_image_gallery', '001', 'scss') - self.assertEqual(s_website_form_000.active, True) - self.assertEqual(s_website_form_001.active, True) self.assertEqual(s_image_gallery_000.active, False) self.assertEqual(s_image_gallery_001.active, True) @@ -46,20 +59,31 @@ class TestDisableSnippetsAssets(TransactionCase): 'Following snippets are not following the snippet versioning system structure, or their previous assets have not been deactivated:\n' + '\n'.join(unwanted_snippets_assets_changes)) - def test_homepage_with_s_website_form_V001(self): + def test_homepage_up_to_date_and_mega_menu_outdated(self): self.homepage.write({ - 'arch_db': HOMEPAGE_WITH_S_WEBSITE_FORM_V001, + 'arch_db': HOMEPAGE_UP_TO_DATE, }) self.mega_menu.write({ - 'mega_menu_content': MEGA_MENU_CONTENT_IMAGE_GALLERY_OUTDATED, + 'mega_menu_content': MEGA_MENU_OUTDATED, }) self.Website._disable_unused_snippets_assets() - s_website_form_000 = self._get_snippet_asset('s_website_form', '000', 'scss') - s_website_form_001 = self._get_snippet_asset('s_website_form', '001', 'scss') + + s_website_form_000_scss = self._get_snippet_asset('s_website_form', '000', 'scss') + s_website_form_001_scss = self._get_snippet_asset('s_website_form', '001', 'scss') + s_website_form_000_js = self._get_snippet_asset('s_website_form', '000', 'js') + self.assertEqual(s_website_form_000_scss.active, False) + self.assertEqual(s_website_form_001_scss.active, True) + self.assertEqual(s_website_form_000_js.active, True) + + s_masonry_block_000_scss = self._get_snippet_asset('s_masonry_block', '000', 'scss') + s_masonry_block_000_variables_scss = self._get_snippet_asset('s_masonry_block', '000_variables', 'scss') + s_masonry_block_001_scss = self._get_snippet_asset('s_masonry_block', '001', 'scss') + self.assertEqual(s_masonry_block_000_scss.active, False) + self.assertEqual(s_masonry_block_000_variables_scss.active, False) + self.assertEqual(s_masonry_block_001_scss.active, True) + s_image_gallery_000 = self._get_snippet_asset('s_image_gallery', '000', 'scss') s_image_gallery_001 = self._get_snippet_asset('s_image_gallery', '001', 'scss') - self.assertEqual(s_website_form_000.active, False) - self.assertEqual(s_website_form_001.active, True) self.assertEqual(s_image_gallery_000.active, True) self.assertEqual(s_image_gallery_001.active, True) @@ -69,7 +93,7 @@ class TestDisableSnippetsAssets(TransactionCase): def _get_active_snippets_assets(self): return self.IrAsset.search([('path', 'like', 'snippets'), ('active', '=', True)]).mapped('path') -HOMEPAGE_WITH_S_WEBSITE_FORM_V001 = """ +HOMEPAGE_UP_TO_DATE = """ @@ -80,6 +104,9 @@ HOMEPAGE_WITH_S_WEBSITE_FORM_V001 = """ +
+
+
@@ -89,7 +116,7 @@ HOMEPAGE_WITH_S_WEBSITE_FORM_V001 = """ """ -HOMEPAGE_WITH_OUTDATED_S_WEBSITE_FORM = """ +HOMEPAGE_OUTDATED = """ @@ -98,6 +125,12 @@ HOMEPAGE_WITH_OUTDATED_S_WEBSITE_FORM = """
+
+
+
+
+
+
@@ -107,7 +140,7 @@ HOMEPAGE_WITH_OUTDATED_S_WEBSITE_FORM = """ """ -MEGA_MENU_CONTENT_IMAGE_GALLERY_V001 = """ +MEGA_MENU_UP_TO_DATE = """
@@ -119,7 +152,7 @@ MEGA_MENU_CONTENT_IMAGE_GALLERY_V001 = """
""" -MEGA_MENU_CONTENT_IMAGE_GALLERY_OUTDATED = """ +MEGA_MENU_OUTDATED = """