This commit goal is to encapsulate every tour-test into a separate assets bundle that would be called only during tests (command line) or by URL (debug=tests). That way, a lot of .js files would not be loaded anymore uselessly outside test mode and will speed up the page loads (especially in the frontend as the backend do not reload the page anyway). In order to do that, we needed to propagate the `debug` state from page to page. Otherwise, every page change during a test would simply lose the debug mode and the test would stop as the test assets would not be loaded. As we could not add the `&debug=tests` on every link (either hardcoded or preprocess during the rendering), it has been decided to store it in session. Technical summary: 1. `debug` state (currently only stored in URL) will be stored in session. Either when adding the `debug` param in URL (handled with _dispatch) or by starting Odoo with `test-enable` or `test-file` (handled by session init). 2. Once activated (and so set in session), debug mode will remain activated even if not visible in URL (after a page navigation eg). To deactivate it, set its value to nothing, `debug=`. That will exit debug mode (whatever mode it is: debug, assets, tests). 3. As tour-test files are now in a separate bundle, every layout (when needed) should `t-call="web.conditional_assets_tests"`. As it would be redundant and verbose, no `t-if` is needed on the t-call to load it only in test debug mode. That will be handled by `compiled_assets_tests` that will actually do the conditionnal t-call-assets to web.assets_tests, if tests debug mode is activated. 4. In addition to separating the tour-tests files in a separate bundle, we also moved those files to a specific folder under /static/tests/tours next to QUnit tests. 5. Also, tour files will be moved in a specific folder /static/src/js/tours for cleanness purpose (those files will still be kept in their 'normal' assets as needed outside test mode since it is tours). This will be done in the next commit. 6. It is possible to enable multiple debug mode, such as 'tests' and 'assets' together. Simply separate debug modes with a comma, eg '?debug=assets,tests' task-1934445 Comes with https://github.com/odoo/enterprise/pull/4281 Closes #33213
72 lines
2.5 KiB
Python
72 lines
2.5 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
import re
|
|
from collections import OrderedDict
|
|
|
|
from odoo import models
|
|
from odoo.addons.http_routing.models.ir_http import url_for
|
|
from odoo.http import request
|
|
from odoo.addons.base.models.assetsbundle import AssetsBundle
|
|
from odoo.tools import html_escape as escape
|
|
|
|
re_background_image = re.compile(r"(background-image\s*:\s*url\(\s*['\"]?\s*)([^)'\"]+)")
|
|
|
|
|
|
class AssetsBundleMultiWebsite(AssetsBundle):
|
|
def _get_asset_url_values(self, id, unique, extra, name, sep, type):
|
|
website_id = self.env.context.get('website_id')
|
|
website_id_path = website_id and ('%s/' % website_id) or ''
|
|
extra = website_id_path + extra
|
|
res = super(AssetsBundleMultiWebsite, self)._get_asset_url_values(id, unique, extra, name, sep, type)
|
|
return res
|
|
|
|
|
|
class QWeb(models.AbstractModel):
|
|
""" QWeb object for rendering stuff in the website context """
|
|
|
|
_inherit = 'ir.qweb'
|
|
|
|
URL_ATTRS = {
|
|
'form': 'action',
|
|
'a': 'href',
|
|
'link': 'href',
|
|
'script': 'src',
|
|
'img': 'src',
|
|
}
|
|
|
|
def get_asset_bundle(self, xmlid, files, remains=None, env=None):
|
|
return AssetsBundleMultiWebsite(xmlid, files, remains=remains, env=env)
|
|
|
|
def _post_processing_att(self, tagName, atts, options):
|
|
if atts.get('data-no-post-process'):
|
|
return atts
|
|
|
|
atts = super(QWeb, self)._post_processing_att(tagName, atts, options)
|
|
|
|
if options.get('inherit_branding') or options.get('rendering_bundle') or \
|
|
options.get('edit_translations') or options.get('debug') or (request and request.session.debug):
|
|
return atts
|
|
|
|
website = request and getattr(request, 'website', None)
|
|
if not website and options.get('website_id'):
|
|
website = self.env['website'].browse(options['website_id'])
|
|
|
|
if not website:
|
|
return atts
|
|
|
|
name = self.URL_ATTRS.get(tagName)
|
|
if request and name and name in atts:
|
|
atts[name] = url_for(atts[name])
|
|
|
|
if not website.cdn_activated:
|
|
return atts
|
|
|
|
if name and name in atts:
|
|
atts = OrderedDict(atts)
|
|
atts[name] = website.get_cdn_url(atts[name])
|
|
if isinstance(atts.get('style'), str) and 'background-image' in atts['style']:
|
|
atts = OrderedDict(atts)
|
|
atts['style'] = re_background_image.sub(lambda m: '%s%s' % (m.group(1), website.get_cdn_url(m.group(2))), atts['style'])
|
|
|
|
return atts
|