flectra/addons/website/models/ir_qweb.py

52 lines
1.7 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2018-01-16 11:34:37 +01:00
# Part of Odoo, Flectra. See LICENSE file for full copyright and licensing details.
2018-07-13 11:51:12 +02:00
import re
from collections import OrderedDict
2018-01-16 11:34:37 +01:00
from flectra import models
from flectra.http import request
2018-07-13 11:51:12 +02:00
re_background_image = re.compile(r"(background-image\s*:\s*url\(\s*['\"]?\s*)([^)'\"]+)")
class QWeb(models.AbstractModel):
""" QWeb object for rendering stuff in the website context """
_inherit = 'ir.qweb'
URL_ATTRS = {
2018-07-13 11:51:12 +02:00
'form': 'action',
'a': 'href',
'link': 'href',
'script': 'src',
'img': 'src',
}
2018-07-13 11:51:12 +02:00
def _post_processing_att(self, tagName, atts, options):
if atts.get('data-no-post-process'):
return atts
2018-07-13 11:51:12 +02:00
atts = super(QWeb, self)._post_processing_att(tagName, atts, options)
2018-07-13 11:51:12 +02:00
if options.get('inherit_branding') or options.get('rendering_bundle') or \
options.get('edit_translations') or options.get('debug') or (request and request.debug):
return atts
2018-07-13 11:51:12 +02:00
website = request and getattr(request, 'website', None)
if not website and options.get('website_id'):
website = self.env['website'].browse(options['website_id'])
2018-07-13 11:51:12 +02:00
if not website or not website.cdn_activated:
return atts
2018-07-13 11:51:12 +02:00
name = self.URL_ATTRS.get(tagName)
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