Skip to content

Commit

Permalink
bad POC for replace ugly image
Browse files Browse the repository at this point in the history
  • Loading branch information
seb-odoo committed Feb 22, 2019
1 parent 91a275d commit 1e6339c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
9 changes: 3 additions & 6 deletions addons/web/controllers/main.py
Expand Up @@ -1057,17 +1057,14 @@ def content_image(self, xmlid=None, model='ir.attachment', id=None, field='datas
if suffix in ('small', 'medium', 'large', 'big'):
content = getattr(odoo.tools, 'image_resize_image_%s' % suffix)(content)

# if int(kw.get('img_optimize', 0)):
# width = 120
# height = 0
content = limited_image_resize(
content, width=width, height=height, crop=crop, upper_limit=upper_limit, avoid_if_small=avoid_if_small)

image_base64 = base64.b64decode(content)

quality = int(kw.get('quality', 80))

# if request and 'image/webp' in request.httprequest.headers.get('Accept'):
# image_base64 = odoo.tools.image.image_save_for_web_webp(image_base64=image_base64, quality=quality)
# headers = self.force_contenttype(headers, contenttype='image/webp')

headers.append(('Content-Length', len(image_base64)))
response = request.make_response(image_base64, headers)
response.status_code = status
Expand Down
18 changes: 18 additions & 0 deletions addons/web/static/src/js/boot.js
Expand Up @@ -290,4 +290,22 @@
};
$(log_when_loaded);

// function optimizeImages() {
// $('img.o_we_custom_image[src*="img_optimize=1"]').each(function () {
// var $img = $(this);
// setTimeout(function () {
// var url = $img.attr('src').replace('img_optimize=1', 'img_optimize=0');
// var $downloadingImage = $("<img>");
// $downloadingImage.load(function () {
// $img.attr('src', url);
// });
// $downloadingImage.attr("src", url);
// }, 2000);
// });
// }
// // TODO SEB this won't work for newly added elements...
// $(document).ready(function () {
// optimizeImages();
// });

})();
9 changes: 9 additions & 0 deletions addons/web_editor/models/ir_ui_view.py
Expand Up @@ -4,6 +4,7 @@
import copy
import logging
from lxml import etree, html
from werkzeug import urls

from odoo.exceptions import AccessError
from odoo import api, fields, models
Expand Down Expand Up @@ -172,6 +173,14 @@ def save(self, value, xpath=None):
arch_section = html.fromstring(
value, parser=html.HTMLParser(encoding='utf-8'))

# for img in arch_section.xpath(".//img[hasclass('o_we_custom_image')]"):
# url = img.get('src', '')
# url = urls.url_parse(url)
# url_params = url.decode_query()
# url_params.update([("img_optimize", 1)])
# url = url.replace(query=urls.url_encode(url_params)).to_url()
# img.set('src', url)

if xpath is None:
# value is an embedded field on its own, not a view section
self.save_embedded_field(arch_section)
Expand Down

0 comments on commit 1e6339c

Please sign in to comment.